TSQL NVARCHAR conversion error inside CASE statement

This choice is driving me crazy.
Error:

Conversion error converting nvarchar '17 .30 h 'value to int data type.

Data:

(DateTime) (Nvarchar) (DateTime) DATAINICI DATAMANUAL DATAFI null 17.30 h 10/01/2015 01/01/2015 20.30 h null 

And the statement:

 CASE WHEN dbo.Activitat.DataInici is null THEN DATEPART(DAY,Activitat.Datafi) ELSE CONVERT(NVARCHAR(50), dbo.Activitat.DataManual) END 
+6
source share
1 answer

You get this error due to an implicit conversion. One part of CASE returns NVARCHAR(50) ie CONVERT(NVARCHAR(50), dbo.Activitat.DataManual) , which cannot be converted to int , and the other returns int ie DATEPART(DAY,Activitat.Datafi) .

Something like this will also return the same error.

 SELECT CASE WHEN 1=2 THEN 1 ELSE 'errorstring' END 

You must CONVERT and return NVARCHAR(50) in both cases so that there is no implicit conversion. Something like that.

 CASE WHEN dbo.Activitat.DataInici is null THEN CONVERT(NVARCHAR(50),DATEPART(DAY,Activitat.Datafi)) ELSE CONVERT(NVARCHAR(50), dbo.Activitat.DataManual) END 
+7
source

Source: https://habr.com/ru/post/988003/


All Articles