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
 CAST function....

Author  Topic 

ZarrinPour
Yak Posting Veteran

66 Posts

Posted - 2008-09-08 : 13:53:17
Hi
i want to know when i use cast function to convert a numeric data to varchar value, if i don't provide length,what does it mean?

Kind Regards.

Nothing is quite impossible!

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-09-08 : 13:58:45
get used to checking Books Online - It's a great source.
quote:

Syntax for CAST:
CAST ( expression AS data_type [ (length ) ])

length
Is an optional parameter of nchar, nvarchar, char, varchar, binary, or varbinary data types. For CONVERT, if length is not specified, the default to 30 characters.



Be One with the Optimizer
TG
Go to Top of Page

ZarrinPour
Yak Posting Veteran

66 Posts

Posted - 2008-09-08 : 14:05:09
Hi TG

quote:

Originally posted by TG

get used to checking Books Online - It's a great source.
[quote]
For CONVERT, if length is not specified, the default to 30 characters.


I've already read this in BOL, BUT what about CAST?

Kind Regards.

Nothing is quite impossible!
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-09-08 : 14:12:44
I didn't notice the "for CONVERT" but I assume its the same for CAST. This little test seems to confirm it:

use tempdb
select cast(0 as varchar) test into #t
exec sp_help #t

OUTPUT:

Column_name Type Computed Length
---------------------------------------------------------------------------------------- -------
test varchar no 30



Be One with the Optimizer
TG
Go to Top of Page

ZarrinPour
Yak Posting Veteran

66 Posts

Posted - 2008-09-08 : 14:21:55
Thanks sooo much.

Nothing is quite impossible!
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-09-08 : 15:16:04
Now you've learned the two steps to being a good programmer: If you have a question, first check the documentation. If that doesn't answer it, just write a simple test.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-09-09 : 02:38:44
Also read http://sqlblogcasts.com/blogs/madhivanan/archive/2007/12/04/column-length-and-data-length.aspx

Madhivanan

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

- Advertisement -