Hi
I haveb below code in query . I want when count(*) = 0 then it should display it as '' but it is displaying 1901-01-01.
SQL
,case when(select count(*) from [PRQS].[dbo].[SentBackLog] where
and ModuleCode = T0.ModuleCode and EntryType = 'Submit') = 0 THEN CAST('' as datetime)
else (select top 1 EntryDate from [PRQS].[dbo].[SentBackLog] where
and ModuleCode = T0.ModuleCode and EntryType = 'Submit') End as 'Last Date'
FROM [EntryLog] T0
where T0.ModuleCode = 2Thanks

Nitin SontakkeRank #264 · 9.6K reputationPosted Nov 11, 2022, 8:28 AM EST
In case when ifTrue then ifFalse end, clause the data type of both ifTrue and ifFalse expression SHOULD be of the SAME data type.
In your case you are casting empty string to datetime, which defaults to 1-1.1901 by SQL Server. You can test this by a simple select statement - select cast('' as datetime).
Null is probably your best choice. Or otherwise make it string and 'No last date' and date as a string in the column.
Hope it clarifies.
Vishal JoshiRank #377 · 6.7K reputationPosted Nov 11, 2022, 6:59 AM EST
Try this
Just Replace CAST('' as datetime) with ''
,case when(select count(*) from [PRQS].[dbo].[SentBackLog] where and ModuleCode = T0.ModuleCode and EntryType = 'Submit') = 0 THEN '' else (select top 1 EntryDate from [PRQS].[dbo].[SentBackLog] where and ModuleCode = T0.ModuleCode and EntryType = 'Submit') End as 'Last Date' FROM [EntryLog] T0 where T0.ModuleCode = 2Amit MohantyRank #112 · 20K reputationPosted Nov 11, 2022, 7:13 AM EST
Check this, it will return as NULL instade of '1900-01-01'
case when (select count(*) from [PRQS].[dbo].[SentBackLog] where ModuleCode = T0.ModuleCode and EntryType = 'Submit') = 0 THEN NULL else (select top 1 EntryDate from [PRQS].[dbo].[SentBackLog] where ModuleCode = T0.ModuleCode and EntryType = 'Submit') end as 'Last Date'Amit MohantyRank #112 · 20K reputationPosted Nov 11, 2022, 5:16 AM EST
try this
case when(select count(*) from [PRQS].[dbo].[SentBackLog] where and ModuleCode = T0.ModuleCode and EntryType = 'Submit') = 0 THEN '' else (select top 1 EntryDate from [PRQS].[dbo].[SentBackLog] where and ModuleCode = T0.ModuleCode and EntryType = 'Submit') End as 'Last Date'Ramco RamcoRank #462 · 5.4K reputationPosted Nov 11, 2022, 5:27 AM EST
same issue
Amit MohantyRank #112 · 20K reputationPosted Nov 11, 2022, 5:40 AM EST
try this
case when (select top 1 EntryDate from [PRQS].[dbo].[SentBackLog] where ModuleCode = T0.ModuleCode and EntryType = 'Submit') IS NULL THEN '' when (select top 1 EntryDate from [PRQS].[dbo].[SentBackLog] where ModuleCode = T0.ModuleCode and EntryType = 'Submit') = '1901-01-01 00:00:00' THEN '' else (select top 1 EntryDate from [PRQS].[dbo].[SentBackLog] where ModuleCode = T0.ModuleCode and EntryType = 'Submit') End as 'Last Date'Ramco RamcoRank #462 · 5.4K reputationPosted Nov 11, 2022, 6:42 AM EST
Hi Amit
still not working. If no record return then what will be the value
Thanks