Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ISNULL on date column

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')
like image 892
Redraidas Avatar asked Mar 19 '19 11:03

Redraidas


People also ask

How do I handle a NULL in a date column in SQL?

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.

How do I insert a NULL in a date column in SQL?

"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);

How do I use Isnull in SQL?

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.


1 Answers

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.

like image 75
Gordon Linoff Avatar answered Oct 19 '22 14:10

Gordon Linoff