How do I create an If-Then-Else in T-SQL - sql

I have some negative values coming back from a query. I would like them to just be zero.
How do I write a condition in my sql query that returns zero if the value is below a certain value.
sol:
CASE WHEN CONVERT(float,dt.FQI53X02_101) < 1.3 THEN 0 ELSE CONVERT(float,dt.FQI53X02_101) END AS FQI53X02_101

You dont use If-Then-Else in the actual query (you can use them but thats something else)...
What you use is a Case statement... Try this
Select
Case When [Value] < 0 Then 0 Else [Value] End
From
Example

If you want it as part of your query, wrap the return inside a CASE statement. Example from MSDN is below
SELECT 'Price Category' =
CASE
WHEN price IS NULL THEN 'Not yet priced'
WHEN price < 10 THEN 'Very Reasonable Title'
WHEN price >= 10 and price < 20 THEN 'Coffee Table Title'
ELSE 'Expensive book!'
END,
CAST(title AS varchar(20)) AS 'Shortened Title'
FROM titles
ORDER BY price

( ABS(Value) + Value ) / 2
edit - this doesn't work now the question has changed

Related

How do I set an upper bound to an INT value within a select statement in SQL?

I'm looking to make part of my query more performant by cutting down on the number of case statements I use. I have a select statement as below currently:
SELECT
ID,
CASE WHEN sum(Value1) > 0 THEN 1 ELSE 0 END as [Value1],
CASE WHEN sum(Value2) > 0 THEN 1 ELSE 0 END as [Value2],
CASE WHEN sum(Value3) > 0 THEN 1 ELSE 0 END as [Value3],
CASE WHEN sum(Value4) > 0 THEN 1 ELSE 0 END as [Value4],
....
FROM table
Essentially I want the query to simply produce a boolean, either a 0 or a 1, but the case statements have tripled the runtime for my query which is less ideal. Is there a way I can force a boolean output or set a maximum value for my select and trim away the cases?
As John Cappelletti has provided, the expected output of limiting an INT value into a Boolean 0/1 output is achieved via the SIGN function. This reduces the runtime for the query significantly compared to the case statement.

i need to use when in sql view

Please its just a simple question , and i did it before but i cant do it right now .
i need to write when in column in sql view to give me value based on another column
as shown in the pic below
i need to make a new column to give me a range of total value from 1000 to 2000 to be ' 1000-2000'
i used to write it as shown
when [total] between '1000' and '2000' then '1000-2000' else 'not'
It should be case when expression
case when [total] between 1000 and 2000 then '1000-2000' else 'not' end
use case when like below and I'd prefer >= and < rather than between
case when total>=1000 and total<=2000 then '1000-2000' else 'not' end

Invalid argument for function integer IBM DB2

