将int格式转换为日期并在SQL Server中查找差异

时间:2019-03-03 12:34:52

标签: sql sql-server

我有一个包含以下各列的表;我的主要要求是实现{enddate}和{startdate}之间的差异。

我的日期以int的形式保存在数据库中,而我的日期是波斯的日期,我需要找到一种方法来用日期值填充{Experiment_Year},{Experiment_Month}和{Experiment_Day}列,实际上我需要一些东西类似于datediff函数。

我已经尝试将年月日和年月日拆分,但是由于蛾子不起作用而失败 子字符串也不起作用。有人知道吗?

Start_Date  End_Date    Experiment_Year Experiment_Month Experiment_Day
----------- ----------- --------------- ---------------- --------------
890101      891001      0               0                0
890101      891001      0               0                0
891001      891101      0               0                0
891101      900101      0               0                0
900101      910101      0               0                0
900101      910101      0               0                0
910101      910114      0               0                0
910114      920101      0               0                0
920101      920601      0               0                0
920101      920601      0               0                0

我的失败代码:

SELECT 
    CAST(CAST(LEFT(CONVERT(VARCHAR(10), se.endDate), 2) AS INT) - 
       CAST(LEFT(CONVERT(VARCHAR(10), se.startdate), 2) AS INT) AS NVARCHAR),    
    CAST(DATEDIFF(mm, DATEADD(yy, DATEDIFF(yy, se.startdate, se.enddate), se.startdate), se.enddate) AS VARCHAR(2)),
    CAST(DATEDIFF(dd, DATEADD(mm, DATEDIFF(mm, DATEADD(yy, DATEDIFF(yy, se.startdate, se.enddate), se.startdate), se.enddate), DATEADD(yy, DATEDIFF(yy, se.startdate, se.enddate), se.startdate)),se.enddate) AS VARCHAR(2)) AS result
FROM 
    (SELECT 
         startdate, enddate 
     FROM
         cnv_hr_Duty_His_sequence 
     WHERE
         EmpID = 16017) se

CASE
   WHEN se.endDate IS NULL
      THEN NULL
      ELSE CAST(CAST(LEFT(CONVERT(VARCHAR(10),se.endDate), 2) as int) - cast( LEFT(CONVERT(VARCHAR(10),se.startdate), 2) as int) as nvarchar)
                --CAST(DATEDIFF(yy, convert(datetime,se.startdate), convert(datetime,se.enddate)) AS varchar(4))
                --CAST(('13' + SUBSTRING(CAST(se.endDate AS NVARCHAR), 1, 2)) AS INT) - CAST(('13' + SUBSTRING(CAST(se.startdate AS NVARCHAR), 1, 2)) AS INT)
            END Experiment_Year,

                    cast(cast(right(LEFT(CONVERT(VARCHAR(10),se.endDate), 4),2) as int) - cast( right(LEFT(CONVERT(VARCHAR(10),se.startdate), 4),2) as int) as nvarchar)

            Experiment_Month,
            CASE
                WHEN se.endDate IS NULL
                THEN NULL
                ELSE cast(cast(RIGHT(CONVERT(VARCHAR(10),se.endDate), 2) as int) - cast( RIGHT(CONVERT(VARCHAR(10),se.startdate), 2) as int) as nvarchar)
            END Experiment_Day, 

我的愿望结果:

Start_Date  End_Date    Experiment_Year                Experiment_Month               Experiment_Day
    ----------- ----------- ------------------------------ ------------------------------ ------------------------------
    890101      891001      0                              9                              0
    890101      891001      0                              9                              0
    891001      891101      0                              1                              0
    891101      900101      0                              1                            0
    900101      910101      1                              0                              0
    900101      910101      1                              0                              0
    910101      910114      0                              0                              13
    910114      920101      0                              11                              16
    920101      920601      0                              5                              0
    920101      920601      0                              5                              0

0 个答案:

没有答案