1

Have you ever came across this error in SQL Server "Conversion failed when converting date and/or time from character string" my query was converting a null value to datetime and throwing that error. To fix it I had to switch from using convert to cast but I want to know why:

If you do the following:

SELECT CONVERT ( datetime, NULL, 101 )

You get no errors and it returns NULL

but when you use CHAR(0) which is equivalent to NULL

SELECT CONVERT ( datetime, CHAR(0), 101 )

You get an error, is there a difference between NULL or CHAR(0) ?

1
  • 3
    Assertion that char(0) is equivalent to null is false. select ascii(Null) B, ascii(char(0)) one is null one is not. Commented Oct 25, 2017 at 14:56

1 Answer 1

4

A character representing null and a null value are not equivalent.

select char(0) does not return null, it returns the character with ascii code 0.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.