I need to filter out rows in table where numer_lini column has number in it and it is between 100 and 999, below code works just fine when i comment out line where i cast marsnr to integer. However when i try to use it i get error: Invalid character found in a character string argument of the function "INTEGER". when looking at the list seems like replace and translate filters only numbers just fine and select only contains legit numbers (list of unique values is not long so its easy to scan by eye). So why does it fail to cast something? I also tried using integer(marsnr), but it produces the same error. I need casting because i need numeric range, otherwise i get results like 7,80 and so on. As I mentioned Im using IBM DB2 database.
select numer_lini, war_trasy, id_prz1, id_prz2
from alaska.trasa
where numer_lini in (
select marsnr
from (
select
distinct numer_lini marsnr
from alaska.trasa
where case
when replace(translate(numer_lini, '0','123456789','0'),'0','') = ''
then numer_lini
else 'no'
end <> 'no'
)
where cast(marsnr as integer) between 100 and 999
)
fetch first 300 rows only
If you look at the optimized SQL from the Db2 explain, you will see that Db2 has collapsed your code into a single select.
SELECT DISTINCT Q2.NUMER_LINI AS "NUMER_LINI",
Q2.WAR_TRASY AS "WAR_TRASY",
Q2.ID_PRZ1 AS "ID_PRZ1",
Q2.ID_PRZ2 AS "ID_PRZ2",
Q1.NUMER_LINI
FROM ALASKA.TRASA AS Q1,
ALASKA.TRASA AS Q2
WHERE (Q2.NUMER_LINI = Q1.NUMER_LINI)
AND (100 <= INTEGER(Q1.NUMER_LINI))
AND (INTEGER(Q1.NUMER_LINI) <= 999)
AND (CASE WHEN (REPLACE(TRANSLATE(Q1.NUMER_LINI,
'0',
'123456789',
'0'),
'0',
'') = '') THEN Q1.NUMER_LINI
ELSE 'no' END <> 'no')
Use a CASE to force Db2 to do the "is integer" check first. Also, you don't check for the empty string.
E.g. with this table and data
‪create‬‎ ‪TABLE‬‎ ‪alaska‬‎.‪trasa‬‎ ‪‬‎(‪numer_lini‬‎ ‪VARCHAR‬‎(‪10‬‎)‪‬‎,‪‬‎ ‪war_trasy‬‎ ‪INT‬‎ ‪‬‎,‪‬‎ ‪id_prz1‬‎ ‪INT‬‎,‪‬‎ ‪id_prz2‬‎ ‪INT‬‎)‪;
insert into alaska.trasa values ('',1,1,1),('99',1,1,1),('500',1,1,1),('3000',1,1,1),('00300',1,1,1),('AXS',1,1,1);
This SQL works
select numer_lini, war_trasy, id_prz1, id_prz2
from alaska.trasa
where case when translate(numer_lini, '','0123456789') = ''
and numer_lini <> ''
then integer(numer_lini) else 0 end
between 100 and 999
Although that does fail if there is an embedded space in the input. E.g. '30 0'. To cater for that, a regular expressing is probably preferred. E.g.
select numer_lini, war_trasy, id_prz1, id_prz2
from alaska.trasa
where case when regexp_like(numer_lini, '^\s*[+-]?\s*((\d+\.?\d*)|(\d*\.?\d+))\s*$'))
then integer(numer_lini) else 0 end
between 100 and 999

Case inside 'where' section that changes the condition of an AND statement

I am creating a store procedure and i am wondering how can i add a case block in an Add statement inside the where statement.That case statement checks an input parameter and depending its value it will change the condition from greater that to smaller than and of course be added to the add conditions
So a part of the query is like:
WHERE
AND BM.Example1 IS NOT NULL
AND BM.Example2 IS NOT NULL
AND ( Case When #inputParamter= 'A' THEN AND BM.Example < 0 ELSE And BM.Example> 0 )
ORDER BY 'SEG' ASC, 'CCY' ASC
So by this approach i am thinking to extract an add statement depending on the input parameter but unfortunately i keep getting syntax errors.
Is that possible?
Yepp, just use this:
AND (( #inputParamter= 'A' AND BM.Example < 0) OR ( #inputParamter<>'A' AND BM.Example> 0) )
However, be carefull with NULL, you have to put it in the logic as a third option.
here is a similar answer using case
AND ( Case When #inputParamter = 'A' AND BM.Example < 0 THEN 'Y'
When #inputParamter <> 'A' AND BM.Example > 0 THEN 'Y' ELSE 'N' END = 'Y')

How do I change a field to a negative based on another field's value in SQL

With the below statement I would like amount to be negative if [Invoice_Type_Code] = 'c', and if is not then it is positive.
SELECT [Invoice_Amount]
FROM [Forefront].[dbo].[VN_GL_DISTRIBUTION_HEADER_MC]
WHERE [Vendor_Code] =' UnitedEL' and
[Date_List1] > '2011-12-31' and
[Date_List1] < '2012-02-01' and
[Company_Code] = 'tmg'
Depending on your RDBMS, using CASE could work -- most RDBMS should support it:
SELECT CASE WHEN Invoice_Type_Code = 'C' THEN -1 ELSE 1 END * Invoice_Amount
FROM [Forefront].[dbo].[VN_GL_DISTRIBUTION_HEADER_MC]
where [Vendor_Code] =' UnitedEL' and
[Date_List1] > '2011-12-31' and [Date_List1] < '2012-02-01' and
[Company_Code] = 'tmg'
This assumes all Invoice_Amounts are positive. If they aren't positive and you still need negative values, use the ABS function and alter your case statement slightly.
Here is the Fiddle.
Good luck.