【问题标题】:Date calculation using Getdate() and patindex使用 Getdate() 和 patindex 计算日期
【发布时间】:2012-07-17 09:42:50
【问题描述】:

希望这是一个简单的

  1. 我想用 getdate() 告诉我今天的日期(完成),
  2. 然后我想从列中取出数字部分 (Don)
  3. 然后我想添加 1 和 2 并生成一个名为 "Date Expires" 的列。例如,如果 aplhanumeric 列称为 CalendarHeaders.Description 我将使用 Patindex 来获取数字部分,但我的问题是如何将 patindex 结果添加到 getdate() 以便我可以获得实际的到期日期?

样本数据

CalendarHeaders.Description 
2 Days from today
5 Days from today
10 Days from today

到目前为止的示例查询

SELECT      
  left(CalendarHeaders.Description, patindex('%[^0-9]%',  CalendarHeaders.Description+'.') - 1) as Expiration, 
  GETDATE()as DateSold 

示例结果(缺少 DateExpires)

Expiration          Datesold                    DateExpires
2                   2012-07-17 04:26:10.283      2012-07-19 04:26:10.283
5                   2012-07-17 04:26:10.283      2012-07-22 04:26:10.283
10                  2012-07-17 04:26:10.283      2012-07-27 04:26:10.283

【问题讨论】:

  • 这是什么SQL方言?最好的方法也是从使 CalendarHeaders.Description 成为数字字段开始

标签: sql sql-server getdate patindex


【解决方案1】:

你可以DATEADD解析的天数到今天的日期;

dateadd(DAY, cast(left(CalendarHeaders.Description, patindex('%[^0-9]%',  CalendarHeaders.Description+'.') - 1) as int), getdate()) AS DateExpires

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-07
    • 1970-01-01
    相关资源
    最近更新 更多