[英]SQL Server : CASE in WHERE clause
I need to create a SQL statement to check where condition if the column is null, it will do other condition. 我需要创建一个SQL语句来检查条件,如果该列为null,它将执行其他条件。 Below is my example: 以下是我的示例:
SELECT
REPLACE(xxShip.Shipment_Date, '/', '-') AS ShipDate,xxShip.Customer,
xxShip.TravelSheetNo
FROM
(SELECT
RANK() OVER (PARTITION BY TravelSheetNo ORDER BY created_on DESC) AS shipwocode,
ShipDate, ShipTime, Shipment_Date,
Customer, Product_no, TravelSheetNo,
[status], ProcessLotNo
FROM
xxDEShipment) xxShip
WHERE
xxShip.shipwocode = 1
AND xxShip.TravelSheetNo = 'ABC'
--here i need to check the condition----------------------------
AND (CASE
WHEN ISNULL(xxShip.Shipment_Date,'') != '' OR xxShip.Shipment_Date != ''
THEN
xxShip.Shipment_Date IS NULL OR xxShip.Shipment_Date = ''
ELSE
CONVERT(DATETIME, xxShip.Shipment_Date) >= @DATESTART and CONVERT(DATETIME, xxShip.Shipment_Date) <= @DATEEND
END)
Please help. 请帮忙。 Thank you 谢谢
This should be something like this: 这应该是这样的:
AND(
(ISNULL(xxShip.Shipment_Date,'') <> '' AND
CONVERT(DATETIME, xxShip.Shipment_Date) >= @DATESTART AND
CONVERT(DATETIME, xxShip.Shipment_Date) <= @DATEEND
)
OR ISNULL(xxShip.Shipment_Date,'') == ''
)
Note that storing dates as strings is a bad practice. 请注意,将日期存储为字符串是一种不好的做法。
If you need to select dates between @DATESTART
and @DATEEND
or null or empty date, use below: 如果您需要选择@DATESTART
和@DATEEND
之间的日期, 或者为空或空日期,请使用以下方法:
ISNULL(CONVERT(DATETIME, NULLIF(xxShip.Shipment_Date, '')), @DATESTART)
BETWEEN @DATESTART AND @DATEEND
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.