Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 data types related question

Author  Topic 

nolan_1184
Starting Member

1 Post

Posted - 2013-02-17 : 00:44:26
let say this small table of Persons

P_id---Firstname---Lastname---Addresss
1------Nevlon------David------H-jol jsj
2------Martin------D'souza----1/78 hdjj
3------Bart--------Cruz-------45 4555

My question: when we declare a datatype for the columnname firstname as vchar(100) ....does this mean that (100) is for a an indivual field in the Firstname column(like its 100 for field Nevlon, then 100 for martin, so on) or just the whole column (like combination of 1, 2, 3 and so on)

second question
what does variable character mean(please explain in layman terms)? why is it different from char data type?

Thanks for reading my question.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-02-17 : 01:45:36
varchar(100) means each value in column can have maximum of 100 characters (ie Nevlon,Martin etc each of those values can be 100 char max)
varchar mean variable character. It implies that based of number of characters actually present, it will assume only the required space. ie for Nevlon it will take only size corresponding to 6 characters. In contrast, char datatypes takes the full size regardless of actual size of data. ie for Nevlon also it takes full 100 character size and fills the rest with spaces. You can check that using below code.


SELECT DATALENGTH(varcharval),DATALENGTH(charval)
FROM
(
SELECT CAST('Nevlon' AS varchar(100)) AS varcharval,CAST('Nevlon' AS char(100)) AS charval
)t


The varchar field will only return size corresponding to 6 characters present whereas char will return size corresponding to entire 100 characters

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -