本文共 2978 字,大约阅读时间需要 9 分钟。
转换函数
SELECT PARSE ( '2.111111' AS float ), CAST ( '2.111111111111' AS FLOAT )---------------------- ----------------------2.111111 2.111111111111( 1 row ( s ) affected )SELECT PARSE ( '19010101' AS DATETIME ), CAST ( '19010101' AS DATETIME )Msg 9819 , Level 16 , State 1 , Line 63Error converting string value '19010101' into data type datetime using culture '' .SELECT PARSE ( '1901-01-01' AS DATETIME ), CAST ( '1901-01-01' AS DATETIME )----------------------- -----------------------1901 - 01 - 01 00 : 00 : 00.000 1901 - 01 - 01 00 : 00 : 00.000( 1 row ( s ) affected )新功能转日期的时候格式一定要注意,不然转不过去,没有cast好用。
SELECT CASE WHEN TRY_CONVERT(float, 'test') IS NULLTHEN 'Cast failed'ELSE 'Cast succeeded'END AS Result;GO
SELECT TRY_PARSE ( '19010101' AS DATETIME ), CAST ( '19010101' AS DATETIME )----------------------- -----------------------NULL 1901 - 01 - 01 00 : 00 : 00.000
SELECTDATEFROMPARTS ( 2010 , 12 , 31 ), CAST ( '20101231' AS date )---------- ----------2010 - 12 - 31 2010 - 12 - 31
SELECT DATETIME2FROMPARTS ( 2011 , 8 , 15 , 14 , 23 , 44 , 5 , 1 ), DATETIME2FROMPARTS ( 2011 , 8 , 15 , 14 , 23 , 44 , 50 , 2 ), DATETIME2FROMPARTS ( 2011 , 8 , 15 , 14 , 23 , 44 , 500 , 3 );GO--------------------------- --------------------------- ---------------------------2011 - 08 - 15 14 : 23 : 44.5 2011 - 08 - 15 14 : 23 : 44.50 2011 - 08 - 15 14 : 23 : 44.500
SELECT DATETIMEFROMPARTS ( 2010 , 12 , 31 , 23 , 59 , 59 , 0 ) ;-----------------------2010 - 12 - 31 23 : 59 : 59.000
DECLARE @date DATETIME = GETDATE ();SELECT EOMONTH ( @date ) AS 'This Month' , EOMONTH ( @date , 1 ) AS 'Next Month' , EOMONTH ( @date , - 1 ) AS 'Last Month' ;GOThis Month Next Month Last Month---------- ---------- ----------2016 - 08 - 31 2016 - 09 - 30 2016 - 07 - 31
SELECT CHOOSE ( 3 , 'Manager' , 'Director' , 'Developer' , 'Tester' )---------Developer
SELECT IIF ( (SELECT COUNT( 1) FROM dbo. t1)>= 1, (SELECT COUNT (1 ) FROM dbo .t1 ), 0 )-----------1
SELECT CONCAT ( 'Happy ' , 'Birthday ' , 11 , '/' , '25' )------------------------------Happy Birthday 11 / 25
DECLARE @d DATETIME = '10/01/2011' ;SELECT FORMAT ( @d , 'D' , 'en-US' ) AS 'US English Result'US English Result---------------------------Saturday , October 1 , 2011本文转自 Fanr_Zh 博客园博客,原文链接:http://www.cnblogs.com/Amaranthus/p/5752804.html ,如需转载请自行联系原作者