I have a a query which retrieves 2 times a count from 2 tables.
Now in the same query it has (countresult1-countresult2) AS restresult
Now restresult is sometimes less than 0 (eq -10) but I want it to return 0 if it's under 0.
Uhm did I explan that right? Minimum value should be 0 not below.
Cheers!!!
GREATEST((countresult1-countresult2), 0) AS restresult
if (countresult1<countresult2, 0, countresult1-countresult2) as restresult
neither countresult1 nor countresult2 will return a negative number, so above should be safe
without seeing your query, you could have something like...
MAX( if( countresult1-countresult2 < 0, 0, countresult1-countresult2 )) as YourResult
Take the maximum of 0 and the value you calculated, like this:
SELECT GREATEST(your-restresult-query,0)
FROM ... (etc)
Until Now I didn't know there was if-else commands in SQL, but I found some.
you will want to use:
WHEN (countresult1-countresult2) < 0 THEN 0 ELSE (countresult1-countresult2)
Here is the source where I found the SQL information: http://www.tizag.com/sqlTutorial/sqlcase.php
Related
I have a query that looks like this:
Select
CHARINDEX('Test_Value',STRING_AGG(FSD.Test_Value_Field, ';')) As My_Return_Value,
FROM QE_CS CS
...
Which returns a value greater than 0 if it's in the aggregated string, however, I just want it to return 1 if the value is greater than 0. I tried using IF like this:
Select
IF CHARINDEX('Test_Value',STRING_AGG(FSD.Test_Value_Field, ';')) > 0 THEN 1 ELSE 0 As My_Return_Value,
FROM QE_CS CS
But this returns:
Incorrect syntax near the keyword 'IF'
Clearly I'm not familiar with using IFs in SQL server - how can I get my expected result?
If statement cannot be used in select statement.
Use case statement like this
Select
CASe when CHARINDEX('Test_Value',STRING_AGG(FSD.Test_Value_Field, ';')) > 0 THEN 1 ELSE 0 end As My_Return_Value,
FROM QE_CS CS
Yet another option is sign()
Example
sign(CHARINDEX('Test_Value',STRING_AGG(FSD.Test_Value_Field, ';')))
Sign() will return -1, 0, 1 or null
You can try this:
Select IIF(CHARINDEX('Test_Value',STRING_AGG(FSD.Test_Value_Field, ';')) > 0, 1, 0) As My_Return_Value FROM QE_CS CS
If you have multiple conditions to be checked CASE is the better option, however you can use 'IIF' function too, official docs here
select IIF(CHARINDEX('Test_Value',STRING_AGG('ABC;Test_Value', ';')) > 0 ,1,0) from QE_CS CS
what excatly 1 - NVL(up, 0) down this statement mean , is it giving me the contradiction of up ?
Presumably, up is a value that can take up to three values: 0, 1, and NULL.
The NVL() function is a database-specific function for COALESCE(). They do the same thing.
So, simply look at what the results are:
up 1 - coalesce(up, 0)
1 0
0 1
NULL 1
So, it is "flipping the switch". That is, when "up" is "true", then it switches it to "false". It treats 0 as "false" for this purpose.
Hi can any one say me how to write query in sql server management studio for statement =>if(isnull("GROSS_SF")=1 or "GROSS_SF"=0,0,"GROSS_SALES"/"GROSS_SF")
by using case statement :
CASE
WHEN ("GROSS_SF"=1 or "GROSS_SF"=0) isnull then 0
else "GROSS_SALES"/"GROSS_SF"
end/* i am getting error if i write it like this */
thanks in advance
If you want to avoid a divide by zero, while at the same time also handling NULL, then you may try the following logic:
CASE WHEN COALESCE(GROSS_SF, 0) = 0
THEN 0
ELSE GROSS_SALES / GROSS_SF END AS output
To be clear, the above logic would return a zero value should either the GROSS_SF be zero or NULL.
I think what are you looking for is IFNULL, look here https://www.w3schools.com/sql/sql_isnull.asp
You are close. You can do:
(CASE WHEN "GROSS_SF" IS NULL OR "GROSS_SF" = 0 THEN 0
ELSE "GROSS_SALES" / "GROSS_SF"
END)
In SQL, divide by zero errors are often avoided using NULLIF():
"GROSS_SALES" / NULLIF("GROSS_SF", 0)
However, that returns NULL rather than 0. If you really want zero:
COALESCE("GROSS_SALES" / NULLIF("GROSS_SF", 0), 0)
This is a bit shorter to write and almost equivalent to your version (this returns 0 if "GROSS_SALES" is NULL, which your version does not).
You can use NULLIF() to prevent arithmetic divide by zero errors :
select . . . ,
GROSS_SALES / nullif(GROSS_SF, 0)
from table t;
However, this would return null instead of error, if you want to display 0 instead then you need to use isnull() (MS SQL Specific) or coalesce() (SQL Standard).
So, you can express it :
select . . . ,
isnull(GROSS_SALES / nullif(GROSS_SF, 0), 0)
from table t;
I am having difficulty when trying to have 3 conditions to fulfill in CASE WHEN in SQL server. Here is my original script but only return value 0 instead of 1 and 0. My purpose is to check when a customer purchase ITEM_01 with more than or equal to 0.06 AND ITEM_02 with more than or equal to 0.06 THEN return value 1 else 0. Here is my script:
CASE WHEN SUM ( CASE WHEN ITEM_01)='DH' AND (ITEM_02)='DH Classic' THEN NVL
(SALE_OUTLETD.PRE_SALES_QTY * SKU_UOM_CONV.MULTIPLIER,0) + NVL
(SALE_OUTLETD.SALES_QTY * SKU_UOM_CONV.MULTIPLIER, 0) -NVL
(SALE_OUTLETD.FRESH_RTN_QTY *SKU_UOM_CONV.MULTIPLIER, 0) - NVL
(SALE_OUTLETD.OLD_RTN_QTY * SKU_UOM_CONV.MULTIPLIER,0) - NVL
(SALE_OUTLETD.DAMAGED_RTN_QTY *SKU_UOM_CONV.MULTIPLIER, 0) - NVL
(SALE_OUTLETD.WITHDRAWAL_RTN_QTY *SKU_UOM_CONV.MULTIPLIER, 0) ELSE 0 END )
>= 0.06 THEN 1 ELSE 0 END 3_PACK_AND_ABOVE
Please help on this question because I have been stuck for few hours now. Any help would be greatly appreciated!
I am finding your example impossible to read but the basic form of CASE is:
select case WHEN first_boolean THEN first_value
WHEN second_boolean THEN second_value
WHEN third_boolean THEN third_value
...
ELSE default_value
Always try to build things off that model. Nested CASE statements like what you are doing are never fun and tend to be fairly error prone.
I have a view in SQL Server 2008 with several columns that are expressions of one column divided by another. I have to account for the divisor being 0, so I use a CASE statement. Here is an example of one:
CASE SUM(dbo.GameStats.BringBacksAttempted)
WHEN 0 THEN
0
ELSE
SUM(dbo.GameStats.BringBacks) / SUM(dbo.GameStats.BringBacksAttempted)
END
Even when SUM(BringBacksAttempted) and SUM(BringBacks) are not 0, the statement is always 0. What am I doing wrong?
What data type is BringBacksAttempted and BringBacks?
If both are int and the result comes to be a fraction, you will only see integer part of it.
e.g. 100 / 250 will return 0.
Whereas, CAST(100 as Decimal) / 250 will return 0.40000
Use a CAST or CONVERT on one of the fields.
Try:
CASE
WHEN SUM(dbo.GameStats.BringBacksAttempted) = 0 THEN
0
ELSE
SUM(dbo.GameStats.BringBacks) / SUM(dbo.GameStats.BringBacksAttempted)
END