When I run the following SQL at my local SQL Server instance:
DECLARE @t DATETIME2 = '2019-12-12 00:00:00'
I get the following error:
Unable to convert @t to a System.Data.SqlClient.SqlParameter object. The specified literal cannot be converted to DateTime2(System.Data.SqlDbType), as it used an unsupported date/time format. Use one of the supported date/time formats. Literal value: 2019-12-12 00:00:00
The only way to fix it is to add 'T' between date and time
DECLARE @t DATETIME2 = '2019-12-12T00:00:00'
At the same time, this script runs perfectly fine at my production server (without adding 'T').
I tried to find the difference and don't see anything that could affect the behavior:
SQL_Latin1_General_CP1_CI_AS
Can somebody explain, why I'm getting the error locally?
This is a client-side error so it seems you are running the query from SSMS with the Enable Always Encrypted (column encryption) option for the connection (connection Options-->Always Encrypted) on the remote client but not the prod server. With the option enabled, the SSMS client parses literals in order to build parameters for AE.
Unlike '2019-12-12 00:00:00'
, the ISO 8601 datetime format literal '2019-12-12T00:00:00'
is unambiguous and can be reliably parsed regardless of the client regional settings.
So your options are to either use an ISO 8601 datetime format or turn of the SSMS Enable AE option for the connection.