How to approach the following query in oracle - sql

How to approach the following query in oracle sql
select last_trans_date,
last_trans_date<sysdate-180 as "a",
last_trans_date<sysdate-360 as "b"
from x;

I'm guessing you want something like this:
select last_trans_date,
CASE
WHEN last_trans_date < sysdate-180 THEN 'YES'
ELSE 'NO'
END as "a",
CASE
WHEN last_trans_date < sysdate-360 THEN 'YES'
ELSE 'NO'
END as "b"
from x;
The Oracle database doesn't support a BOOLEAN type so you can't return the result of the comparison directly. Here I've arbitrarily chosen to return 'YES' and 'NO' - change it to whatever you like better/best.

Related

How to optimally find all fields that contain an email address

I would like to return the column names for all columns containing an email address (... LIKE '%#%.%'). Is there a way to search fields for emails without doing it exhaustively? That is as soon as one email instance is found in the field the next is searched as oppose to looking at all its values? This is to scale better to large databases.
In the case below 'Foo' and 'Email' should be returned without searching all their records.
You could use conditional aggregation here:
SELECT
CASE WHEN SUM(Foo LIKE '%#%.%') > 0 THEN 'YES' ELSE 'NO' END AS Foo,
CASE WHEN SUM(Bar LIKE '%#%.%') > 0 THEN 'YES' ELSE 'NO' END AS Bar,
CASE WHEN SUM(Email LIKE '%#%.%') > 0 THEN 'YES' ELSE 'NO' END AS Email,
CASE WHEN SUM(Name LIKE '%#%.%') > 0 THEN 'YES' ELSE 'NO' END AS Name
FROM yourTable;

missing right parenthesis when using if

I am trying to use simple IF query in sql developer (Oracle), but i get 'missing right parenthesis' Error. I want to write all ID from my table TEST_TABLE, and write yes if ID is 1234, else 'no'. What am i doing wrong?
select id, if(id = 1234, 'yes', 'no') from TEST_TABLE t
Can't use IF there (IF is for control flow in PLSQL code, not queries), use CASE WHEN instead:
select id, CASE WHEN id = 1234 THEN 'yes' ELSE 'no' END as idIs1234 from TEST_TABLE t
The basic form of a case when is:
CASE
WHEN test THEN truevalue
[WHEN othertest THEN othertruevalue]
[ELSE falsevalue ]
END
CASE must do one or more tests that each return a single value. If no case matches (all tests are false) and there is no ELSE clause, null is returned.
There is also a form that tests a single variable for equality against various values:
CASE id WHEN 1234 THEN 'yes1' WHEN 2345 THEN 'yes2' ELSE 'no' END

Using regular expressions in conditional expressions in Postgresql v.9.6

I have a table with a column I denote with "col" containing character values. I would like to extract a column such that at the position where the column "col" starts with either '01', '02' or '03' the column will have the value "yes" and otherwise "no". I tried with the following code and can't get it to work.
SELECT
CASE WHEN col ~ '^(01|02|03)' THEN 'yes'
ELSE 'no' END
FROM table;
I know that one solution is to use the LIKE operator in the following way, but I am looking for a more compact way to solve this.
SELECT
CASE WHEN col LIKE '01%' THEN 'yes'
WHEN col LIKE '02%' THEN 'yes'
WHEN col LIKE '03%' THEN 'yes'
ELSE 'no' END
FROM table;
Any solution, even without regular expression, would be very helpful.
Thanks // Henri

SQL INNERJOIN with a switch case and if statement in select

Please help:
I have to create a column based on an expression from other columns,
but before that expression hits, there has to be a date check
if the date is before a specific date- it must do an expression, if the date is after said date, it must do another expression,
THEN: theres a unique column that has the digits 1-10 in it, each number represents a different expression.
the inner join and selecting the rows are fine, its just the switch and if expression that are beating me
basically the statement needs to look like this
select column1 if(date<neededDate)
{select case ExpressionColumn
when 1 //do stuff
when 2 // do stuff
else// do nothing
}
select column1 if(date>neededDate)
{select case ExpressionColumn
when 1 //do stuff
when 2 // do stuff
else// do nothing
}
i hope this made sense
You need two case statement nested within another case statement, it can be done like following
SELECT CASE WHEN date > neededDate THEN
CASE ExpressionColumn
WHEN 1 THEN '1'
WHEN 2 THEN '2'
ELSE 'null'
END
WHEN date < neededDate THEN
CASE ExpressionColumn
WHEN 1 THEN '1'
WHEN 2 THEN '2'
ELSE 'null'
END
ELSE 'null'
END
FROM YourTable;
you have your syntax wrong:
select case sign(datediff('s', date, neededDate)) -- precision: second
when 0 then -- case missing in your spec !
else
case ExpressionColumn
when 1 then -- case 1
when 2 then -- case 2
else -- whatever
end
end
from whatever
;
replace each comment with the appropriate expression over columns.
in your case a searched case expression might be more convenient:
select case
when (date < neededDate) then
-- note the difference: instead of 'case x when y then ...' you write 'case when b then ...'
case ExpressionColumn
when 1 then -- case 1
when 2 then -- case 2
else -- whatever
end
when (date > neededDate) then
case ExpressionColumn
when 1 then -- case 1
when 2 then -- case 2
else -- whatever
end
else -- this is missing from your spec!
end
from whatever
;
You need to check CASE..WHEN..THEN in Sql server
Simple CASE expression:
CASE input_expression
WHEN when_expression THEN result_expression [ ...n ]
[ ELSE else_result_expression ]
END
Searched CASE expression:
CASE
WHEN Boolean_expression THEN result_expression [ ...n ]
[ ELSE else_result_expression ]
END

Y or N in an SQL result

Is there a way to have a query return either a 'Y' or 'N' for a column if that column has a value above a certain number say '25'.
I am using DB2 on ZOS
SELECT CASE WHEN Col1 > 25 THEN 'Y' ELSE 'N' END As Value1
FROM Table1
For MySQL:
SELECT IF(column > 25, 'Y', 'N')
FROM table
http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function_if
There is a IF block for SQL.
http://sqltutorials.blogspot.com/2007/06/sql-ifelse-statement.html
Use the SQL CASE statement
SELECT CASE WHERE columnname > 25 THEN 'Y' ELSE 'N' END FROM table;
select
case when somecolumn > 25 then 'Y' else 'N' end as somename
from sometable;
Select If(myColumn > 25, 'Y', 'N') From myTable
Apart from the IF and CASE statements another valid alternative is to create a user defined function feeding in your column value and returning Y or N.
This has the not inconsiderable advantage that if you are selecting on this criteria in more than on SQL statement and at a later date your condition changes - to over 30 say - then you have only one place to change your code. Although using a function adds complexity and overhead so is not always optimal don't underestimate the maintenance advantage of this approach. A minor plus too is you can make the name of the function something more meaningful, and hence self documenting, than an inline CASE