2017-11-16 1 views
0

그래서이 테이블에는 int 형식의 날짜 열이 있습니다.int 20171116101856을 yyyymmddhhmmss 형식으로 변환하여 datediff 함수로 사용합니다.

last_run_date | last_run_time 
    20171116 | 100234 

이 두 값을 datetime으로 변환하여 datediff 문에 사용하려고합니다.

이 내 선언문 last_run_time 값이 두 자리 시간 형식 101216에 있지만 경우에만이 있지만 작동

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 

의 한 자리 시간의 91316는 다음과 같은 오류와 함께 실패 할 때마다,이다

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

저는 SQL Server 2005를 사용 중입니다.

+2

내가 대답을 구축하기 시작 유지하고, 왜 테이블이 같은 사람 건축가 지구는 것에 이후 자신에게 물어을 .. ... – pimbrouwers

+1

음 .... 그 숫자는 정수에 비해 너무 큽니다. 당신은 무엇을 기대합니까? 나는 이런 식으로 날짜와 시간을 저장하는 팬이 아니다. 이것이 우리가 datetime 데이터 유형을 갖는 이유입니다. –

+2

이 뷰는 msdb.dbo.sysjobsteps의 데이터를 가져오고이 뷰는 여기에서 사용되는 형식입니다. 나는 그것에 대해 할 수있는 일이 없습니다. – Markov

답변

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 서버 (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 
+0

순전히 감사한다 나는이 기능에 관하여 전혀 몰랐고 나가 찾아 낼 때 나는 google에 그것을 찾아 내지 않았다 놀랜다. 변환 방법. – Markov

+0

@ MarkVoznyuk 문서화되지 않은 기능이지만, 아무런 문제가 없었습니다. –

+0

이 정보를 주셔서 감사합니다. 카스는 앞으로 몇 주 안에이 날짜 형식으로 작업하게 될 것입니다. – Markov

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 | 
+---------------+---------------+-------------------------+ 
+0

와우, 고마워. 내 datediff 함수에서 convert 문을 사용하면 훌륭합니다. – Markov

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 
+0

이것도 잘 작동합니다. 정말 고마워요. – Markov

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 

는 시간 플러스 분 위치를 제공합니다.


등은

+0

당신은 왜 우리가 '1900-01-01'을 추가하는지 설명 할 수 있겠습니까 – Markov

+0

그냥 해냈고 1900-01-01을 보여주었습니다. 결코 알지 못했습니다, 감사합니다. – Markov

+0

거기에 지나치게 많이 생성되었을 수도 있습니다 ... 시간 위치, 분 위치, 초 위치를 계산합니다. 알고리즘의 각 부분을 개별적으로 수행해보십시오. 나는 명확성을 위해 문제를 다시 편집 할 것이다. – Zorkolot

관련 문제