Using SUM in CASE if value is numeric - sql

I have a similar situation to the CASE clause well known problem:
DECLARE #i INT = 1;
SELECT CASE WHEN #i = 1 THEN 1 ELSE MIN(1/0) END;
In that case the code will throw an exception, Divide by zero error encountered, even though in theory you would never reach that min(1/0) scenario.
So I have similar situation:
CASE WHEN CodeValue in ('Numeric1','Numeric2') THEN SUM(cast(VarcharValue as int)) ELSE max(VarcharValue) END
In other words I need to use SUM function if VarcharValue is numeric (it could be determined by CodeValue if it is numeric), if that's not the case, I need return VarcharValue with MAX function.
Any suggestions?

You have a strange construct, because one case branch returns a string and another a number. SQL Server decides, in such a case, that the expression returns a number.
And you'll have problems with the ELSE, because a non-numeric string will be converted to a number. Error.
The following might work:
(CASE WHEN CodeValue in ('Numeric1', 'Numeric2')
THEN CAST(VARCHAR(MAX), SUM(CAST(VarcharValue as int)))
ELSE MAX(VarcharValue)
END)
You might still have a problem, if the SUM() is being calculated over all data before filtering (which I think is possible with an aggregation query). My recommendation is to upgrade to a supported version of SQL Server and use:
(CASE WHEN CodeValue in ('Numeric1', 'Numeric2')
THEN CAST(VARCHAR(MAX), SUM(TRY_CAST(VarcharValue as int)))
ELSE MAX(VarcharValue)
END)
However, you can repeat the CASE logic:
(CASE WHEN CodeValue in ('Numeric1', 'Numeric2')
THEN CAST(VARCHAR(MAX), SUM(CASE WHEN CodeValue in ('Numeric1', 'Numeric2') THEN CAST(VarcharValue as int) END))
ELSE MAX(VarcharValue)
END)

Related

Does the SQL'%' wild card character capture null values?

