日期差异查找器

时间:2016-03-03 08:58:07

标签: sql sql-server sql-server-2012 datediff

我正在尝试在表格中查找today's dategiven date之间的日期差异。有些行有1 date而其他行有2 dates(请提前约会)。我尝试使用动态变量和临时表,但仍无法正常工作

input table

    declare @t nvarchar(1000)
    set @t = ' select a.Availability from [ScrappedData_Regina] a'
    if(len(@t) < = 55  )  -- for single date case
    begin
    SELECT * INTO #result1
     FROM 
     ( 
    select distinct [Product Name],[SKU],DATEDIFF(day,cast(right([Availability],10) as date),cast(GETDATE() as date)) as [Delivery Date] 
    from [ScrappedData_Regina]        
    where [Availability] like '%none%'     -- total 158 products

    )               
    end     
else                        -- for two date case
    begin 
    SELECT * INTO #result2
     FROM 
     ( 

      declare @t date
      set @t = '
      select SUBSTRING(Availability, PATINDEX('% [0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]%', [Availability]), 11) as [Date Part] from [ScrappedData_Regina] '

    -- extracts date part from a column

      select  distinct [Product Name],[SKU],DATEDIFF(day,cast(@t as date),cast(GETDATE() as date)) as [Delivery Date]  from ScrappedData_Regina
    )  -- i want to pass the date part in this select statement

选择*进入结果     from(select * from#result1 union select * from#result2)

select *from result

第一个查询将结果显示为

firstquery result

对于两个日期的情况,我也希望第二次查询的结果相同。

secondquery result

最后将结果合并到一个表

2 个答案:

答案 0 :(得分:1)

    ----------------------------------------------------------------------
--  Calculating Date Difference between today's date and given date -- 
-----------------------------------------------------------------------

---- Total Products : 1505 ----


    IF OBJECT_ID('#RESULT') IS NOT NULL
    DROP TABLE #RESULT
    GO



     SELECT *INTO #RESULT
         FROM
         (

    select  distinct sku,category,subcategory,[Product Name],summary ,DATEDIFF(day,cast(right([Availability],10) as date),cast(GETDATE() as date)) as [Delivery Date] 
    from ScrappedData_Regina       
    where [Availability] like '%none%'  AND DATEDIFF(day,cast(right([Availability],10) as date),cast(GETDATE() as date)) < 0    -- gives only delivery date that has to be delievered in future i.e in minus
                                                                                                                                -- total 74 Products
   union

    select  distinct sku,category,subcategory,[Product Name],summary,'' as [Delivery Date] from ScrappedData_Regina where  summary like '%in stock%'

   union

   select  distinct sku,category,subcategory,[Product Name],summary,'' as [Delivery Date] from ScrappedData_Regina where  summary like '%call for availability%'

       ) a

       select * from #RESULT


 select * into input_scrapregina
 from
 (

 SELECT *, StockInformation = case 
                        when (summary = 'in stock') then 'Any Stock'
                        when (summary = 'call for availability') then 'Call for Availability'
                        when (a.[Delivery Date] < -1 and a.[Delivery Date] >= -30) then 'Out of Stock expected within 30 days'
                        when (a.[Delivery Date] <= -30 and a.[Delivery Date] >= -60) then 'Out of Stock expected within 60 days' 
                        when (a.[Delivery Date] <= 60 and a.[Delivery Date] >= -90) then 'Out of Stock expected within 90 days' 
                        when (a.[Delivery Date] <= 60 and a.[Delivery Date] >= -90) then 'Out of Stock,not expected' 
                        else 'Call for Availability' end from #RESULT a 
)c


---------------------------------
---------  Final Result --------- 
---------------------------------

---- Total Products : 1505 ----

select *from input_scrapregina

答案 1 :(得分:0)

你也可以试试这个:

select substring(dbo.TableName.TableColumn, 15, charindex('/',dbo.TableName.TableColumn) - 1) FROM dbo.TableName

然后

select getdate() - cast(substring(dbo.TableName.TableColumn, 15, charindex('/',dbo.TableName.TableColumn) - 1)as datetime) FROM dbo.TableName

注意:更改(15)数字,以便它可以从您希望它从

开始的索引开始