I’ve got a date column where some rows have got NULL values. I would like to use ISNULL or something like that to substitute those values with something like ‘N/A’, however, when I try to use ISNULL, I get an error due to two different data types. If I try to convert my Date column to VARCHAR in order to be able to use ISNULL, then the way my column displays dates gets distorted. Is there any way to solve this problem?
ISNULL(DateSent, 'N/A')
Or, you can make use of the DEFAULT constaints: ... DateTimeColumn DateTime DEFAULT NULL, ... @OziOz If this datetime fild is defined as DateTimeColumn DateTime DEFAULT NULL , just ignore it in the insert columns' list and it will be inserted with NULL value.
"NULL" can be specified as a value in the Date field to get an empty/blank by using INSERT statement. Example: CREATE table test1 (col1 date); INSERT into test1 values (NULL);
SQL Server ISNULL() Function The ISNULL() function returns a specified value if the expression is NULL. If the expression is NOT NULL, this function returns the expression.
I recommend COALESCE()
, because it is standard. However, your problem is that the first column is a date/time and that is not compatible with a string.
So, you need to convert the value. You can use the default format:
COALESCE(CONVERT(VARCHAR(255), DateSent), 'N/A')
Or you can add a conversion argument:
COALESCE(CONVERT(VARCHAR(255), DateSent, 120), 'N/A')
Or you can use FORMAT()
for more flexibility.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With