假設我有下表:
------ ------------------------- -------------------------
| ID | CreatedDate | LastChangedDate |
------ ------------------------- -------------------------
| 3965 | 2019-01-23 03:54:44.903 | 2021-03-12 06:24:45.390 |
------ ------------------------- -------------------------
| 3966 | 2019-01-23 03:55:37.160 | 2021-01-09 04:50:20.697 |
------ ------------------------- -------------------------
| 3967 | 2019-01-23 03:56:21.197 | 2020-05-11 06:10:14.203 |
------ ------------------------- -------------------------
| 3968 | 2019-01-23 03:57:07.943 | 2020-05-11 11:28:26.580 |
------ ------------------------- -------------------------
| 3969 | 2019-01-23 03:58:01.020 | NULL |
------ ------------------------- -------------------------
| 3970 | 2019-01-23 03:58:42.293 | 2021-05-11 09:57:54.553 |
------ ------------------------- -------------------------
| 4143 | 2019-03-19 04:23:08.003 | 2020-12-14 10:08:38.303 |
------ ------------------------- -------------------------
| 4144 | 2019-03-19 04:51:14.533 | 2020-12-14 10:05:11.867 |
------ ------------------------- -------------------------
| 4145 | 2019-03-19 05:16:28.980 | 2019-07-11 07:23:15.803 |
------ ------------------------- -------------------------
| 4146 | 2019-03-19 05:18:49.550 | 2020-01-02 09:12:13.597 |
------ ------------------------- -------------------------
| 4808 | 2019-09-17 05:44:54.587 | 2021-01-09 10:35:20.860 |
------ ------------------------- -------------------------
| 5243 | 2020-01-02 09:07:10.573 | 2021-02-01 16:06:51.770 |
------ ------------------------- -------------------------
| 5666 | 2020-08-12 07:16:20.617 | 2021-01-09 04:52:25.427 |
------ ------------------------- -------------------------
| 5877 | 2020-09-05 05:35:56.160 | 2021-01-09 04:51:43.707 |
------ ------------------------- -------------------------
現在假設我想查看列CreatedDate是否LastChangedDate大于“2021-01-09”。要進行比較,我需要檢查是否LastChangedDate將給NULL定日期與CreatedDate欄位進行比較,否則與LastChangedDate欄位進行比較。
到目前為止我已經嘗試過:
DECLARE @test_date as varchar(20) = '2021-01-09'
SELECT ID, CreatedDate, LastChangedDate
FROM #temptable
WHERE CAST(ISNULL(LastChangedDate,CreatedDate) as date) > CAST(@test_date as date)
但它沒有給出正確的輸出。
我想要的是:
------ ------------------------- -------------------------
| ID | CreatedDate | LastChangedDate |
------ ------------------------- -------------------------
| 3967 | 2019-01-23 03:56:21.197 | 2020-05-11 06:10:14.203 |
------ ------------------------- -------------------------
| 3968 | 2019-01-23 03:57:07.943 | 2020-05-11 11:28:26.580 |
------ ------------------------- -------------------------
| 3969 | 2019-01-23 03:58:01.020 | NULL |
------ ------------------------- -------------------------
| 4143 | 2019-03-19 04:23:08.003 | 2020-12-14 10:08:38.303 |
------ ------------------------- -------------------------
| 4144 | 2019-03-19 04:51:14.533 | 2020-12-14 10:05:11.867 |
------ ------------------------- -------------------------
| 4145 | 2019-03-19 05:16:28.980 | 2019-07-11 07:23:15.803 |
------ ------------------------- -------------------------
| 4146 | 2019-03-19 05:18:49.550 | 2020-01-02 09:12:13.597 |
------ ------------------------- -------------------------
這里還有一個帶有示例資料的 sqlplayground
uj5u.com熱心網友回復:
事實上,你不應該需要任何CASTs:
- 首先將您的變數定義為 a
date而不是字串 - 始終為您存盤的資料使用正確的資料型別。 - 第二次使用邏輯操作(
AND/OR)而不是COALESCE使您的查詢可分析,即能夠使用索引。每當您在子句中的列上使用函式時,WHERE都會冒著阻止使用索引和減慢查詢速度的風險。
DECLARE @test_date as date = '2021-01-09';
SELECT ID, CreatedDate, LastChangedDate
FROM #temptable
WHERE (LastChangedDate IS NOT NULL AND LastChangedDate > @test_date)
OR (LastChangedDate IS NULL AND CreatedDate > @test_date);
uj5u.com熱心網友回復:
我認為您只比較日期。所以嘗試比較日期和時間。
SELECT ID,CreatedDate, LastChangedDate
FROM #temptable WHERE
CAST(ISNULL(LastChangedDate,CreatedDate) as datetime) > CAST(@test_date as datetime)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/454349.html
上一篇:如何為SQL函式設定條件?
