[英]In SQL Where if date then compare dateColumn1 else compare with dateColumn2
Don't really know what could be the best title regards my problem. 我真的不知道最好的标题是什么。 I don't even know if this could be possible.
我什至不知道这是否可能。 I will take every chance I have.
我将抓住一切机会。
Let's go with my problem. 让我们解决我的问题。 i have @dateStart and @dateEnd then I have 2 columns for date.
我有@dateStart和@dateEnd然后我有2列的日期。 let's name it for example dateCol1 and dateCol2 Is it possible to create a condition on
让我们以它命名,例如dateCol1和dateCol2是否可以在
Select * From table Where
case 1 if @dateStart is greater to sample date '1/01/2014' then it would push to the condition 情况1如果@dateStart大于采样日期'1/01/2014',则它将推至条件
dateCol1 between @dateStart and @dateEnd
else if @dateStart is less than that date i could have a condition of 否则,如果@dateStart小于该日期,则我可能有以下条件:
dateCol2 between @dateStart and @dateEnd
I'm Not really good in SQL, so I would be really thankful if someone could help me here. 我不太擅长SQL,因此如果有人可以在这里帮助我,我将非常感谢。 by the way I'm using microsoft SQL
顺便说一句,我正在使用Microsoft SQL
Select * From table Where case when @dateStart > '1/01/2014' then dateCol1 else dateCol2 end
between @dateStart and @dateEnd
try this solution 试试这个解决方案
Use CASE WHEN... ELSE 否则,请使用案例
CASE
WHEN Boolean_expression THEN.
result_expression [ ...n ]
[ ELSE else_result_expression ]
END
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.