Change NULL values in Datetime format to empty string

前端 未结 10 1487
鱼传尺愫
鱼传尺愫 2021-01-07 16:50

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



        
相关标签:
10条回答
  • 2021-01-07 17:16
    declare @date datetime; set @date = null
    --declare @date datetime; set @date = '2015-01-01'
    
    select coalesce( convert( varchar(10), @date, 103 ), '')
    
    0 讨论(0)
  • 2021-01-07 17:17

    This also works:

    REPLACE(ISNULL(CONVERT(DATE, @date), ''), '1900-01-01', '') AS 'Your Date Field'
    
    0 讨论(0)
  • 2021-01-07 17:18
    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.

    0 讨论(0)
  • 2021-01-07 17:19

    Try to use the function DECODE

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

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

    0 讨论(0)
  • 2021-01-07 17:26
    select case when IsNull(CONVERT(DATE, StartDate),'')='' then 'NA' else Convert(varchar(10),StartDate,121) end from table1
    
    0 讨论(0)
  • 2021-01-07 17:28

    CASE and CAST should work:

    CASE WHEN mycol IS NULL THEN '' ELSE CONVERT(varchar(50), mycol, 121) END
    
    0 讨论(0)
提交回复
热议问题