如何在 SQL 的单独字段中组合日期和时间

How to Combine dates and time in separate fields in SQL

Table :

schedule_id job_id      next_run_date   next_run_time
------------------------------------------------------
221         D23EA7B2    20151005            90000
222         18EDFB21    20151020            90000
242         90283725    20151001            170000
239         4B69C670    20151011            90000

结果:

schedule_id  job_id      next_run_date_Time 
--------------------------------------------
221         D23EA7B2    2015-10-05 09:00 AM
222         18EDFB21    2015-10-20 09:00 AM
242         90283725    2015-10-01 05:00 PM
239         4B69C670    2015-10-11 09:00 AM

如何将 next_run_datenext_run_time 合并为一个列?

我在 SSRS 2008 中使用的查询

    SELECT c.Name AS ReportName,[LastRunTime],
'Next Run Date' = CASE next_run_date WHEN 0 THEN null ELSE
substring(convert(varchar(15),next_run_date),1,4) + '/' +
substring(convert(varchar(15),next_run_date),5,2) + '/' +
substring(convert(varchar(15),next_run_date),7,2)
END,
--Need to add next_run_date_Time here
FROM 
dbo.[Catalog] c
INNER JOIN dbo.[Subscriptions] S ON c.ItemID = S.Report_OID
INNER JOIN dbo.ReportSchedule R ON S.SubscriptionID = R.SubscriptionID
INNER JOIN msdb.dbo.sysjobs J ON Convert(nvarchar(128),R.ScheduleID) = J.name
INNER JOIN msdb.dbo.sysjobschedules JS ON J.job_id = JS.job_id
ORDER BY S.LastRunTime DESC

这是一种方法:

-- Create sample table and data 
CREATE TABLE tbl (
  next_run_date char(8),
  next_run_time varchar(6)
)

INSERT INTO tbl VALUES
(20151005, 93020),
(20151001, 170000)

如果需要,使用 cte1 用前导零填充 next_run_time, 并使用 cte2 将字符串分解为 "normal" 时间表示:

;with cte1 as 
(
    select next_run_date,
           right('000000'+ next_run_time, 6) as run_time_base
    FROM tbl
), cte2 as
(
    select next_run_date, 
           left(run_time_base, 2) + ':' + 
           substring(run_time_base, 3, 2) + ':' +
           right(run_time_base, 2) as run_time
    from cte1 
)

select cast(next_run_date as datetime) + cast(run_time as datetime) as run_datetime
from cte2

-- clean up
drop table tbl

结果:

run_datetime
-----------------------
2015-10-05 09:30:20.000
2015-10-01 17:00:00.000

我在这里假设 next_run_datenext_run_time,我在这里编写了查询,它将提供您想要的输出..

select 
    schedule_id
   ,job_id 
   ,convert(datetime, 
      convert(varchar, convert(datetime, next_run_date, 112), 111)
      + ' ' + substring(REPLICATE('0',6-LEN(next_run_time)) + next_run_time, 1, 2)
      + ':' + substring(REPLICATE('0',6-LEN(next_run_time)) + next_run_time, 3, 2)
      + ':' + substring(REPLICATE('0',6-LEN(next_run_time)) + next_run_time, 5, 2)
    ) as next_run_date_Time 
 from TableName

这是sql fiddle

我认为最简单的选择是:

dateadd(HH, cast(left(time, len(time) - 4) as int), cast(date as datetime)) 

varchar 日期转换为 datetime 格式,默认为该日期的午夜,然后加上时间指定的小时数。上面的语法假设时间总是在整点——如果不是,则根据需要添加分钟。

进入 datetime 后,您可以使用 convert 指定您喜欢的任何显示格式。

假设两者都是 varchar,试试这个:

SELECT schedule_id, job_id,
       CONVERT(datetime, next_run_date, 112)
       + CONVERT(time,
                 SUBSTRING(next_run_time, 1, LEN(next_run_time) - 4) + ':'
                 + LEFT(RIGHT(next_run_time, 4), 2) + ':'
                 + RIGHT(next_run_time, 2),
               114) AS next_run_date_Time
FROM my_table

这是一个fiddle

如果这些字段是数字,您可以先在子查询中转换它们,然后应用上面相同的查询:

SELECT schedule_id, job_id,
       CONVERT(datetime, next_run_date, 112)
       + CONVERT(time,
                 SUBSTRING(next_run_time, 1, LEN(next_run_time) - 4) + ':'
                 + LEFT(RIGHT(next_run_time, 4), 2) + ':'
                 + RIGHT(next_run_time, 2),
               114) AS next_run_date_Time
FROM (SELECT schedule_id, job_id
           , CAST(next_run_date AS VARCHAR(8)) AS next_run_date
           , CAST(next_run_time AS VARCHAR(6)) AS next_run_time
      FROM my_table) AS t

这是一个fiddle

编辑 您可以更新您的查询以像这样使用此概念:

SELECT c.Name AS ReportName,[LastRunTime],
       CONVERT(datetime, next_run_date, 112)
       + CONVERT(time,
                 SUBSTRING(next_run_time, 1, LEN(next_run_time) - 4) + ':'
                 + LEFT(RIGHT(next_run_time, 4), 2) + ':'
                 + RIGHT(next_run_time, 2),
               114) AS 'Next Run Date'
FROM 
dbo.[Catalog] c
INNER JOIN dbo.[Subscriptions] S ON c.ItemID = S.Report_OID
INNER JOIN dbo.ReportSchedule R ON S.SubscriptionID = R.SubscriptionID
INNER JOIN msdb.dbo.sysjobs J ON Convert(nvarchar(128),R.ScheduleID) = J.name
INNER JOIN (SELECT schedule_id, job_id
                 , CAST(next_run_date AS VARCHAR(8)) AS next_run_date
                 , CAST(next_run_time AS VARCHAR(6)) AS next_run_time
            FROM msdb.dbo.sysjobschedules) AS JS ON J.job_id = JS.job_id
ORDER BY S.LastRunTime DESC