NULL check in WHEN statement is not working in SQL Server - sql

I expect 'No Conversion' should be printed for the below code but instead receiving the following error 'Error converting data type varchar to numeric.'
When the TRY_PARSE inside the WHEN statement returns null then the THEN block should execute?
DECLARE #VALUE varchar(20) = 'NA'
SELECT
CASE
WHEN TRY_PARSE(#VALUE AS DECIMAL) IS NULL
THEN 'No Conversion'
ELSE (CONVERT(DECIMAL(10, 3), #VALUE, 0))
END

All branches of a CASE expression need to have the same type. Given that the error message is text, you should also be including the cast decimal value as text, something like this:
SELECT CASE WHEN TRY_PARSE(#VALUE AS DECIMAL) IS NULL
THEN 'No Conversion'
ELSE CONVERT(varchar(max), CONVERT(decimal(10,3), #VALUE, 0)) END

Related

SQL Select Case Conversion failed when converting the varchar value to data type int

With the help of case statement, I would like to get the result as "Non Revenue" in Geodept column where the value is 0
Simple case statement - (gives conversion error)
case when GeoDeptId = 0 then 'Non Revenue' else GeoDeptId end
So tried as below but getting value as 0 again
convert(int, case when IsNumeric(convert(int, rd.GeoDeptId ))= 0 then convert (varchar(14),'Non Revenue') else rd.GeoDeptId
end ),
So tried as below but getting value as 0 again
convert(int, case when IsNumeric(convert(int, rd.GeoDeptId ))= 0 then convert (varchar(14),'Non Revenue') else rd.GeoDeptId
end )
Cast the GeoDeptId to text:
CASE WHEN GeoDeptId = 0 THEN 'Non Revenue' ELSE CAST(GeoDeptId AS varchar(max)) END
The source of your error is that all branches of a CASE expression must have the same type. Since it is not possible for 'Non Revenue' to be cast to an integer, therefore we can convert the GeoDeptId to text.

Why is SQL Server trying to convert my nvarchar(20) datatype to an int?

I'm getting the "conversion" error in a SQL Select query.
The error is:
Msg 248, Level 16, State 1, Line 6
The conversion of the nvarchar value '7000952682' overflowed an int column.
Problem is, there are no int columns in my table!
Here is the table structure:
Here is the query:
If I set the value of #SU to NULL, then it does return all rows as expected. When the value is set to the string value of '7000952682' I get the error.
Why is SQL Server trying to convert the nvarchar value to an int?
All branches of a CASE expression have to have the same type. In this case (no pun intended), it looks like SQL Server is using an integer type and doing an implicit cast of SU to integer. The problem is that the max value for an integer in SQL Server is roughly 2.1 billion, and the example you gave is using the value 7000952682, hence the overflow.
You have two options here. You could make everything varchar:
CASE WHEN #SU IS NULL OR #SU = '' THEN '1' ELSE [SU] END
Or, you could make everything numeric, using a type that won't overflow, e.g.
CASE WHEN #SU IS NULL OR #SU = ''
THEN CAST(1 AS numeric(20, 6))
ELSE CAST([SU] AS numeric(20, 6)) END
As a side note, you could write the first part of your CASE expression more succinctly using COALESCE:
CASE WHEN COALESCE(#SU, '') = '' THEN '1' ELSE [SU] END
Don't use case in the where clause. The logic is more simply and accurately expressed as:
where (#su is null or #su = '' or #su = su)

Conversion failed when converting the nvarchar value 'These are comments for errors' to data type int

I have been struggling to get this corrected for a while now. Below is the part of a query where I am getting the error in the MAX function and I am not able to understand why its trying to convert to data type INT. I would like to keep it as string as this particular column is for comments
MAX(CASE
WHEN ID = -416 AND Value IS NOT NULL
THEN Value
ELSE 'No Error'
END) AS Error_Comments
This particular table is still in test and hence for test purposes there is only one comment added for now which is
These are comments for errors
I also tried converting the case statement but it still gives the same error:
MAX(CONVERT(VARCHAR, CASE
WHEN ID = -416 AND Value IS NOT NULL
THEN Value
ELSE 'No Error'
END)) AS Error_Comments
I also tried this but didn't work: Value <> ''
I couldn't find the answer in any of the stackoverflow questions.
Try this:
declare #t table(ID int, value nvarchar(1000))
insert #t values(-416, 'These are comments for errors'),
(-416, null)
SELECT
COALESCE(MAX(CASE WHEN ID = -416 THEN value END), 'No Error') AS Error_Comments
FROM #t
Result:
These are comments for errors
It would seem that value is an integer. So, your case statement is returning different types, and SQL Server has decided that the type should be an integer. Your string doesn't convert.
So, do an explicit conversion:
MAX(CASE WHEN ID = -416 AND Value IS NOT NULL THEN cast(Value as varchar(255))
ELSE 'No Error'
END) AS Error_Comments
EDIT:
The comment makes a lot of sense. I suppose this should be:
COALESCE(CAST(CASE WHEN ID = -416 AND Value IS NOT NULL THEN VALUE END) as varchar(255)),
'No Error'
)

Error in Converting nvarchar to int

I have one table #DateDay_temp in which there is a column ExtraAdultPrice of type NVARCHAR(20). When I am trying to run this statement in a stored procedure
DECLARE #ExtraAdultPAX DECIMAL(10,2)
SELECT
#ExtraAdultPAX = SUM(CAST((CASE ISNULL(ExtraAdultPrice, '')
WHEN '' THEN 0
ELSE ExtraAdultPrice END) AS DECIMAL(10,2)))
FROM #DateDay_temp;
When I am passing the value 54.56 in the ExtraAdultPrice" column, I get the error
Conversion failed when converting the nvarchar value '54.56' to data
type int.
Help would be appreciated.
Most likely this error happens because of the fact you have a 0 in your CASE statement; therefore, the "type" of the CASE statement is decided to be an INT - which is completely wrong here....
Try this instead:
SELECT
#ExtraAdultPAX = SUM(CAST((CASE ISNULL(ExtraAdultPrice, '')
WHEN '' THEN 0.00 <<== use "0.00" instead of just "0" !!
ELSE ExtraAdultPrice
END) AS DECIMAL(10,2)))
FROM
#DateDay_temp;
Now, everything should be DECIMAL(10,2) and there shouldn't be any attempt to convert your values to an INT
54.56 isn't an int because of the decimal point.

SELECT CASE WHEN TINYINT field condition met return string

I am using SQL Server 2005. I am trying to do a select from a TINYINT field. If the field's value is 0 I want to select an empty string instead ' ' . The fields name is level. HEre is what I am doing:
SELECT
[Level] =
Case
t.[Level]
WHEN 0 THEN ' '
ELSE t.[Level]
END
FROM table t
This code always returns 0. I was trying to troubleshoot the issue and tried this:
SELECT
[Level] =
Case
t.[Level]
WHEN 0 THEN 'test'
ELSE t.[Level]
END
FROM table t
And I got a the error Conversion failed when converting the varchar value 'test' to data type tinyint
So I'm seeing that there is a conversion problem here. I've tried:
SELECT
[Level] =
Case
t.[Level]
WHEN 0 THEN CONVERT(VARCHAR,t.[level])
ELSE t.[Level]
END
FROM table t
But this of course still returns 0, just the character, so it's still not doing what I need. I am thinking that there is most likely a better way to do this but am not sure how to approach it. Could anyone give me some advice on how to handle this? Thanks much!
For CASE statements, all returned values must be of the same type (or automatically convertable); that's the reason why '' was working, but 'test' wasn´t.
SELECT [Level] =
Case t.[Level]
WHEN 0 THEN ' '
ELSE CONVERT(VARCHAR(3), t.[Level])
END
FROM table t
Your attempt to fix this went the wrong way; you needed to convert the else portion to a varchar to match the varchar empty string:
SELECT
[Level] =
Case
t.[Level]
WHEN 0 THEN ''
ELSE CAST(t.[Level] AS VARCHAR(10))
END
FROM table t