How to round this result in SQL SELECT statement - sql

I need a method to round the result returned from the following statement:
CASE
WHEN 0 <> (sod.XDFFLT - sod.DUEQTY)
THEN (sod.XDFFLT - sod.DUEQTY)
ELSE ''
END AS Balance
Which resides in this SELECT statement.
SELECT TOP (10000) som.ORDNUM
,som.NAME
,som.ADDR1
,som.ADDR2
,som.CITY
,som.STATE
,som.ZIPCD
,som.CNTRY
,som.CUSTPO
,COALESCE(cpd.CUSTPRT, sod.PRTNUM) AS CustomerSku
,cpd.CUSTPRT
,som.CUSTID
,sod.PRTNUM
,ps.PMDES1
,sod.CURQTY
,sod.DUEQTY
,sod.XDFFLT
,sod.SHPQTY
,CASE
WHEN 0 <> (sod.XDFFLT - sod.DUEQTY)
THEN (sod.XDFFLT - sod.DUEQTY)
ELSE ''
END AS Balance
,sod.LINNUM + sod.DELNUM AS LineDelNum
,CASE
WHEN 12 <= len(sod.UDFREF)
THEN substring(sod.UDFREF, 9, 4)
ELSE sod.UDFREF
END AS Skid

Replace the CASE with this:
,CASE
WHEN 0 <> (sod.XDFFLT - sod.DUEQTY)
THEN ROUND((sod.XDFFLT - sod.DUEQTY), <number_of_decimals>)
ELSE NULL
END AS Balance
Make sure to replace the <number_of_decimlas> with the appropriate number of decimals you want the number to be rounded to.
greater than 0 will return a number which is rounded on the right side of the comma (decimal point)
less than 0 will return a number which is rounded on the left side of the comma (decimal point)
Also, don't use a blank string in the ELSE clause of your case since this will cause a datatype missmatch and generate an error (a CASE must return a single datatype). Hence, it is better to replace this with NULL, since your first condition will always return a number.

Related

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

SQL - get only numbers including minus value not alphabets and character from a column

I have a column which has numbers(including negative values),alphabets,alphanumeric and single character symbol(like -,&,#).
How to get rid of alphabets,alphanumeric and symbols and get the sum of the column.
I have to use the condition only in select statement not in where condition. Because it should not affect other column results.
This is what I've tried:
SELECT COUNT(*), CASE
WHEN REGEXP_LIKE(REMD_PART_CSN, '^-?[0-9]\d*(\.\d+)?$') THEN SUM(REMD_PART_CSN)
ELSE NULL
END
FROM <TABLE>
How about this?
SELECT
COUNT(*),
SUM(
CASE
WHEN REGEXP_LIKE(REMD_PART_CSN, '^-?\d+(\.\d+)?$')
THEN CAST(REMD_PART_CSN AS NUMBER)
ELSE 0
END
)
FROM yourtable
I.e. for every row, if REMD_PART_CSN looks like a number, then convert it to a number, else use 0 instead. At the end, return the sum of all those values.

How to change number of digits after decimal point?

I have this line in my query:
CASE WHEN count(n.Counter) <> 0 THEN (cast(count(n.Counter) as float)/count(t.Counter)) * 100 ELSE 0 END as percent
I'm getting results like 79.565974946.
I want to get a result like 80% (with no digits after the decimal point and with '%' sign).
How can i do that?
You need to ROUND and then, because you're trying to do formatting, convert to a string before adding the %:
CONVERT(varchar(20),
ROUND(
CASE WHEN count(n.Counter) <> 0
THEN (cast(count(n.Counter) as float)/count(t.Counter)) * 100
ELSE 0 END
,0)) + '%' as percent
Also, there may be a typo here - it looks like you're trying to avoid a divide by zero error, but you're testing the wrong operand (n.Counter rather than t.Counter) so you may still get a division by zero from this code.
If using MySQL, just use the ROUND function.
CASE WHEN count(n.Counter) <> 0 THEN ROUND(cast(count(n.Counter) as float)/count(t.Counter)) * 100 ELSE 0 END as percent
Try this:
CASE WHEN count(n.Counter) <> 0 THEN round((cast(count(n.Counter) as float)/count(t.Counter)) * 100,0) ELSE 0 END as percent
Where are you showing the data? Adding % should be done at the front end application. Just use this and format it in fornt end if you use
round(
case when count(n.counter) <> 0
then (cast(count(n.counter) as float)/count(t.counter)) * 100
else 0 end
,0))

