英文:
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 < @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 < @today_date, 'True', 'False') as ComparisonResult;
OR
SELECT IIF(@order_date < @today_date, 1, 0) as ComparisonResult;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论