Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««1234»»»

Cast vs. Convert Expand / Collapse
Author
Message
Posted Monday, March 8, 2010 8:22 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 1:13 PM
Points: 1,384, Visits: 1,757
I've done a lot of SQLXML programming with simple queries in XML files and XSLT files to transform those query results to HTML pages. I used CONVERT a lot to convert money and datetime values to varchar in the format I needed before it even gets to the XSLT, saving a lot of messy XSL formatting on the front end.

I'm quite surprised that, at the time I answered the question, approximately 3/4 of people got this question wrong. Just under 1/3 of respondents thought there was no real difference! This question, to me, is T-SQL 101. Even if you don't know that CONVERT() is T-SQL-specific, you should know the differences in inputs and potential outputs.

This is not criticism of those who got the question wrong: we all have holes in our knowledge. But it does highlight the value of this QoTD. Before this question, I would have assumed this to be a universally-known topic in MSSQL circles. Now I know I can't make that assumption. And now those who didn't know of the difference have learned something.

Great question!
Post #878650
Posted Monday, March 8, 2010 8:30 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 1:13 PM
Points: 1,384, Visits: 1,757
Rune Bivrin (3/8/2010)
Good question, but which alternative is correct depends on how you define platform. If you define "platform" as "versions of SQL Server", then CONVERT works in every version whereas CAST was introduced in 7.0 (IIRC - could've been 2000).


Well, if you define "black" as "white", you can prove anything. The standard definition of platform is independent of versions of specific platforms. I think it's quite ironic that you're criticizing a question that compares an ANSI standard function to a vendor-specific function by relying on a non-standard definition.
Post #878657
Posted Monday, March 8, 2010 9:17 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Yesterday @ 3:32 PM
Points: 18,068, Visits: 16,111
Good question.



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Post #878699
Posted Monday, March 8, 2010 9:20 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Yesterday @ 3:32 PM
Points: 18,068, Visits: 16,111
Paul White (3/8/2010)
I am shocked and appalled that Jeff hasn't posted anything about code portability being a myth yet.
Very disappointing

I love CONVERT.



He'll be in later to handle that.




Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Post #878706
Posted Monday, March 8, 2010 9:39 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 7:18 PM
Points: 7,930, Visits: 9,654
Paul White (3/8/2010)
I am shocked and appalled that Jeff hasn't posted anything about code portability being a myth yet.
Very disappointing

I love CONVERT.

I'm sure he will soon.
Worrying about portability is crazy until we get a relational dbms with a relational language - then we can just worry about porting to that.


Tom
Post #878726
Posted Monday, March 8, 2010 9:42 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, February 4, 2011 7:20 AM
Points: 977, Visits: 1,499
I thought I knew the answer, but BZZZZZZT!

Nice question.


Tom Garth
Vertical Solutions

"There are three kinds of men. The one that learns by reading. The few who learn by observation. The rest of them have to pee on the electric fence for themselves." -- Will Rogers
Post #878729
Posted Monday, March 8, 2010 9:44 AM


Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Friday, December 19, 2014 6:38 AM
Points: 685, Visits: 277
bouarroudj Mohamed (3/8/2010)
CONVERT works across all platforms & has more capabilities in SQL Server is also true


That is what I am looking at as well, does this not mean the same as the given right answer?
If one has less the other must have more?
Post #878732
Posted Monday, March 8, 2010 10:21 AM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Tuesday, December 7, 2010 12:55 AM
Points: 771, Visits: 504
erwin.oosterhoorn (3/8/2010)
bouarroudj Mohamed (3/8/2010)
CONVERT works across all platforms & has more capabilities in SQL Server is also true


That is what I am looking at as well, does this not mean the same as the given right answer?
If one has less the other must have more?


If I understand correctly, CONVERT is MS SQL specific whereas CAST is ANSI-SQL and will work on multiple platforms (i.e. Oracle, DB2, etc.).

Therefore, the above statement is incorrect because CONVERT does NOT work across all platforms (though it does have more capabilities in SQL Server).

Please correct me if I'm wrong.

--
Kevin C.
Post #878757
Posted Monday, March 8, 2010 10:26 AM


Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Friday, December 19, 2014 6:38 AM
Points: 685, Visits: 277
KevinC. (3/8/2010)
erwin.oosterhoorn (3/8/2010)
bouarroudj Mohamed (3/8/2010)
CONVERT works across all platforms & has more capabilities in SQL Server is also true


That is what I am looking at as well, does this not mean the same as the given right answer?
If one has less the other must have more?


If I understand correctly, CONVERT is MS SQL specific whereas CAST is ANSI-SQL and will work on multiple platforms (i.e. Oracle, DB2, etc.).

Therefore, the above statement is incorrect because CONVERT does NOT work across all platforms (though it does have more capabilities in SQL Server).

Please correct me if I'm wrong.

--
Kevin C.


Thanks Kevin, if you are correct than that makes sense now.
Post #878761
Posted Monday, March 8, 2010 11:57 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: 2 days ago @ 4:41 AM
Points: 3,119, Visits: 997
sknox (3/8/2010)
Rune Bivrin (3/8/2010)
Good question, but which alternative is correct depends on how you define platform. If you define "platform" as "versions of SQL Server", then CONVERT works in every version whereas CAST was introduced in 7.0 (IIRC - could've been 2000).


Well, if you define "black" as "white", you can prove anything. The standard definition of platform is independent of versions of specific platforms. I think it's quite ironic that you're criticizing a question that compares an ANSI standard function to a vendor-specific function by relying on a non-standard definition.

Who says I'm criticizing the question?

I'm merely pointing out that since this is SQL Server Central, and not DBMS central, such an interpretation isn't completely out of order. And from that point of view CONVERT is somewhat more universal, even if I sincerely hope noone still wrestles with SQL Server 6.5...



Just because you're right doesn't mean everybody else is wrong.
Post #878829
« Prev Topic | Next Topic »

Add to briefcase ««1234»»»

Permissions Expand / Collapse