Blank values in Date column returning as 1900/01/01 on running SELECT statement

后端 未结 2 1766
遥遥无期
遥遥无期 2020-12-16 02:52

The column [PAYOFF DATE] has some blank values and some values in mm/dd/yy format.

I have to replace \'/\' with \'-\' and return the date as yyyy-mm-dd. The below qu

相关标签:
2条回答
  • 2020-12-16 03:24

    CASE WHEN [Pay Date] = '' THEN NULL ELSE TRY_CONVERT(DATE, [Pay Date]) END

    0 讨论(0)
  • 2020-12-16 03:26

    You dont need to do the string manipulation as you have shown in your question. If you have dates stored in mm/dd/yyyy format just cast it as DATE.

    SELECT cast(a.[PAYOFF DATE] AS DATE) 
    FROM MTG a 
    

    For 1900-01-01 values, since you are converting from a string data type to Date, String datatype can have Empty strings but Date datatype cannot have empty date values, It can have either a date value or NULL value.

    Therefore you need to convert the empty string to nulls before you convert it to date. 1900-01-01 is just a default value sql server puts in for you because Date datatype cannot have an empty value.

    You can avoid having this sql server default value by doing something like this.

    SELECT cast(NULLIF(a.[PAYOFF DATE],'') AS DATE) 
    FROM MTG a 
    
    0 讨论(0)
提交回复
热议问题