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.
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.
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)
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.
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.
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.
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
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
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
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