简体   繁体   English

nvarchar列在sql中查找上一个日期

[英]nvarchar column to find the previous date in sql

I have followin table schema 我有followin表架构

declare @temp table  
 (
   id int identity(1,1) not null,
   nod nvarchar(50) 
 )

in which nod column have following data 其中nod列具有以下数据

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' 如果nod'N/A' ,则应显示'N/A'

or if there any numeric value like 5,15,31 then it should show getdate()-nod date date column 或是否有任何数字值(例如5,15,31),则应显示getdate()-nod date date列

I have tried following but fail to minus the days and also represent 'N/A' when 'N/A' in that nvarchar column 我曾尝试以下,但未能减去天,也代表'N/A'时, 'N/A'在nvarchar列

 select  DATEADD(dd,case nod when 'N/A' then 0 else nod end,GETDATE()) from @temp

sql fiddle is here sql小提琴在这里

The following query would return a VARCHAR(50) column that contains either 'N/A' or now - nod date as varchar. 以下查询将返回VARCHAR(50)列,其中包含“ N / A”或now - nod日期为varchar。

SELECT
     CASE nod
         WHEN 'N/A' THEN 'N/A'
         ELSE CONVERT(VARCHAR(50), 
                      DATEADD(dd, 
                              -1 * CONVERT(INT, nod), 
                              GETDATE()
                             )
                     ) 
     END
FROM @temp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM