Conversion failed when converting datetime from binary/varbinary string

Error Message:
Msg 210, Level 16, State 1, Line 3
Conversion failed when converting datetime from binary/varbinary string.

Severity level:
16.

Description:
This error message appears when you try to convert an invalid BINARY(VARBINARY) string to a value of type DATETIME.

Consequences:
The T-SQL statement can be parsed, but causes the error at runtime.

Resolution:
Error of the Severity Level 16 are generated by the user and can be fixed by the SQL Server user. The (VAR)BINARY string must either be valid and convertible to DATETIME.

Versions:
All versions of SQL Server

Example(s):
DECLARE @bin BINARY(8)
SELECT @bin = CAST(GETDATE() AS BINARY(8))
SELECT CAST(@bin + CAST(1 AS BINARY(1)) AS DATETIME)
–or
SELECT CAST(0x002D248F00000000 AS DATETIME)

Remarks:
In the first example above the conversion to DATETIME is illegal because of the addition of two binary values.

In the second example is the binary string as such valid, but the resulting DATETIME value exceeds the upper limit of the DATETIME data type. This raises the error.

]]>

Leave a comment

Your email address will not be published.