SQL CAST, CONVERT 比較

來源:互聯網
上載者:User
   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
顯示結果:

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.