nvarchar column to find the previous date in sql
Question
I have followin table schema
declare @temp table
(
id int identity(1,1) not null,
nod nvarchar(50)
)
in which nod
column have following data
insert into @temp select 'N/A'
insert into @temp select 'N/A'
insert into @temp select '5'
insert into @temp select 'N/A'
insert into @temp select '7'
insert into @temp select 'N/A'
insert into @temp select '31'
insert into @temp select '15'
i want that select stament shoud give me result on following basis
if nod
value 'N/A'
then it should show 'N/A'
or if there any numeric value like 5,15,31 then it should show getdate()-nod date
date column
I have tried following but fail to minus the days and also represent 'N/A'
when 'N/A'
in that nvarchar column
select DATEADD(dd,case nod when 'N/A' then 0 else nod end,GETDATE()) from @temp
Solution
The following query would return a VARCHAR(50)
column that contains either 'N/A' or now - nod
date as varchar.
SELECT
CASE nod
WHEN 'N/A' THEN 'N/A'
ELSE CONVERT(VARCHAR(50),
DATEADD(dd,
-1 * CONVERT(INT, nod),
GETDATE()
)
)
END
FROM @temp
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow