SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 data types related question
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

nolan_1184
Starting Member

1 Posts

Posted - 02/17/2013 :  00:44:26  Show Profile  Reply with Quote
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

India
52249 Posts

Posted - 02/17/2013 :  01:45:36  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000