SQL CAST, CONVERT 比较
本文转自:http://blog.csdn.net/wobuwei/archive/2009/08/18/4458185.aspx
if (@StartTime > @EndTime)
Set @EndTime = cast (convert (char , @ScheduleDate + 1 , 101)+' '+convert (char , @EndTime, 108) as datetime )
Else
Set @EndTime = cast (convert (char , @ScheduleDate , 101)+' '+convert (char , @EndTime, 108) as datetime )
Set @StartTime = cast (convert (char , @ScheduleDate , 101)+' '+convert (char , @StartTime, 108) as datetime )
这条语句中的Convert,Cast 的意思,作用
Convert (data_type[,length],expression[,style])
这个转换函数一般在时间类型和字符串类型转换的时候才用到.
style格式在转换时间时候的格式如下:
Style(2位表示年份) | Style(4位表示年份) | 输入输出格式
------------------------------------------------------------------------------------------------------------
0 | 100 | mon dd yyyy hh:miAM(或PM)
------------------------------------------------------------------------------------------------------------
1 | 101 美国 | mm/dd/yy
------------------------------------------------------------------------------------------------------------
2 | 102 ANSI | yy-mm-dd
------------------------------------------------------------------------------------------------------------
3 | 103 英法 | dd/mm/yy
------------------------------------------------------------------------------------------------------------
4 | 104 德国 | dd.mm.yy
------------------------------------------------------------------------------------------------------------
5 | 105 意大利 | dd-mm-yy
------------------------------------------------------------------------------------------------------------
6 | 106 | dd mon yy
------------------------------------------------------------------------------------------------------------
7 | 107 | mon dd,yy
------------------------------------------------------------------------------------------------------------
8 | 108 | hh:mm:ss
------------------------------------------------------------------------------------------------------------
9 | 109 | mon dd yyyy hh:mi:ss:mmmmAM(或PM)
------------------------------------------------------------------------------------------------------------
10 | 110 美国 | mm-dd-yy
------------------------------------------------------------------------------------------------------------
11 | 111 日本 | yy/mm/dd
------------------------------------------------------------------------------------------------------------
12 | 112 ISO | yymmdd
------------------------------------------------------------------------------------------------------------
13 | 113 欧洲默认值 | dd mon yyyy hh:mi:ss:mmm(24小时制)
------------------------------------------------------------------------------------------------------------
14 | 114 | hh:mi:ss:mmm(24小时制)
------------------------------------------------------------------------------------------------------------
20 | 120 ODBC 规范 | yyyy-mm-dd hh:mi:ss(24小时制)
------------------------------------------------------------------------------------------------------------
21 | 121 | yyyy-mm-dd hh:mi:ss:mmm(24小时制)
Cast(Expression as data_type) : 将某种数据类型的数据显示转换为另外一种类型.
select top 1 starttime from itemtransaction
select top 1 convert (char ,starttime+1,102) from itemtransaction
select top 1 Convert (char ,starttime,108) from itemtransaction
select top 1 cast (( convert (char ,starttime+1,102)+Convert (char ,starttime,108)) as datetime ) from itemtransaction
显示结果: