SQL date 1900-01-01 appearing out of nowhere
So I got assigned this bug where date 1900-01-01 was displayed on the screen so, as I am lazy, I started to look into the code without reproducing the issue. The SQL stored procedure looked fine, it was returning:
SELECT
CASE SpecialCase=1 THEN ''
ELSE SomeDate
END as DateFilteredBySpecialCase
Then the value was being passed around through various application layers, but it wasn't transformed into anything, then it was displayed. So where did this magical value come from? I was expecting some kind of ISNULL(SomeDate,'1900-01-01') or some change in the mapping code or maybe SomeDate was 1900-01-01 in some records, but I couldn't find anything like that.
Well, at second glance, the selected column has to have a returning type, so what is it? The Microsoft documentation explains:
Returns the highest precedence type from the set of types in result_expressions and the optional else_result_expression. For more information, see Data Type Precedence.
If you follow that link you will see that strings are at the very bottom, while dates are close to the top. In other words, a CASE statement that returns strings and dates will always have the return type a date!
SELECT CAST('' as DATETIME) -- selects 1900-01-01
Just a quickie. Hope it helps.
Comments
Be the first to post a comment