• SQL CAST, CONVERT 比较


    本文转自:http://www.cnblogs.com/denylau/archive/2010/12/01/1893371.html

    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 convert (char ,starttime+1,102) from itemtransaction
    select top Convert (char ,starttime,108) from itemtransaction

    select top cast (( convert (char ,starttime+1,102)+Convert (char ,starttime,108)) as datetime ) from itemtransaction
     
    显示结果:

  • 相关阅读:
    利用Github Actions自动保持 GitHub 提交状态常绿
    windows下搭建轻量级php代码审计环境
    jquery实现点击弹出对话框
    redis学习记录(1)Redis简介
    StatefulSet
    Pod
    Spark 写Hive指定动态分区
    我常用的git操作
    在C#中如何使用GetOpenFileName函数多选文件
    image matching challenge
  • 原文地址:https://www.cnblogs.com/wangjiahong/p/3427905.html
Copyright © 2020-2023  润新知