how can i print my own messages depending on the output of a SQL quesry.
Eg:
print "got it" when select * from emp wherempno=10; return atlest one record.
else " not presnet" when the above quesry returns 0 records
i just one one sql quesry and not a Pl/SQL code.I am using oracle 9 db.
You could try grabbing the total in a sub-query and then selectively returning the result in a case statement.
I don't have access to oracle at the moment, so the syntax may not be perfect, but something like below should work
select
case t.c
when 0 then 'not presnet'
else 'got it'
end as result
from
(select count(*) as c from emp wherempno=10) t
This works (tested on OracleXE):
SELECT CASE WHEN COUNT(1) = 0 THEN 'not present' ELSE 'got it' END
FROM emp
WHERE mpno = 10
Can you not use a ROWCOUNT to determine the number of rows affected, and then combine that with an IF statement to achieve what you are looking for?
Related
Hi I'm not sure why CASE does not work when I run this on Impala.
So this query should check if at the end of account number there is exactly the same sort code as in other column in that database - but it does not do the job. The outcome of CONCAT is exactly as expected eg '%18002' but does not find the match.
SELECT
account_no
,sort_code
,CASE WHEN account_no LIKE CONCAT("'",'%',CAST(sort_code AS STRING),"'") THEN 1 ELSE 0 END AS CHECK
,CONCAT("'",'%',CAST(sort_code AS STRING),"'") AS CONCAT_OUTPUT
FROM bddaoao01p.aml_alerts_control_flags;
the output of the query:
any idea what's the problem here?
You can try the below -
SELECT account_no,sort_code,
CASE WHEN account_no LIKE CONCAT('%',CAST(sort_code AS STRING)) THEN 1 ELSE 0 END AS CHECK
CONCAT("'",'%',CAST(sort_code AS STRING),"'") AS CONCAT_OUTPUT
FROM bddaoao01p.aml_alerts_control_flags
I am trying different ways to put a 0 in front of month less than 10.
I tried the following expression but the 0 get dropped.
What am I doing wrong?
CASE
WHEN month([Transact_Date]) < 10
THEN CONCAT(str(0),STR(month([Transact_Date]),1))
ELSE month([Transact_Date])
END AS month_w_0
Thanks!
Tom
I think a left padding trick is what you want here. Assuming your database be SQL Server:
SELECT RIGHT('00' + STR(MONTH([Transact_Date])), 2) AS month_w_0
FROM yourTable;
You don't need a CASE expression for this. In more recent versions of SQL Server, the FORMAT function might also be able to handle this.
I have tested this and it returns the result you want:
select CASE WHEN mnt < 10 THEN
concat('0' , mnt)
ELSE mnt
END AS month_w_0
from ( select month([Transact_Date]) mnt
from test_t) A
I have realised what your problem is. YOu can do it this way too:
SELECT
CASE
WHEN month([Transact_Date]) < 10
THEN CONCAT(str(0),STR(month([Transact_Date]),1))
ELSE STR(month([Transact_Date]),1)
END AS month_w_0
from test_t
The problem is only the else part and I believe that is because case when clause returns only one type od data. In your then part you have tryed to retunr string and in the else part number.
Hope this helps...
Here is a demo
I was wondering if we can change the label of the select statement like we do for data in sql select using CASE
SELECT CASE column1 = 1 THEN 1 ELSE 0 END AS [Available]
But can we have a dynamic header something like
SELECT column1 AS <-- Available when 1 or Not Available when 0
This can be handled on the front end but its wise if we have it on backend. Any help or useful link is appreciated
You can do it with dynamic sql and if...else instruction but it not make sense for me. In relational database value in the cell tells you if something is available or not. If header tells you the same as cell it's duplicate information. If you want to description of the value you can use case syntax instead of 0/1 value
SELECT CASE when column1 = 1 THEN 'Available'
ELSE 'Not available'
END AS [Available]
Well, that would not make sense, as what would you expect the column name to be if you hade 2 rows, one with 1 (being available) and the other with 0(being Not Available)?
You would have to stick to something like
SELECT
CASE
WHEN column1 = 1
THEN 'Available'
ELSE 'Not available'
END as Availability
FROM YourTable
I have created a working SQL query but it's ugly. I am using the statement:
CASE WHEN p.Guests is null THEN 0 ELSE p.Guests
About 10 times in some calculations being done in the query and I am wondering if it is possible to do something like:
variable = CASE WHEN p.Guests is null THEN 0 ELSE p.Guests
and then instead of 123 * (CASE WHEN p.Guests is null THEN 0 ELSE p.Guests)
I could do 123 * variable. p.Guests is the number of guests attending an event and it varies based on each row in the query.
So my question is: is there a way to make a variable like this in SQL?
No need for a long-winded case statement when there's
ISNULL(p.Guests, 0)
http://msdn.microsoft.com/en-us/library/ms184325.aspx
You can use ISNULL:
select 123 * ISNULL(p.Guests,0) FROM ...
The ISNULL function will return the first parameter, unless it is null in which case it will return the second parameter.
I want to compare two numbers. Let's take i.e. 1 and 2.
I've tried to write the following query but it simply doesn't work as expected (Toad says: ORA-00923: FROM keyword not found where expected):
SELECT 1 > 2 from dual
The DECODE is something like a Switch case, so how can I get the result of an expression evalutation (i.e. a number comparison) putting it in the select list?
I have found a solution using a functions instead of an expression in the SELECT LIST: i.e.
select DECODE(SIGN(actual - target)
, -1, 'NO Bonus for you'
, 0,'Just made it'
, 1, 'Congrats, you are a winner')
from some_table
Is there a more elegant way?
Also how do I compare two dates?
There is no boolean types in sql (at least in oracle).
you can use case:
SELECT CASE when 1 > 2 THEN 1 ELSE 0 END FROM dual
But your solution (decode) is also good, read here
The SIGN() function is indeed probably the best way of classifying (in)equality that may be of interest to you if you want to test a > b, a = b and a < b, and it will accept date-date or numeric-numeric as an argument.
I'd use a Case statement by preference, rather than a decode.
Select
case sign(actual-target)
when -1 then ...
when 0 then ...
when 1 then ...
end
SELECT (CASE
WHEN (SIGN(actual - target) > 0 ) THEN
'NO Bonus for you'
ELSE
'Just made it' END)
FROM dual
you can compare two dates with sql
METHOD (1):
SELECT TO_DATE('01/01/2012') - TO_DATE('01/01/2012')
FROM DUAL--gives zero
METHOD (2):
SELECT CASE
when MONTHS_BETWEEN('01/01/2012','01/01/2010') > 0
THEN 'FIRST IS GREATER'
ELSE 'SECOND IS GREATER OR EQUAL' END
FROM dual
sorry i cant format the code the formatting toolbar disappeared !
do any one know why?