我尝试case when
使用null
并not null
设置为值ErrorCode
字段。
前任。
EmpNo|ChkDate |ChkIn |ChkOut |ErrorCode
00001|2012-10-01 00:00:00.000|2012-10-01 07:21:00.000|2012-10-01 17:05:00.000|0
00002|2012-10-01 00:00:00.000|2012-10-01 22:17:00.000|2012-10-01 00:00:00.000|6
00003|2012-10-01 00:00:00.000|2012-10-01 00:00:00.000|2012-10-01 19:30:00.000|6
00004|2012-10-01 00:00:00.000|NULL |NULL |7
00005|2012-10-01 00:00:00.000|2012-10-01 07:10:00.000|2012-10-01 12:00:00.000|0
00006|2012-10-01 00:00:00.000|2012-10-01 13:50:00.000|2012-10-01 19:20:00.000|0
但我需要输出(错误代码)
EmpNo|ChkDate |ChkIn |ChkOut |ErrorCode
00001|2012-10-01 00:00:00.000|2012-10-01 07:21:00.000|2012-10-01 17:05:00.000|0
00002|2012-10-01 00:00:00.000|2012-10-01 22:17:00.000|2012-10-01 00:00:00.000|6
00003|2012-10-01 00:00:00.000|2012-10-01 00:00:00.000|2012-10-01 19:30:00.000|6
00004|2012-10-01 00:00:00.000|NULL |NULL |7
00005|2012-10-01 00:00:00.000|2012-10-01 07:10:00.000|2012-10-01 12:00:00.000|8
00006|2012-10-01 00:00:00.000|2012-10-01 13:50:00.000|2012-10-01 19:20:00.000|8
ChkIn 和 ChkOut 中的值是半天。我需要设置值 = 8。但我尝试 = 0。
此代码:
SELECT
tf.EmpNo, tf.ChkDate, tf.ChkIn, tf.ChkOut,
CASE
WHEN ChkIn is not null and Convert(nvarchar(10), ChkOut,108) != '00:00:00'
THEN 0
WHEN ChkIn is not null and Convert(nvarchar(10) ,ChkOut,108) = '00:00:00'
THEN 6
WHEN Convert(nvarchar(10),ChkIn,108) = '00:00:00' and ChkOut is not null
THEN 6
WHEN Convert(nvarchar(10),ChkOut,108) <= '12:00:00'
OR Convert(nvarchar(10),ChkOut,108) >= '12:00:01' Then 8
WHEN ChkIn is null and ChkOut is null THEN 7
END as 'ErrorCode'
FROM filesTA tf
WHERE tf.ChkDate = '2012-10-01'