I want to check for a pattern in sql such that if there is anything in that expression (or table cell) other than numeric it should return 1. If that whole cell has only numeric values it should return 0
eq:
case when '200290' like [anything other than numbers]
then 1
else o
In SQL Server, you can use something like (I'm not writing the whole function for you):
DECLARE #t varchar(100) = '231321321321'
SELECT CASE WHEN PATINDEX('%[^0-9]%', #t) > 0 THEN 1
ELSE 0 END
SELECT CASE CHARACTERS(OTRANSLATE(<your_string>,'a1234567890','a'))
WHEN 0 THEN 0 ELSE 1 END;
Ex.:
-- BTEQ Enter your SQL request or BTEQ command:
SELECT CASE CHARACTERS(OTRANSLATE('12345','a1234567890','a'))
WHEN 0 THEN 0 ELSE 1 END;
* Query completed. One row found. One column returned.
* Total elapsed time was 1 second.
<CASE expression>
------------------
0
SELECT CASE CHARACTERS(OTRANSLATE('1a23b45c','a1234567890','a')) WHEN 0 THEN 0 ELSE 1 END;
* Query completed. One row found. One column returned.
* Total elapsed time was 1 second.
<CASE expression>
------------------
1
Related
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 verify one column only has numbers(integer number only) in oracle . If yes then set flag as 1 otherwise 0. I am trying to use below reg_exp( but it sets flag to 1 even for hyphen ):
CASE WHEN REGEXP_LIKE(Column_name,'\d\) THEN 1 ELSE 0 END
Examples:
12345 should ( this is working and above code is setting flag to 1)
-1234 should set flag to 0 ( above code is setting it to 1)
abcd should set flag to 0 ( above code is working correct in this case)
123.23 should set flag to 0
If you want check if contain only digit or not try
CASE WHEN REGEXP_LIKE(Column_name, '^[[:digit:]]+$') THEN 1 ELSE 0 END ;
You may use TRANSLATE function
SELECT case when TRANSLATE(Column_name, 'X0123456789', 'X') is null
then 1
else 0
end
FROM t;
I tried to:
select 1>2 from dual;
but got:
ORA-00923: FROM keyword not found where expected
Is there boolean type for column expression in Oracle SQL?
I able to do:
select case when 1>2 then 'T' else 'F' end from dual;
Originally I tried to compare date fields and the quickest way I found was getting difference and look to sign...
UPDATE I tried SIGN function, I don't know if it is vendor specific extension:
select SIGN(1-2) from dual;
select SIGN(DATE '2017-01-02' - DATE '2017-02-12') from dual;
but this trick doesn't work for strings...
No there is not, you can use 0 and 1 just as yes/no.
If you need to get the result 1 if something is true and 0 if it is false, you can use a case expression:
select case when (any_logical_condition_here) then 1 else 0 end as my_col
from ....
where ....
For example:
select case when 1 > 2 then 1 else 0 end as bool_result
from dual;
BOOL_RESULT
---------------------------------------
0
NOTE though - "Boolean" refers strictly to the TRUE/FALSE logic, it has no place for UNKNOWN. When you deal with null, as you must in SQL, you need three-valued logic. The case expression as written above returns 1 when the logical condition is true and 0 otherwise. Try it with 1 > null - the truth value is UNKNOWN, the case expression will return 0.
Given a table with column like city and cityId,Adderesses. How would i write a query that gives a list cities where cityID is only even numbers. Plz explain in details.
Use the modulo operator
select * from your_table
where mod(cityID, 2) = 0
You can detect whether a number is even with the module operator (%), which gives the remainder after division by 2:
select * from your_table where cityID % 2= 0
The above query will give all the rows in which the cityID is divisible by 2
If you are interested in mod then it would be
select 6%2 -- results for this is 0
if you would want to select a number divided by 2 it will always return 0 for the perfect devisors.
if want the division the use the normal / operator as below
eg select 6/3
I used a slightly different approach. this is an event if you want a result of a zero or 1 if perfect divisor and zero otherwise
So my script is something like this
select case when #value<2 then 0 when #value%2 =0 then 1 else 0 end as myresult;
An example is as below for validating numbers between 1 and 20. You could amend it to fit your purpose
begin
declare #a int,#b int
set #a=1;
set #b = 20
while #a<=#b
begin
declare #value int
set #value =#a
select #value as myval, case when #value<2 then 0 when #value%2 =0 then 1 else 0 end
as myresult;
set #a=#a+1;
end
end
I like to check the length of a specified fields is x length and available value is a numeric value
i can do this by using two separate case statements like below
(case when len(columnA) = 10 then 0 else 1 end)+
(case when IsNumeric(columnA) = 1 then 0 else 1 end)+
(case when len(columnB) = 8 then 0 else 1 end)+
(case when IsNumeric(columnB) = 1 then 0 else 1 end)
is there any better approach as i need to this for more than 40 columns and their datatype is varchar and each of the column will have specific length.
using some short cut to reduce above two case statements into one line
If you only care that a column is invalid and are less interested in which criteria it failed on then you could just put both criteria into the one case statement as
(case when len(columnA) = 10 and IsNumeric(columnA) = 1 then 0 else 1 end)
In regards to the issue Sean raised, ISNUMERIC only confirms the value can be converted to a numeric datatype so commas and periods are valid too. you could do a check for any single character that isn't in the range of numbers
case when len(columnA) = 10 and ColumnA not like '%[^0-9]%' then 0 else 1 end
It is a little ugly because we have to say is not [not in range], so you might want to change the logic a bit.