Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert NULL datetime to Blank

I would like to convert a datetime field to varchar so I can show a blank or a message when the date is NULL. This is what I have so far:

select
isnull(us.Date,0) as USDate,
isnull(au.Date,0) as AUDate
from ustable us
left join autable au
on us.column=au.column

Right now this is showing:

USDATE 
2014-10-24 10:29:07.450
1900-01-01 00:00:00.000

I would like to be able to make the "1900-01-01 00:00:00.000" varchar so I can write a message or show a true blank.

like image 207
Emily Avatar asked Oct 09 '14 15:10

Emily


People also ask

How do you handle NULL values in a date column?

If you have a STRING column where its format is like TIMESTAMP , you can simply apply it. Then, DATE will extract just the date and it takes care of the NULL values. Show activity on this post. You can try substr[1] from 1 to 10 to get the date, and then you can use the safe.

How do I replace a NULL date in a string in tableau?

The steps are simple: Change the data type of the Date to a String (right-click on the field in the list of dimensions and Change Data Type) Right-click on the dimension again (once it's a string) and select Aliases. Set the Alias value for Null to be a space if you want it blank (or a “-“/dash/hyphen/minus/etc)

How do I change the default date in SQL Server NULL?

The ISNULL Function is a built-in function to replace nulls with specified replacement values. To use this function, all you need to do is pass the column name in the first parameter and in the second parameter pass the value with which you want to replace the null value.


5 Answers

The simplest line of code I've found for what you're trying to accomplish is as follows.

ISNULL(CONVERT(VARCHAR(30),us.Date,121),'') as USDate,
ISNULL(CONVERT(VARCHAR(30),au.Date,121),'') as AUDate,

I have tested this and verified that it works.

like image 176
Lewis Avatar answered Oct 06 '22 06:10

Lewis


To do it in SQL:

Change your SELECT statement to incorporate a WHEN...THEN clause:

CASE us.Date 
  WHEN '1900-01-01 00:00:00.000' THEN ''
  WHEN NULL THEN ''
  ELSE us.Date
END as USDate,
CASE au.Date 
  WHEN '1900-01-01 00:00:00.000' THEN ''
  WHEN NULL THEN ''
  ELSE au.Date
END as AUDate

If you have front-end code, then you could just implement the functionality on the front-end without changing your SQL code.

like image 27
Alex W Avatar answered Oct 06 '22 05:10

Alex W


Given a column defined like this:

date_of_birth datetime null

You can simply say

select date_of_birth = coalesce( convert(varchar(32),date_of_birth) , '' )
from some_table_with_a_nullable_datetime_column
like image 26
Nicholas Carey Avatar answered Oct 06 '22 06:10

Nicholas Carey


You can use Case AND CONVERT like this -

SELECT
CASE WHEN us.Date IS NULL THEN '' ELSE CONVERT(VARCHAR(30), us.Date, 121) END AS USDate,
CASE WHEN au.Date IS NULL THEN '' ELSE CONVERT(VARCHAR(30), au.Date, 121) END AS AUDate
FROM ustable us
left join autable au
on us.column=au.column
like image 21
Krishnraj Rana Avatar answered Oct 06 '22 04:10

Krishnraj Rana


You can use the NVL() function ...

You can use the NVL function to convert an expression that evaluates to NULL to a value that you specify. The NVL function accepts two arguments: the first argument takes the name of the expression to be evaluated; the second argument specifies the value that the function returns when the first argument evaluates to NULL. If the first argument does not evaluate to NULL, the function returns the value of the first argument.

For example:

select
nvl(us.Date,'') as USDate,
nvl(au.Date,'') as AUDate
from ustable us
left join autable au
on us.column=au.column

Or put anything you want as the Default:

select
nvl(us.Date,'this was a NULL') as USDate,
nvl(au.Date,'this was a NULL') as AUDate
from ustable us
left join autable au
on us.column=au.column
like image 22
kttii Avatar answered Oct 06 '22 04:10

kttii