下面的答案是計數我對你的要求的解釋,你的問題有點不清楚。
SELECT
ID,
EMPLOYEE,
[ROLE],
WorkType1,
CASE WHEN Date1 IS NULL THEN WorkType1 ELSE CAST(Date1 AS VARCHAR(23)) END Date1,
WorkType2,
CASE WHEN Date2 IS NULL THEN WorkType2 ELSE CAST(Date2 AS VARCHAR(23)) END Date2,
WorkType3,
CASE WHEN Date3 IS NULL THEN WorkType3 ELSE CAST(Date3 AS VARCHAR(23)) END Date3
WorkType4,
CASE WHEN Date4 IS NULL THEN WorkType4 ELSE CAST(Date4 AS VARCHAR(23)) END Date4
日期的顯式轉換爲VARCHAR(23)當你用日期值這是不兼容的數據類型我選擇VARCHAR(23)混合Worktype允許日期時間
根據需要在您的數據庫設置中,您可能需要額外拆分日期以在輸出中保留您期望的格式,下面給出了yyyy-MM-dd HH:mm:ss。
CAST(Datepart(Year,Date1) AS VARCHAR(4)) + '-'+ RIGHT('00'+CAST(Datepart(Month,Date1) AS VARCHAR(2)),2) + '-' + RIGHT('00' + CAST(Datepart(Day,Date1) AS VARCHAR(2)),2) + ' ' + RIGHT('00' + CAST(Datepart(HH,Date1) AS VARCHAR(2)),2)+ ':' + RIGHT('00'+CAST(Datepart(MINUTE,Date1) AS VARCHAR(2)),2) + ':' + RIGHT('00'+CAST(Datepart(ss,Date) AS VARCHAR(2)),2)
我用下面的測試
DECLARE @Date1 DATEtime = '2017-01-01', @Date2 DATEtime = NULL, @Date3 Datetime = '2017-01-02'
,@Worktype1 Varchar(20) = 'Test1'
,@Worktype2 Varchar(20) = 'Test2'
,@Worktype3 Varchar(20) = 'Test3'
SELECT
CASE WHEN @Date1 IS NULL THEN @WorkType1 ELSE CAST(@Date1 AS VARCHAR(25)) END Date1, /*Varchar 23 date*/
CASE WHEN @Date2 IS NULL THEN @WorkType2 ELSE CAST(@Date2 AS VARCHAR(25)) END Date2, /* Result of Worktype2 due to null */
CASE WHEN @Date3 IS NULL THEN @WorkType3 ELSE CAST(Datepart(Year,@Date3) AS VARCHAR(4)) + '-'+ RIGHT('00'+CAST(Datepart(Month,@Date3) AS VARCHAR(2)),2) + '-' + RIGHT('00' + CAST(Datepart(Day,@Date3) AS VARCHAR(2)),2) + ' ' + RIGHT('00' + CAST(Datepart(HH,@Date3) AS VARCHAR(2)),2)+ ':' + RIGHT('00'+CAST(Datepart(MINUTE,@Date3) AS VARCHAR(2)),2) + ':' + RIGHT('00'+CAST(Datepart(ss,@Date3) AS VARCHAR(2)),2) END Date3 /* Result for Date 3 formatted to yyyy-MM-dd HH:mm:ss*/
其提供了以下的結果集
[結果集] [1]:https://i.stack.imgur.com/Y1uUp.jpg
它返回空值或空白? – McNets
只需使用'IS NULL'或'IS NOT NULL'。如果一個字段包含一個空字符串,它*會*有一個值。如果你想檢查空或空字符串,使用'myField爲空或myfield =''' –
'null'在SQL中意味着'未知'。你可以給空或空白不管這意味着你要 –