日期差异查找器
Date Difference Finder
我正在尝试查找 table.Some 行中 today's date
和 given date
之间的日期差异具有 1 date
而其他行具有 2 dates
(取较早的日期).我尝试使用动态变量和临时变量 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
select *进入结果
来自 ( select * 来自 #result1 union select * 来自 #result2 )
select *from result
第一个查询的结果为
对于两个日期的情况,我也希望第二次查询的结果相同。
最后将两个结果合并为 table
----------------------------------------------------------------------
-- 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
你也可以试试这个:
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) 编号,以便它可以从您希望它开始的索引开始
我正在尝试查找 table.Some 行中 today's date
和 given date
之间的日期差异具有 1 date
而其他行具有 2 dates
(取较早的日期).我尝试使用动态变量和临时变量 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
select *进入结果 来自 ( select * 来自 #result1 union select * 来自 #result2 )
select *from result
第一个查询的结果为
对于两个日期的情况,我也希望第二次查询的结果相同。
最后将两个结果合并为 table
----------------------------------------------------------------------
-- 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
你也可以试试这个:
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) 编号,以便它可以从您希望它开始的索引开始