如何执行日期/月份sql查询 [英] How do I perform date/month sql query
本文介绍了如何执行日期/月份sql查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好。
我想在我的SQL查询中进行选择。
Hi all .
I want to make selection in my sql query .
SELEC DATEADD((CASE WHEN table.date_i = '1' or table.date_i = '4' THEN 'day' ELSE 'month' end as col ), 7 , current_timestamp ) < current_timestamp FROM table
我需要添加7天或7个月取决于 table.datei 值。此查询不起作用。请帮我搞定。
MS SQL SERVER T-SQL
I need to add 7 days or 7 months depend on table.datei values . This query doesn't work . Please help me to make this .
MS SQL SERVER T-SQL
推荐答案
试试:
Try:
SELECT
CASE WHEN table.date_i = '1' or table.date_i = '4' THEN DATEADD( dd, 7 , current_timestamp)
ELSE DATEADD( mm, 7 , current_timestamp)
END AS Col
FROM table
怀疑它是否正是你想要的 - 但你的SQL非常奇怪,所以我可以确切地解决你想要做的事情......
Doubt if it'll be exactly what you want - but your "SQL" is seriously odd, so I can;t work out exactly what you are trying to do...
你需要在使用定义间隔的方式上进行小的修改。一种可能性是使用如下结构:
You need to make small modifications in the way use define the interval. One possibility is to use structure like the following:
DECLARE @intervaltype int;
DECLARE @datetouse date
SET @datetouse = GETDATE()
SET @intervaltype = 1;
--SET @intervaltype = 2;
SELECT CASE @intervaltype
WHEN 1 THEN DATEADD( day, 7, @datetouse)
WHEN 2 THEN DATEADD( month, 7, @datetouse)
END
在实际情况中,您可以使用某些表中的某些列替换该变量。
In the real situation you would replace the variable with some columns from some table.
这篇关于如何执行日期/月份sql查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文