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
 difference between char & varchar

Author  Topic 

phanicrn
Starting Member

42 Posts

Posted - 2007-11-06 : 12:45:34
what'z the difference between char & varchar, like i am doing a cast function, which one should be used..

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-11-06 : 12:49:14
varchar is a variable length datatype so it will always hold as many bytes at there is data
char is fixed length datatype and will pad shorter data with spaces to fill it's capacity

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com
Go to Top of Page

phanicrn
Starting Member

42 Posts

Posted - 2007-11-06 : 13:08:20
basically iam converting interger datatype using cast function into char..

month column in database has int16. values as 1,2,3,.....
now i am creating a calculation with case, which show as month=1 then "january' this datatype being generated as character. now when i am sorting on month(calculated column) its performing sorting on character. values as "Aug", "apr"...

but i want my values to appear as January, February.. How do we do this..
Go to Top of Page

rmiao
Master Smack Fu Yak Hacker

7266 Posts

Posted - 2007-11-06 : 21:54:04
What's length of the char column?
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-11-07 : 01:01:58
Use the logic suggested here
http://sqlteam.com/forums/topic.asp?TOPIC_ID=92192

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-11-07 : 01:04:06
or you can use

datename(month,dateadd(month,month_number-1,0))

instead of multiple CASEs

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

phanicrn
Starting Member

42 Posts

Posted - 2007-11-07 : 11:46:41
length of char column is 9. and this datename(month,dateadd(month,month_number-1,0)) didnt work.
Go to Top of Page

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2007-11-07 : 12:01:02
replace "month_number" with your column name containing the number and it will work

this:
datename(month(dateadd(m,1-1,0))
produces "January"
Go to Top of Page

phanicrn
Starting Member

42 Posts

Posted - 2007-11-07 : 16:26:43
replace "month_number" with your column name containing the number and it will work

this:
datename(month(dateadd(m,1-1,0))
produces "January"


in this example, datename(month-- refers to character column or to return as "month"
Go to Top of Page

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2007-11-07 : 16:54:12
quote:
Originally posted by phanicrn

replace "month_number" with your column name containing the number and it will work

this:
datename(month(dateadd(m,1-1,0))
produces "January"


in this example, datename(month-- refers to character column or to return as "month"



Not quite, in this example datename(month...
refers to the month property of the resulting date found in the dateadd sequence. in the OP's example, Month_Number represents his column of numbers. This is quite evident if you actually put my code in the QA.
Go to Top of Page

arorarahul.0688
Posting Yak Master

125 Posts

Posted - 2007-11-08 : 02:22:27
quote:
Originally posted by madhivanan

Use the logic suggested here
http://sqlteam.com/forums/topic.asp?TOPIC_ID=92192

Madhivanan

Failing to plan is Planning to fail


hi madhivanan
Happy diwali to you and your family

Rahul Arora
MCA 07 Batch
NCCE Israna, Panipat
HRY, INDIA

######################
IMPOSSIBLE = I+M+POSSIBLE
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-11-09 : 01:00:06
quote:
Originally posted by arorarahul.0688

quote:
Originally posted by madhivanan

Use the logic suggested here
http://sqlteam.com/forums/topic.asp?TOPIC_ID=92192

Madhivanan

Failing to plan is Planning to fail


hi madhivanan
Happy diwali to you and your family

Rahul Arora
MCA 07 Batch
NCCE Israna, Panipat
HRY, INDIA

######################
IMPOSSIBLE = I+M+POSSIBLE



Thanks and wishing you the same


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -