簡體   English   中英

如何在SQL中的單獨字段中組合日期和時間

[英]How to Combine dates and time in separate fields in SQL

表:

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將字符串拆分為“正常”時間表示形式:

;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

假設兩個都是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

這是一個小提琴

如果這些字段是數字,則可以先在子查詢中將其轉換,然后在上面應用相同的查詢:

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

這是一個小提琴

編輯您可以更新查詢以使用如下概念:

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

我在這里假設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小提琴

我認為最簡單的選擇是:假設兩者都是varchars

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

轉換varchar日期的datetime格式,將默認為午夜的日期,再加入由時間指定的小時數。 上面的語法假定時間總是在小時上-如果不是,請根據需要添加分鍾。

datetime ,您可以使用convert指定所需的任何顯示格式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM