比较两个日期或日期时间变量在Microsoft SQL Server中。

huangapple go评论52阅读模式
英文:

Compare two date or datetime variables in Microsoft SQL Server

问题

I am trying to run this but I get an error:

DECLARE @today_date DATETIME = GETDATE(), 
        @order_date DATETIME = GETDATE();

SELECT @order_date = order_date 
FROM app_orderbook 
WHERE order_no = 1;

SELECT (@order_date < @today_date);

I am getting the following error:

Msg 102, Level 15, State 1, Line 3
Incorrect syntax near '<'.

For completeness' sake, the order_date is a datetime column from tblOrders. I am not able to correct this after searching for guidance and hint.

How do I resolve this? For simplicity, you can even comment out 2nd SQL statement. Still it shows error.

英文:

I am trying to run this but I get an error:

DECLARE @today_date DATETIME = GETDATE(), 
        @order_date DATETIME = GETDATE();

SELECT @order_date = order_date 
FROM app_orderbook 
WHERE order_no = 1;

SELECT (@order_date &lt; @today_date);

I am getting the following error:

> Msg 102, Level 15, State 1, Line 3
> Incorrect syntax near '<'.

For completeness' sake, the order_date is a datetime column from tblOrders. I am not able to correct this after searching for guidance and hint.

How do I resolve this? For simplicity, you can even comment out 2nd SQL statement. Still it shows error.

答案1

得分: 1

以下是翻译好的部分:

following @ThmA advise, it should look like this:

DECLARE @today_date DATETIME = GETDATE(), 
        @order_date DATETIME = GETDATE();

SELECT @order_date = order_date 
FROM app_orderbook 
WHERE order_no = 1;

SELECT IIF(@order_date < @today_date, 'True', 'False') as ComparisonResult;

OR

SELECT IIF(@order_date < @today_date, 1, 0) as ComparisonResult;
英文:

following @ThmA advise, it should look like this:

DECLARE @today_date DATETIME = GETDATE(), 
        @order_date DATETIME = GETDATE();

SELECT @order_date = order_date 
FROM app_orderbook 
WHERE order_no = 1;

SELECT IIF(@order_date &lt; @today_date, &#39;True&#39;, &#39;False&#39;) as ComparisonResult;

OR

SELECT IIF(@order_date &lt; @today_date, 1, 0) as ComparisonResult;

huangapple
  • 本文由 发表于 2023年4月13日 20:56:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76005703.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定