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

时间:2015-09-29 04:38:29

标签: sql sql-server date ssrs-2008

表:

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

4 个答案:

答案 0 :(得分:1)

这是一种方法:

-- 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将字符串分解为“正常”时间表示:

;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

答案 1 :(得分:1)

假设两者都是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

答案 2 :(得分:0)

我在这里假设next_run_date& next_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

答案 3 :(得分:0)

我认为最简单的选择,假设两者都是varchars开头,是:

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

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

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