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;
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.
Trying to return a column, giving 1, when column1 is NOT NULL and different than 0. So far managed to do this:
MAX(CASE WHEN column1 IS NOT NULL
THEN CASE WHEN column1 <> 0 THEN 1 ELSE 0 END
ELSE 0 END)
Getting this error:
SELECT Failed. 2620: The format or data contains a bad character.
It works quite ok with NOT NULL as a single condition, though.
I'm not sure why you need to nest anything, but it looks like you're missing the non-equality sign.
You might try,
CASE
WHEN column1 IS NOT NULL AND column1 <> 0
THEN 1
ELSE 0
END
Alternatively, this will produce the same result as an OR operator, where, but CASE executes the WHEN clauses in order.
CASE
WHEN column1 IS NOT NULL
THEN 1
WHEN column1 <> 0
THEN 1
ELSE 0
END
But in your description, it sounds like you wanted BOTH conditions to be true, so it doesn't make sense to nest or use multiple WHEN statements, because you can just connect them together with AND
I have a multilist field returning a list of values.
My query is filtering the list using IN (list), but if the user do not select anything is list, it returns an empty list, or null (I can't see).
I'm trying to do something like:
case when #list is null then 1
when c.SK_dimLocalizacao in (#list) then 1
else 0
end = 1
It works when I select none or one value, but if I select more than one it bring me an error.
I already tried to use:
case when #list = '' then 1
when c.SK_dimLocalizacao in (#list}) then 1
else 0
end = 1
I can't modify the query in backend because i'm using Pentaho.
Maybe this? (not sure if I understand the end goal)
IF LEN(ISNULL(#list,'')) <> 0
BEGIN
--Query for if the list isn't empty or null
END
ELSE
--Query for if the list is empty or null
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.
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