SQL Server: casting the column datatype

SQL Server 2005/ 2008
I have kept a CASE condition for a SQL query which results Float /Numeric Value. One of the CASE condition is to say N/A. I kept the piece of code.
For column : Compliance - Possible values could be N/A,100.0,99.1 ... ( nvarchar, float ).
select
x.MemberName, x.DOB, x.FilePath,
x.Medication, x.NDC, x.Directions,
x.Name, x.Strength, x.GenericName,
x.QtyOrdered, x.DaysSupply, x.DateFilled,
CASE WHEN x.test = 0 THEN 'N/A'
WHEN compliance > 100.0 THEN '100.0'
ELSE CAST(FLOOR(compliance * 10)/10.0 AS DECIMAL(3, 1))
END AS [Compliance]
Above syntax spills error as ..
Msg 8114, Level 16, State 5, Line 10
Error converting data type varchar to numeric.
How should I type cast the field ?
CASE is an expression that returns a single result and regardless of the path it always needs to result in the same data type (or implicitly convertible to the same data type). Try:
...
CASE
WHEN x.test = 0 THEN 'N/A'
WHEN compliance > 100.0 THEN '100.0'
ELSE CONVERT(VARCHAR(5), CAST(FLOOR(compliance *10)/10.0 AS DECIMAL(3,1)))
END AS as [Compliance];
You're not consistent in your CASE statement - you need to provide the same data type as result for all options.
CASE WHEN x.test = 0 THEN 'N/A'
WHEN compliance > 100.0 THEN '100.0'
ELSE CAST(FLOOR(compliance * 10)/10.0 AS DECIMAL(3, 1))
END AS [Compliance]
Since the first two options return a string ('N/A' or '100.0'), your last option also must return a string value:
CASE WHEN x.test = 0 THEN 'N/A'
WHEN compliance > 100.0 THEN '100.0'
ELSE CAST(CAST(FLOOR(compliance * 10)/10.0 AS DECIMAL(3, 1)) AS VARCHAR(20))
END AS [Compliance]

SQL Computer Column Formula

A SQL Table (Trades) has three fields AvgProfit, MinProfit and Hold - Hold is a Computed Colum...
If AvgProfit < MinProfit, Hold = 'Hold' and if AvgProfit > MinProfit, Hold = 'Trade'
The Computed Col formula for Hold is as follows.
SQL Table Trades
AvgProfit varchar(35)
MinProfit varchar(35)
Hold varchar(35)
(case when CONVERT([decimal](8,4),isnull([AvgProfit],(0)),(0))>=CONVERT([decimal](8,4),isnull([MinProfit],(0)),(0)) then 'Trade' else 'Hold' end)
PROBLEM: Updates cause the AvgProfit to be empty at times and this results in an error when the table references the Hold formula
'Error Converting varchar to numeric'
How do I add IS NULL or EMPTY to the above formula, the ISNULL does not catch AvgProfit = '' ??
Consider typing your database with decimal or numeric columns as paxdiablo has suggested. Is there a reason why those columns are set as varchar?
Alternatively if you have to keep your varchar columns, try ISNUMERIC(), http://msdn.microsoft.com/en-us/library/ms186272.aspx. It works with all common SQL numeric types. So you query might become:
ISNUMERIC can be used like this:
select case when ISNUMERIC('123') = 1 then 1 else 0 end ' returns 1
select case when ISNUMERIC('xyz') = 1 then 1 else 0 end ' returns 0
select case when ISNUMERIC(null) = 1 then 1 else 0 end ' returns 0
So your query might become:
case when isnumeric(AvgProfit) = 1 THEN case([AvgProfit] as decimal) else 0 END
Or something similar.
varchar columns should be used for VARiable sized CHARacter columns, not for numeric data.
In other words, they shouldn't be empty (other than NULL, of course, but you've taken care of that).
If you cannot fix that little oversight for some reason, you can make your queries even more complex and slower :-) by doing something like:
select case when xyz = '' then 0 when isnull(xyz) then 0 else xyz end ...
In other words, check for both NULL and empty varchar values.
Myself,I'd fix the schema since it'll be better for you in the long term. I'm merely offering the other solution on the chance that you're not able to do that.