将int 20171116101856转换为yyyymmddhhmmss格式,用于datediff函数

时间:2017-11-16 15:19:28

标签: sql sql-server datetime datediff

所以我的这个表在int类型中有日期列。

last_run_date | last_run_time
   20171116   | 100234

我试图将这两个值转换为dateif语句中使用的日期时间。

这是我的陈述

SELECT 1
FROM V_Jobs_All_Servers vjas
WHERE JobName='DailyReports_xxxx' and Step_Name='xxxx'
and DATEDIFF(hour, Convert(varchar,STUFF(STUFF(STUFF(STUFF(STUFF(cast(
Convert(varchar(100),vjas.last_run_date) +  Convert(varchar(100),vjas.last_run_time) as varchar)
,5,0,'-'),8,0,'-'),11,0,' '),14,0,':'),17,0,':')), Getdate()) <3

仅当last_run_time值为两位小时格式时才有效 101216,但每当它的一位数小时91316失败并出现以下错误时,

The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

我在SQL Server 2005上

4 个答案:

答案 0 :(得分:1)

你大大超过了这一点,只需用领先的0填充时间值并从那里转换:

declare @t table(last_run_date int, last_run_time int);
insert into @t values(20171116,90234),(20171116,100234);

select last_run_date
      ,last_run_time
      ,convert(datetime,cast(last_run_date as nvarchar(8))
                + ' '
                + stuff(stuff(right('0' + cast(last_run_time as nvarchar(6))
                                   ,6)
                             ,5,0,':')
                       ,3,0,':')
             ,112) as DateTimeData
from @t

输出:

+---------------+---------------+-------------------------+
| last_run_date | last_run_time |      DateTimeData       |
+---------------+---------------+-------------------------+
|      20171116 |        100234 | 2017-11-16 09:02:34.000 |
|      20171116 |        100234 | 2017-11-16 10:02:34.000 |
+---------------+---------------+-------------------------+

答案 1 :(得分:1)

如果您从msdb.dbo.sysjobsteps获得此值,则会有一个内置函数msdb.dbo.agent_datetime(),用于将last_run_datelast_run_time转换为日期时间:

select job_id,
    step_id,
    step_name,
    msdb.dbo.agent_datetime(nullif(last_run_date,0),nullif(last_run_time,0)) as last_run_datetime
from msdb.dbo.sysjobsteps

这是一个无证件的功能。但是,至少在我的SQL Server(2012)版本中,该函数具有以下定义:

CREATE FUNCTION agent_datetime(@date int, @time int)
RETURNS DATETIME
AS
BEGIN
 RETURN
  (
    CONVERT(DATETIME,
          CONVERT(NVARCHAR(4),@date / 10000) + N'-' + 
          CONVERT(NVARCHAR(2),(@date % 10000)/100)  + N'-' +
          CONVERT(NVARCHAR(2),@date % 100) + N' ' +        
          CONVERT(NVARCHAR(2),@time / 10000) + N':' +        
          CONVERT(NVARCHAR(2),(@time % 10000)/100) + N':' +        
          CONVERT(NVARCHAR(2),@time % 100),
    120)
  )
END

答案 2 :(得分:0)

这是一种丑陋的方式......

declare @table table (last_run_date int, last_run_time int)
insert into @table
values
(20171116,100234),
(20171116,91316)

select 
    cast(cast(cast(last_run_date as varchar) as datetime) + ' ' + stuff(stuff(last_run_time,len(last_run_time) - 1,0,':'),len(stuff(last_run_time,len(last_run_time) - 1,0,':')) - 4,0,':') as datetime)
from @table

答案 3 :(得分:0)

DECLARE @temp TABLE (last_run_date int, last_run_time int)
INSERT INTO @temp VALUES (20171116, 100234)

SELECT convert(datetime,CAST(last_run_date as varchar))
             + Convert(time, Dateadd(SECOND, Right(last_run_time,2)/1
                                           ,Dateadd(MINUTE, Right(last_run_time,4)/100
                                                          ,Dateadd(hour, Right(last_run_time,6)/10000
                                                                       ,'1900-01-01'
                                                                  )
                                                    )
                                     )
                      ) [DateConverted]
  FROM @temp

生成输出:

DateConverted
2017-11-16 10:02:34.000

您可以通过单独执行每个部分来了解其工作原理。

SELECT Dateadd(hour, Right(last_run_time,6)/10000
                   ,'1900-01-01')
FROM @temp

给出小时位置。

SELECT Dateadd(MINUTE, Right(last_run_time,4)/100
                     ,Dateadd(hour, Right(last_run_time,6)/10000
                     ,'1900-01-01'))
FROM @temp

给出小时加分钟位置。