【问题标题】:nvarchar column to find the previous date in sqlnvarchar 列在 sql 中查找上一个日期
【发布时间】:2012-10-10 18:31:20
【问题描述】:

我有以下表架构

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

其中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'

我希望选择语句在以下基础上给我结果

如果nod'N/A' 那么它应该显示'N/A'

或者如果有像 5,15,31 这样的数值,那么它应该显示 getdate()-nod date 日期列

我已尝试关注但未能减去天数,并且当 'N/A' 在该 nvarchar 列中时也代表 'N/A'

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

sql fiddle is here

【问题讨论】:

    标签: sql getdate


    【解决方案1】:

    以下查询将返回一个 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
    

    【讨论】:

      猜你喜欢
      • 2016-04-05
      • 1970-01-01
      • 2017-06-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-07
      • 1970-01-01
      • 2021-01-27
      • 2014-03-12
      相关资源
      最近更新 更多