I have the following tables with column/row values:
tbl1
ID
1
2
3
tbl2
ID Color DateRegister
1 'Red' '2021-02-04 00:00:00.000'
3 'Blue' '2021-02-04 00:00:00.000'
I'm trying to write a SQL code that selects records from tbl1 with tbl2 via LEFT JOIN. If there is no connected records between tbl1 and tbl2, the tbl2 columns should be NULL. I use CASE to display blank strings for the NULL values. This does not work for the date column.
SELECT
tbl1.ID
, CASE WHEN tbl2.Color IS NULL THEN '' ELSE tbl2.Color END AS 'Batch-Color'
, tbl2.DateRegister as 'Date-1'
, CASE WHEN tbl2.DateRegister IS NULL THEN '' ELSE WHEN tbl2.DateRegister END AS 'Batch-Date'
FROM tbl1 LEFT JOIN tbl2 ON tbl1.ID = tbl2.ID
Here is the output. I need the date to display as a blank string instead of '1900-01-01'
ID Batch-Color Date-1 Batch-Date
1 'Red' '2021-05-28 00:00:00.000' '2021-05-28 00:00:00.000'
2 '' NULL '1900-01-01 00:00:00.000'
3 'Blue' '2021-02-04 00:00:00.000' '2021-02-04 00:00:00.000'
NULLvalues, and let your client code format it as what it wants. If you convert to blanks, you also have to decide what format to use to convert the dates that are notNULL-- relying on what the server implicitly produces based on regional settings is a bad idea.''is by definition not a date and you can't store or resolve non-date values in a date and time column in SQL; a column must be a single strongly typed data type. Leave it to your presentation layer to displayNULLas a blank value, not the RDBMS.