Recently I've come across a problem with a query that isn't returning everything that it's expected to return. Part of the query which selects by a condition is as follows:
AND field LIKE
CASE WHEN #val = 1 THEN
'%'
ELSE
'N'
END
Now, when #val is 1, I'd expect this piece of code to essentially do nothing, as in the condition to basically accept any value what so ever, including null values.
Am I wrong about this? And if so, does anyone have a solution? I was thinking something along the lines of
AND field LIKE
CASE WHEN #val = 1 THEN
'%' OR ISNULL(field)
ELSE
'N'
END
However SQL doesn't work like that, but that's basically what I wish to accomplish.
Thanks all, and sorry if this is a duplicate, I couldn't find an answer.
Based on what you're trying to accomplish, it seems your query could be optimized to this:
AND (#val = 1 OR field = 'N')
There doesn't seem to be a reason for the LIKE.
UPDATE
Since you are trying to understand the behavior of LIKE and CASE moreso than working with existing queries, here are some variations of the accepted answer.
To use CASE within the LIKE, you have to use something like COALESCE to handle the null case as well.
COALESCE(Field, '') LIKE (CASE WHEN #val = 1 THEN '%' ELSE 'N' END)
Otherwise, you can use the LIKE within the CASE (like accepted answer), but probably personal preference that this seems easier to read:
1 = (CASE WHEN #val = 1 OR Field LIKE 'N' THEN 1 ELSE 0 END)
field LIKE '%' does not match null. CASE expressions must return a single type of result, I like int in most of mine.
AND 1 = CASE
WHEN #val = 1 THEN 1
WHEN field like 'N' THEN 1
ELSE 0
END
Try this (assuming that field is varchar or nvarchar) -
AND ISNULL(field,'') LIKE
CASE WHEN #val = 1 THEN
'%'
ELSE
'N'
END

SQL query returning error only when CASE WHEN is used

I have the sql code below and the case when statement is always returning the 'Error converting data type varchar to numeric'. But if I comment it out then the statement runs successfully even though the same code is used in the first column.
SELECT CAST(DATA1 AS DECIMAL(10,5)), AMOUNT, CASE WHEN NAME ='A' THEN CAST(DATA1 AS DECIMAL(10,5)) ELSE '0' END FROM TEST
If I run as is above, it fails. If I comment out the Case when piece then it runs successfully. Thanks!
Your CASE statement is trying to return two different types, DECIMAL(10,5) and varchar.
It needs to return one type, so instead of having '0' just use 0.0.
You are telling it to display two different data types in the same column. Try;
SELECT CAST(DATA1 AS DECIMAL(10,5)), AMOUNT,
CASE
WHEN NAME LIKE 'A' THEN CAST(DATA1 AS DECIMAL(10,5))
ELSE CAST(0.0 AS DECIMAL(10,5)) END FROM TEST

SQL Server: interpreting 'y' as BIT value

In C, when you compared true/false value to 1/0, it worked very well.
I would want the similar possibility with SQL Server - when I have a bit column, I would like to compare myBitField = 'y' / myBitField = 'n'
Is there anything I can do about that? Maybe change some SQL interpreter settings or something?
Example of what I would like to do:
select * from
(
select CAST(1 AS BIT) as result
) as main
where main.result = 'y'
Currently, it throws an error, and I would like it to return 1/true/'y', whatever, but I would like it to be able to make that comparison.
I suppose you want to do it for some yes/no thing. But this is generally a wrong concept, your application which is accessing the SQL Server should interpret y as a 1 and n as a 0 and afterwards set the correct parameters for the query. You should not (actually I'm temped to write "must not") do this in SQL Server, that's what you have a business logic for.
As others have said, BIT and CHAR / VARCHAR are entirely different datatypes. But if you want to cast them during the select, you can use CASE expression like so:
-- Reading string as BIT
SELECT CAST(CASE RESULT WHEN 'Y' THEN 1 WHEN 'N' THEN 0 ELSE NULL END AS BIT) RESULT
-- Reading BIT as string
SELECT CAST(CASE RESULT WHEN 1 THEN 'Y' WHEN 0 THEN 'N' ELSE NULL END AS CHAR(1)) RESULT
And that's about as far as your options go here, far as I can understand. :)

sql inconsistent datatypes: expected number got char

I am trying to write a Case When statement, but I get an inconsistent datatypes error. I need 'Returned' displayed if the first when statement is not met.
CASE
WHEN (X.RECEIVED_QTY = 0) THEN FLOOR(SYSDATE-INVENTORY_TRANS.TRANSACTION_DATE)
WHEN (X.RECEIVED_QTY = 0) THEN 'RETURNED'
END AS DAYS_OUT
You can't mix results of a CASE statement, that means you can't return an INT under one condition and a VARCHAR in another. If you want to return Returned then you will need to CONVERT or CAST your numeric values to VARCHAR
You also have a syntactical problem with your CASE.
You'd have to change it to something like this:
CASE X.RECEIVED_QTY
WHEN 0 THEN CAST( FLOOR(SYSDATE-INVENTORY_TRANS.TRANSACTION_DATE) AS VARCHAR(20))
ELSE 'RETURNED'
END AS DAYS_OUT

Specify order of (T)SQL execution

I have seen similar questions asked elsewhere on this site, but more in the context of optimization.
I am having an issue with the order of execution of the conditions in a WHERE clause. I have a field which stores codes, most of which are numeric but some of which contain non-numeric characters. I need to do some operations on the numeric codes which will cause errors if attempted on non-numeric strings. I am trying to do something like
WHERE isnumeric(code) = 1
AND CAST(code AS integer) % 2 = 1
Is there any way to make sure that the isnumeric() executes first? If it doesn't, I get an error...
Thanks in advance!
The only place order of evaluation is guaranteed is CASE
WHERE
CASE WHEN isnumeric(code) = 1
THEN CAST(code AS integer) % 2
END = 1
Also just because it passes the isnumeric test doesn't guarantee that it will successfully cast to an integer.
SELECT ISNUMERIC('$') /*Returns 1*/
SELECT CAST('$' AS INTEGER) /*Fails*/
Depending upon your needs you may find these alternatives preferable.
Why not simply do it using LIKE?:
Where Code Not Like '%[^0-9]%'
Btw, either using my solution or using IsNumeric, there are some edge cases which might lead one to using a UDF such as 1,234,567 where IsNumeric will return 1 but Cast will throw an exception.
Why not use a CASE statement to say something like:
WHERE
CASE WHEN isnumeric(code) = 1
THEN CAST(code AS int) % 2 = 1
ELSE /* What ever else if not numeric */ END
You could do it in a case statement in the select clause, then limit by the value in an outer select
select * from (
select
case when isNum = 1 then CAST(code AS integer) % 2 else 0 end as castVal
from (
select
Case when isnumeric(code) = 1 then 1 else 0 end as isNum
from table) t
) t2
where castval = 1