|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Thursday, May 27, 2010 1:31 PM
Points: 132,
Visits: 101
|
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 12:46 AM
Points: 2,655,
Visits: 717
|
|
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).
Just because you're right doesn't mean everybody else is wrong.
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 1:12 AM
Points: 1,631,
Visits: 2,028
|
|
I liked the question. I got it wrong and I learnt something. Thank you.
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Monday, May 06, 2013 3:27 AM
Points: 1,864,
Visits: 297
|
|
| Which capabilities is CAST missing when it "has less capabilities in SQL Server"?
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 7:46 AM
Points: 2,018,
Visits: 4,913
|
|
Convert has a third parameter that is optional and is called style. It is mostly used with dates. For example check out the next script:
declare @dt char(10)
select @dt = '31/01/2010'
--This will always work select convert(smalldatetime, @dt, 103)
--This might work and might fail select cast (@dt as smalldatetime) go
declare @dt char(10)
select @dt = '01/31/2010'
--This will always work select convert(smalldatetime, @dt, 101)
--This might work and might fail select cast (@dt as smalldatetime)
Notice that with the convert function, I was able to work with the string as date regardless of the format that I was using because of the third parameter, but when I used cast function only one format could be cast to smalldatetime.
Adi
-------------------------------------------------------------- To know how to ask questions and increase the chances of getting asnwers: http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 7:11 PM
Points: 7,079,
Visits: 7,122
|
|
Good question, correct answer (well, it does depend on how one interprets "platform" but it's correct for what I think is the natural interpretation) but the explanation is a little lacking in that it appears to suggest that the style parameter of convert is only for datetime, whereas in fact it also is used for binary, float, real, money, smallmoney, and xml types.
Tom Que conclure à la fin de tous mes longs propos? C'est que les préjugés sont la raison des sots. (Voltaire, 1756)
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 10:50 AM
Points: 6,367,
Visits: 8,227
|
|
CONVERT has the third parameter "style" which can be used when converting datetime to string The style parameter can also used when converting float/real, money/smallmoney, xml and binary/varbinary/char/varchar.
See BOL for more information.
Wayne Microsoft Certified Master: SQL Server 2008 If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it! Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines, CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Tuesday, October 16, 2012 4:11 PM
Points: 449,
Visits: 168
|
|
I took platforms to mean various RDMS' (Oracle, Sybase, etc.). And I knew CAST worked in Oracle (since that's where I'm spending alot of time lately) but wasn't sure about CONVERT. Took a gamble, got it right.
The distance between genius and insanity is measured only by success.
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Wednesday, May 08, 2013 10:33 AM
Points: 10,989,
Visits: 10,529
|
|
I am shocked and appalled that Jeff hasn't posted anything about code portability being a myth yet. Very disappointing 
I love CONVERT.
Paul White SQL Server MVP SQLblog.com @SQL_Kiwi
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Monday, May 06, 2013 8:44 AM
Points: 90,
Visits: 129
|
|
| CONVERT works across all platforms & has more capabilities in SQL Server is also true
|
|
|
|