【问题标题】:DateTime Conversion Error in T-SQLT-SQL 中的日期时间转换错误
【发布时间】:2011-07-29 09:32:03
【问题描述】:

我在一个名为 TimeZone 的表中有两列 Arrive_Date 和 Interval。我正在尝试添加这两列以获得第三列,其中包含日期和间隔。

我的表有这样的数据:

Interval        Arrive_Date
830             2010-11-01 00:00:00.000
1100            2010-11-01 00:00:00.000
1230            2010-11-02 00:00:00.000
0               2011-01-04 00:00:00.000
30              2011-03-17 00:00:00.000

我想要第三列

Interval        Arrive_Date                           Arrive_DateTime
830             2010-11-01 00:00:00.000               2010-11-01 08:30:00.000
1100            2010-11-01 00:00:00.000               2010-11-01 11:00:00.000
1230            2010-11-02 00:00:00.000               2010-11-02 12:30:00.000
0               2011-01-04 00:00:00.000               2011-01-04 00:00:00.000
30              2011-03-17 00:00:00.000               2011-03-17 00:30:00.000

我正在使用这个查询:

SELECT CAST(LEFT(CONVERT(VARCHAR,Arrive_DATE,101),10) + ' ' + LEFT(Interval,2) + ':' + RIGHT(Interval,2) + ':00'  AS DATETIME)
from TimeZone

但我收到此错误:

Msg 242, Level 16, State 3, Line 1
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.

谁能帮我解决这个问题?

【问题讨论】:

  • 您已经存在了足够长的时间并提出了足够多的问题来了解如何正确格式化代码 - 请这样做。
  • @JNK 你能告诉我我们如何在这里格式化吗?我试过这样做,但我不知道该怎么做
  • 在文本编辑器中,选择要标记为Code 的内容,然后单击大括号{} 图标。这将“编码”任何选定的内容。

标签: sql sql-server-2005 tsql sql-server-2008 datetime


【解决方案1】:

好的,我没有使用带有数据库引擎的计算机,所以我无法对此进行测试(我同意它们是丑陋的字符串操作),但这是一种方法:

SELECT  Interval, Arrive_Date,
        CAST(CONVERT(VARCHAR(8),Arrive_Date,112) + ' ' + LEFT(RIGHT('0000'+CAST(Interval AS VARCHAR(4)),4),2)+':'+RIGHT('00'+CAST(Interval AS VARCHAR(4)),2)+':00' AS DATETIME) AS Arrive_Datetime
FROM TimeZone

【讨论】:

    【解决方案2】:

    我会使用dateadd() 和数学操作数来完成工作。它应该更快。

    select dateadd(minute, 
                   Interval%100, 
                   dateadd(hour, 
                           CAST(Interval/100 as int), 
                           Arrive_Date)
                  )
    from TimeZone
    

    【讨论】:

      【解决方案3】:

      DATEADD()代替这些丑陋的字符串操作怎么样?

      尝试类似:

      SELECT
          Interval % 100 AS [Minutes],
          CONVERT(INT, Interval / 100) AS [Hours],
          DATEADD(HOUR, CONVERT(INT, Interval / 100), DATEADD(MINUTE, Interval % 100, Arrive_Date)) AS [AllTogether]
      FROM TimeZone
      

      【讨论】:

      • 他仍然需要将他的Interval 转换为小时和分钟。
      • 没错。希望发明这种方式来存储间隔的人受到惩罚......
      【解决方案4】:

      我会使用一个计算列(并且我会将“间隔”存储为从一天开始的几秒钟内所需的偏移量(但这只是我):

      drop table #TimeZone
      go
      create table #TimeZone
      (
        id            int      not null identity(1,1) primary key ,
        interval_hhmm int      not null ,
        Arrive_Date   datetime not null ,
        Arrive_DateTime as dateadd(   mm , interval_hhmm % 100 , -- add the minutes
                             dateadd( hh , interval_hhmm / 100 , -- add the hours
                               convert(datetime,convert(varchar,Arrive_Date,112),112) -- make sure the base date/time value doesn't have a time component
                             )
                           ) ,
      )
      go
      
      insert #TimeZone ( interval_hhmm , Arrive_Date ) values(830  , '2010-11-01 23:59:59.000')
      insert #TimeZone ( interval_hhmm , Arrive_Date ) values(1100 , '2010-11-01 00:00:00.000')
      insert #TimeZone ( interval_hhmm , Arrive_Date ) values(1230 , '2010-11-02 00:00:00.000')
      insert #TimeZone ( interval_hhmm , Arrive_Date ) values(0    , '2011-01-04 00:00:00.000')
      insert #TimeZone ( interval_hhmm , Arrive_Date ) values(30   , '2011-03-17 00:00:00.000')
      
      select * from #timezone
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-11-01
        • 1970-01-01
        • 2018-11-03
        • 1970-01-01
        • 1970-01-01
        • 2013-02-24
        相关资源
        最近更新 更多