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

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.

Related

Need to show as loss or gain as a separate column based on the output in sql oracle

I need to show like loss/gain for a column based on the amount
If the result amount is -10000 then I need to show as loss , if its 10000 means gain in sql query..
Here is my query if the output is negative then i should display the amount and one more column as loss and if the output is positive then i should display the amount and one more column as gain
select ((NVL (asset_cost, 0) - NVL (accumalated_dep, 0))- NVL (closed_lability, 0))
From Asset_management
just wrap in a sub query or CTE to avoid typing it out again:
select value,case when value =-10000 then 'loss' case when value =10000 then 'gain' else '??' end
from
(
select ((NVL (asset_cost, 0) - NVL (accumalated_dep, 0))- NVL (closed_lability, 0)) as value
From Asset_management
) as calc
or
with
(
select ((NVL (asset_cost, 0) - NVL (accumalated_dep, 0))- NVL (closed_lability, 0)) as value
From Asset_management
) as calc
select value,case when value =-10000 then 'loss' case when value =10000 then 'gain' else '??' end
(approximate syntax due to no usable scripts)

Check specific integer is even

I want to know if the 4th integer in the ID, is even, or if its odd.
If the 4th number is even (if the number is either 0,2,4,6,8 I want to put the ID into a new column named 'even'
IF the 4th number is odd, the column should have the name 'Odd'
select ID as 'Female'
from Users2
where ID LIKE '%[02468]'
This shows if any of the numbers are even. I want to specify the 4th number
Try this:
select *, OddOrEven = iif(substring(ID,4,1) in ('0','2','4','6','8') , 'Even', 'Odd') from Users2
This will tell you whether the 4th character is Odd or Even.
This is of course assuming that the 4th character of ID column will be numeric.
To make it permanently part of the table, you can add a computed column as shown below.
alter table Users2
add OddOrEven as iif(substring(ID,4,1) in ('0','2','4','6','8'), 'Even', 'Odd')
Substring the character you are interested in
Convert to an int
Check whether modulus 2 returns 0 (i.e. even).
select id
, case when convert(int,substring(id, 4, 1)) % 2 = 0 then 'Even' else 'Odd' end
from Users;
Example:
select id
, case when convert(int,substring(id, 4, 1)) % 2 = 0 then 'Even' else 'Odd' end
from (values ('4545-4400'), ('4546-4400')) X (id);
Returns
id
4545-4400
Odd
4546-4400
Even
Thats assuming there is always a 4th character. If not you would need to check for it.
You were close, but only need to check a single character against a set of characters:
where Substring( Id, 4, 1 ) like '[02468]'
Note that there is no wildcard (%) in the pattern.
It can be used in an expression like:
case when Substring( Id, 4, 1 ) like '[02468]' then 'Even' else 'Odd' end as Oddity

SQL query to sort data while insert , first numbers then alphabets an last symbols

I am getting trouble to write SQL sort Query,
I have table as follows
And I want to sort above data as, First should number and then alphabets and last special symbols like following table.
First numbers should sort like 1,2,3,11,22,33
then Alphabets should sort like a ,b,c,..z
and then symbols,
like following table
I have tried many ways but still not getting correct way, please help me to write query.
You can use a CASE WHEN on the ORDER BY to create some groups:
SELECT *
FROM table_name
ORDER BY
CASE WHEN LEFT(FilterParameterValue, 1) LIKE '[0-9]' OR LEFT(FilterParameterValue, 2) LIKE '-[0-9]' OR LEFT(FilterParameterValue, 2) LIKE '+[0-9]' THEN 1 ELSE 0 END DESC, -- group for numbers
CASE WHEN ISNUMERIC(FilterParameterValue) = 1 THEN CAST(FilterParameterValue AS MONEY) ELSE NULL END ASC, -- order the numeric values
CASE WHEN LEFT(FilterParameterValue, 1) LIKE '[A-Za-z]' THEN 1 ELSE 0 END DESC, -- group for chars from A to Z (capital / non capital)
colName
demo on dbfiddle.uk
Try using regex in order by clause.. For ex
ORDER BY IF(FilterParameterValue RLIKE '^[a-z]', 1, 2), FilterParameterValue
You can try to cast your Column to Numeric Type then ordered
SELECT * FROM table_name ORDER BY TRY_CAST(Column_Name as Type)

divisor is equal to zero in sql need to add some sort of try catch

I have this query that i get errors when data gets loaded to this table and the divisor is zero which is the b. living_units column. I want to know if I can do some sort of try catch or if its zero to show null instead of failing or something that will help not error out?
SELECT a.SERVICE_TYPE_GRP,
a.HSIA_TYPE,
a.STAT_DYN_IND,
a.VIDEO_IND,
a.VOICE_IND,
a.CUST_CNT,
b.LIVING_UNIT_CNT,
a.DT_MODIFIED,
a.CUST_CNT / b.LIVING_UNIT_CNT AS ALLRGN_TK_RT_PCT
FROM ( SELECT lp.SERVICE_TYPE_GRP SERVICE_TYPE_GRP,
lp.HSIA_TYPE HSIA_TYPE,
lp.STAT_DYN_IND STAT_DYN_IND,
lp.VIDEO_IND VIDEO_IND,
lp.VOICE_IND VOICE_IND,
lp.DT_MODIFIED DT_MODIFIED,
SUM (lp.CUST_CNT) CUST_CNT
FROM RPT_SUBSCR_REGION_DTL lp
GROUP BY SERVICE_TYPE_GRP,
HSIA_TYPE,
STAT_DYN_IND,
VIDEO_IND,
VOICE_IND,
DT_MODIFIED) a,
( SELECT DT_MODIFIED, SUM (LIVING_UNIT_CNT) LIVING_UNIT_CNT
FROM RPT_REGION_CUST_DTL
WHERE dt_modified = (SELECT dt_modified
FROM ls_dt_modified
WHERE NAME = 'RPT_REGION_CUST_DTL')
GROUP BY DT_MODIFIED) b
WHERE a.DT_MODIFIED = b.DT_MODIFIED;
The error i get is ORA-01476: divisor is equal to zero.
If you have a complex divisor expression and a NULL result is acceptable, there is an alternative that does not require repeating the divisor expression:
a.CUST_CNT / nullif(b.LIVING_UNIT_CNT, 0)
The NULLIF function returns NULL if the first parameter is equal to the second parameter. In this case it returns NULL if b.LIVING_UNIT_CNT is equal to zero. And anything divided by NULL also becomes NULL. It's a bit of a "trick" maybe, but it saves the expression repetition of the CASE statement.
Try a case statement:
case
when b.LIVING_UNIT_CNT = 0 -- the divisor
then 0 -- a default value
else a.CUST_CNT / b.LIVING_UNIT_CNT
end
ALLRGN_TK_RT_PCT

Setting variables on CASE results in SQL?

I'm sure the answer is simple, but I have been searching for a while and can't seem to get this working ...
I am tring to set a variable based on a Case statement result.
So I am checking length of two columns
dbo.MY_TABLES.MY_RULESET,
dbo.MY_TABLES.MY_SPECIFICATION
My case statements check If length/75 is over 1 then we return 1. If under 1, then we return the value (under 1)
I then want to add these two results together and is where im getting problems when i need the addtion dependant on the case results.
Here is sample
Declare #SpecValue FLOAT
Declare #RuleValue FLOAT
SELECT
CAST(LEN(MY_SPECIFICATION) AS FLOAT)/75 as Score1,
CAST(LEN (MY_RULESET) AS FLOAT)/75 AS Score2,
CASE
WHEN (CAST(LEN(MY_SPECIFICATION) AS FLOAT)/75) >1 Then '1'
ELSE (Round(CAST(LEN(MY_SPECIFICATION) AS FLOAT)/75,2))
END as Spec1_RuleLength,
CASE
When (CAST(LEN (MY_RULESET) AS FLOAT)/75) >1 Then '1'
ELSE (Round(CAST(LEN (MY_RULESET) AS FLOAT)/75,2))
END as Eval2_RuleLength,
dbo.MY_TABLES.MY_RULESET,
dbo.MY_TABLES.MY_SPECIFICATION
FROM MY_TABLES
All columns in a SELECT clause are computed as if they're evaluated in parallel. This means that one column cannot depend on the value of another column computed in the same SELECT.
You might try moving the current query into a subselect:
SELECT Score1 + Score2 /* etc */
FROM (
SELECT
CAST(LEN(MY_SPECIFICATION) AS FLOAT)/75 as Score1,
CAST(LEN (MY_RULESET) AS FLOAT)/75 AS Score2,
CASE
WHEN (CAST(LEN(MY_SPECIFICATION) AS FLOAT)/75) >1 Then '1'
ELSE (Round(CAST(LEN(MY_SPECIFICATION) AS FLOAT)/75,2))
END as Spec1_RuleLength,
CASE
When (CAST(LEN (MY_RULESET) AS FLOAT)/75) >1 Then '1'
ELSE (Round(CAST(LEN (MY_RULESET) AS FLOAT)/75,2))
END as Eval2_RuleLength,
dbo.MY_TABLES.MY_RULESET,
dbo.MY_TABLES.MY_SPECIFICATION
FROM MY_TABLES
) t