35

I have a table which contains 'NULL' values which are of type 'Datetime'. Now i have to convert those into empty string but when when i use convert function

ISNULL( [Accrued Out of Default] ,'' )

here accrued into default is of datetime type, what it does it changes null to '1900-01-01 00:00:00.000' instead of empty

Then i try to convert them into varchar and apply same

ISNULL(CONVERT(varchar(50),  [Amort Into Default] ),'')

Now I am able to convert into empty string but now those datetime are converted to string which I needed in datetime So I try to CAST, CONVERT but non of them works.

CONVERT(Datetime,'ISNULL(CONVERT(varchar(50),  [Amort Into Default] ),'')',120)

This gives error.

Is there any possible solution to this.

> **Solution Hi someone answered this to do as.
>      ISNULL(CONVERT(varchar(50),  [Amort Into Default] ,120),'') and it works  I dont know why .

**

Kermit
  • 33,827
  • 13
  • 85
  • 121
Ke7in
  • 917
  • 2
  • 7
  • 16
  • 5
    In the result set all values of one field have to be the same data type. So you can't output some values as VARCHAR and other as a DATETIME. You should parse it on client side or output all values as a VARCHAR type. – valex Nov 12 '13 at 13:39
  • 4
    A `datetime` value is *incapable* of storing a *string*. There is no way to simultaneously have a) that the type of the result expression is of type `datetime`, and b) that a row contains something other than a valid datetime value or NULL. – Damien_The_Unbeliever Nov 12 '13 at 13:49

10 Answers10

49

CASE and CAST should work:

CASE WHEN mycol IS NULL THEN '' ELSE CONVERT(varchar(50), mycol, 121) END
Ben
  • 34,935
  • 6
  • 74
  • 113
Bohemian
  • 412,405
  • 93
  • 575
  • 722
13

using an ISNULL is the best way I found of getting round the NULL in dates :

ISNULL(CASE WHEN CONVERT(DATE, YOURDate) = '1900-01-01' THEN '' ELSE CONVERT(CHAR(10), YOURDate, 103) END, '') AS [YOUR Date]
GPH
  • 1,817
  • 3
  • 29
  • 50
  • 2
    this worked really good for me. I had some case statements working out the dates I wanted and just kept the 1900-01-01 date in there into a temp table then just applied this line to each applicable line from the select on the temp table. I only did this because I'm working with only 13 columns; anymore than that I would have done something else - it was just cleaner and more readable. – natur3 Jan 09 '15 at 18:48
  • why invoking CONVERT twice? You need only evaluate IS NULL, like the answer of @Bohemian – Jaime Feb 20 '19 at 18:50
  • Worked great. Just had to be mindful of the date format. – Libertine Makinta Mar 12 '19 at 12:11
  • would be able to use the same logic in an SSIS expression? if so how would it be formulated – HappyCoder123 May 20 '21 at 19:52
7
declare @date datetime; set @date = null
--declare @date datetime; set @date = '2015-01-01'

select coalesce( convert( varchar(10), @date, 103 ), '')
shadow
  • 1,883
  • 1
  • 16
  • 24
3

I had something similar, and here's (an edited) version of what I ended up using successfully:

ISNULL(CONVERT(VARCHAR(50),[column name goes here],[date style goes here] ),'')

Here's why this works: If you select a date which is NULL, it will show return NULL, though it is really stored as 01/01/1900. This is why an ISNULL on the date field, while you're working with any date data type will not treat this as a NULL, as it is technically not being stored as a NULL.

However, once you convert it to a new datatype, it will convert it as a NULL, and at that point, you're ISNULL will work as you expect it to work.

I hope this works out for you as well!

~Eli

Update, nearly one year later:

I had a similar situation, where I needed the output to be of the date data-type, and my aforementioned solution didn't work (it only works if you need it displayed as a date, not be of the date data type.

If you need it to be of the date data-type, there is a way around it, and this is to nest a REPLACE within an ISNULL, the following worked for me:

Select 
    ISNULL(
        REPLACE(
            [DATE COLUMN NAME],
            '1900-01-01',
            ''
        ),
    '') AS [MeaningfulAlias]
Eli
  • 2,538
  • 1
  • 25
  • 36
2

This also works:

REPLACE(ISNULL(CONVERT(DATE, @date), ''), '1900-01-01', '') AS 'Your Date Field'
Ryan Pratt
  • 21
  • 1
2
declare @mydatetime datetime
set @mydatetime = GETDATE() -- comment out for null value
--set @mydatetime = GETDATE()

select 
case when @mydatetime IS NULL THEN ''
else convert(varchar(20),@mydatetime,120)
end as converted_date

In this query, I worked out the result came from current date of the day.

JackLock
  • 1,168
  • 1
  • 13
  • 26
1
select case when IsNull(CONVERT(DATE, StartDate),'')='' then 'NA' else Convert(varchar(10),StartDate,121) end from table1
Si8
  • 9,141
  • 22
  • 109
  • 221
1
Select isnull(date_column_name,cast('1900-01-01' as DATE)) from table name
Abhishek Gurjar
  • 7,426
  • 10
  • 37
  • 45
0

You could try the following

select case when mydatetime IS NULL THEN '' else convert(varchar(20),@mydatetime,120) end as converted_date from sometable

-- Testing it out could do --

declare @mydatetime datetime
set @mydatetime = GETDATE() -- comment out for null value
--set @mydatetime = GETDATE()

select 
case when @mydatetime IS NULL THEN ''
else convert(varchar(20),@mydatetime,120)
end as converted_date

Hope this helps!

ODU90DBA
  • 86
  • 4
0

Try to use the function DECODE

Ex: Decode(MYDATE, NULL, ' ', MYDATE)

If date is NULL then display ' ' (BLANK) else display the date.

Guest
  • 1