473,399 Members | 3,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Difference between varchar and int

pradeepjain
563 512MB
Hi,
I was storing integer values like 5000 and 10000 in a column which was defined as varchar.

i was trying to use statement like

1.
Expand|Select|Wrap|Line Numbers
  1. select * from mytable where mycol between '5000' and '10000';

but was not working but
2.
Expand|Select|Wrap|Line Numbers
  1.  select * from mytable where mycol between 5000 and 10000;
but i changed the datatype to int and it started working . 1st statement started working,just confused

Just wanted to know how ' ' was making difference in the statement
Mar 31 '10 #1
4 14820
Atli
5,058 Expert 4TB
Hey.

Anything between single-quotes is considered a string, not a number. When you say '500', MySQL reads that as three characters of text, but when you say 500, MySQL reads that as a single number.

The BETWEEN ... AND operators are meant to check if a value is in between a range of numbers (dates are also considered numbers internally). Trying to use it on strings will not work properly.

I was storing integer values like 5000 and 10000 in a column which was defined as varchar.
This is a big mistake, by the way. A huge waste of space. - When you store an integer as a VARCHAR, the storage requirements for each letter in the number is equal to the total storage requirements if it were stored as an INT.

And that's not to mention the headaces it will give you when you try to actually use it as a number (as you have discovered).
Mar 31 '10 #2
pradeepjain
563 512MB
but
when i make the column as int and use



1.
Expand|Select|Wrap|Line Numbers
  1.  select * from mytable where mycol between '5000' and '10000';

it works. how is it ?
Mar 31 '10 #3
Atli
5,058 Expert 4TB
MySQL must assume the values are supposed to be the same type as the "mycol" column and silently converts them. - It does the same when you try to compare an INT to a string.

However, you should try not to quote values that are supposed to be numbers. Even if MySQL does automatically convert some of them, it costs a few cycles and the behavior may not be consistent.

Bottom line: always leave numbers unquoted, and only quote strings and dates.
Mar 31 '10 #4
pradeepjain
563 512MB
Thanks a lot for help.Its one of the guidlines for me.
Mar 31 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Doug Reese | last post by:
hello, i have what seems to me a very common operation i'm performing. i need to find the balance on an invoice. i was not having any problems until the production server was upgraded to mysql...
5
by: dmhendricks | last post by:
Greetings, I have a question. I work on some SQL2k/ASP.NET apps at work. My predacessor, who created the databases/tables seemed to have liked to use 'char' for all text fields. Is there a...
10
by: Techie | last post by:
what's the difference of the two data types?
22
by: jdokos | last post by:
Hello, I have a question about VARCHAR fields. Our application groups here are starting to use VARCHARs much more frequently. Even VARCHAR (2) to (9) length fields. They say this is because...
7
by: James o'konnor | last post by:
hello. i have the next for create one table into db2 CREATE TABLE "MYSQUEMA"."TABLADEMO" ( "ID" INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY ( START WITH +0 INCREMENT BY +1 MINVALUE +0...
2
by: John Smith | last post by:
What is the difference? Is LONG VARCHAR only 28 bytes longer then VARCHAR? Is this the only difference?
1
by: Troels Arvin | last post by:
Hello, In the DBMS I know best, PostgreSQL, there is no real performance difference between CHAR and VARCHAR. And since CHAR pads with spaces, I generally dislike CHAR (with CHAR, it's not...
6
by: mike | last post by:
so I keep optimizing my fields down to the minimum character length necessary i.e., varchar(15), then I find out a month later its gotta get bigger, then a few months later, bigger again, etc. ...
2
by: bharathreddy | last post by:
Here i will show the differences between nvarchar and varchar: The difference is that nvarchar is used to store unicode data, which is used to store multilingual data in the database tables....
4
by: Nick Chan | last post by:
all these while i've only used varchar for any string i heard from my ex-boss that char helps speed up searches. is that true? so there are these: 1) char with index 2) char without index...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.