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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 CAST v CONVERT

Author  Topic 

Pace
Constraint Violating Yak Guru

264 Posts

Posted - 2006-12-18 : 05:12:28
Hi All,

Please can someone explain to me what the difference is between CAST and CONVERT.

For example if you run the following;

SELECT
CONVERT(nvarchar(30), GETDATE())AS UsingConvert,
CAST(GETDATE() AS nvarchar(30)) AS UsingCast
GO

Gives you the same result set.

"Impossible is Nothing"

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-12-18 : 05:19:48
CAST and CONVERT basically provide similar functionality, but CONVERT gives you more control over the formatting of the value by providing Style parameter.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-12-18 : 05:37:11
I provide to think of CONVERT as a function, and use it as such. The syntax of CAST suits my programming style less.

Kristen
Go to Top of Page

Pace
Constraint Violating Yak Guru

264 Posts

Posted - 2006-12-18 : 05:41:06
Thank you

"Impossible is Nothing"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-12-18 : 07:25:47
CONVERT is more powerful when you want to convert the DATE into different formats.
But if you use front end application, you should format the DATE there

Madhivanan

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

Pace
Constraint Violating Yak Guru

264 Posts

Posted - 2006-12-18 : 11:58:38
Really?

I tend to SP absolutely all INSERT UPDATE DELETE statements...

I appreciate putting the Client back into Client / Server, I tend to only use the client machine for calculating the data that gets sent from the server.

"Impossible is Nothing"
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-12-18 : 12:38:36
"I tend to only use the client machine for calculating the data that gets sent from the server"

So you mean that you would "style" a Date in the Server, rather than the Client?

The reason for not doing this is so that you transfer a "date object" from Server to Client, and that Client doesn't have to parse it [possibly ambiguously] and can do "stuff" with it - like sort into chronological order; compare two dates; format appropriately for the Locale the client is in, etc.

(But I reckon you know that, so maybe I've got the wrong end of what you meant??)

Kristen
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-12-19 : 22:53:06
quote:
Originally posted by Pace

Hi All,

Please can someone explain to me what the difference is between CAST and CONVERT.

For example if you run the following;

SELECT
CONVERT(nvarchar(30), GETDATE())AS UsingConvert,
CAST(GETDATE() AS nvarchar(30)) AS UsingCast
GO

Gives you the same result set.

"Impossible is Nothing"


Why do you need nvarchar anyway?

Madhivanan

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

Kristen
Test

22859 Posts

Posted - 2006-12-20 : 10:43:43
"Why do you need nvarchar anyway?"

To save some CPU cycles on the Client?
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-12-20 : 10:52:01
quote:
Originally posted by Kristen

"Why do you need nvarchar anyway?"

To save some CPU cycles on the Client?



Kristen, you have high sense of Humour

Madhivanan

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

Kristen
Test

22859 Posts

Posted - 2006-12-20 : 10:57:45
I thought you'd like that one!!
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2006-12-20 : 12:48:30
CAST is ANSI, which is good if you are multi platform dimensional

(beam me up Scotty)


CONVERT does allow date, and integer conversions...but that should be done in the front end anyway



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page
   

- Advertisement -