I currently have two SQL statements that I'd like to combine into one, and set a status if it meets each one. I've posted some massively stripped down code below:
select * from database where date1 < SYSDATE
select * from database where date2 < SYSDATE
Ideally, I'd like it so if it meets the criteria in the first statement, it'd set a flag of 'status1' and if it doesn't meet that criteria, but meets the second statement's criteria I'd like to set a flag of 'status2' - hope that makes sense!
For example the data would be like: Name | ID | Status
Thanks :)
Give this a shot:
select Name, ID, CASE
WHEN date1 < SYSDATE THEN 1
WHEN date2 < SYSDATE THEN 2
END as Status
from mytable
where date1 < SYSDATE or date2 < SYSDATE
select *
from (
select name, id, 'status1' as status
from some_table
where date1 < SYSDATE
UNION ALL
select name, id, 'status2'
from some_table
where date2 < SYSDATE
)
UPDATE mytable
SET flag =
CASE
WHEN date1 < SYSDATE THEN
'status1'
ELSE
'status2'
END
WHERE date1 < SYSDATE
OR
date2 < SYSDATE
Since you said you wanted to UPDATE the status...
UPDATE database
SET flag = CASE WHEN date1 < SYSDATE THEN 'status1' WHEN date2 < SYSDATE THEN 'status2' ELSE NULL END
Related
I'm trying to select phone number that is between date_from and date_to
or the date_to is null
but the problem in the Or part it neglects the other conditions (where pho.parent_id =:p_id and pho.phone_type='M' and rownum <=1 )
select pho.PHONE_NUMBER
from per_phones pho
where pho.parent_id = :p_id and
pho.phone_type='M'
and
rownum <=1 and sysdate > pho.DATE_FROM and sysdate < DATE_TO or DATE_TO is null;
Put your OR condition within braces
select pho.PHONE_NUMBER
from per_phones pho
where pho.parent_id = :p_id and pho.phone_type='M'
and rownum <=1 and
(sysdate > pho.DATE_FROM and sysdate < DATE_TO or DATE_TO is null)
I think your DATE_TO is null in case your record is valid from DATE_FROM till end of time.
You will still need to check DATE_FROM condition and also put rownum condition in the outer query so you need following:
Select PHONE_NUMBER from
(select pho.PHONE_NUMBER
from per_phones pho
where pho.parent_id = :p_id
and pho.phone_type='M'
and rownum <= 1 -- this is not needed here
and sysdate > pho.DATE_FROM
and (sysdate < DATE_TO or DATE_TO is null)
)
Where rownum <= 1
Cheers!!
I am trying to get the row with maximal negative date difference ( date - Now() for example ) from a table.
Something like this ( pseudo code ):
SELECT ID, MAX( DateDiff("d", Now(), myDate) ) AS [test] FROM myTable
WHERE test < 0;
The result would be something like this:
ID | test
1 | -15
I have tried to solve this on my own but have failed. I do not even know how to phrase the search question on Google, so Internet can not help me. I am just stuck.
NOTE:
Maximal negative number example: between -10 and -125, result should be -10.
EDIT:
Trying out on my own, I was able to come close:
SELECT MAX( DateDiff("d", myDate, Now()) as [test] from myTable
WHERE DateDiff("d", myDate, Now() < 0;
It returns correct date difference, but when I try to add ID as well, it returns date difference for all the rows. I can not get single value.
SELECT TOP 1
ID,
DateDiff('d', Now(), myDate) AS [test]
FROM myTable
WHERE Now() > myDate
ORDER BY 2 DESC;
Use ORDER BY and TOP 1.
Something like this.
SELECT TOP 1 ID, myDate, DateDiff(Day, now(), myDate) AS [test]
FROM myTable
WHERE DateDiff(Day, now(), myDate) < 0
ORDER BY DateDiff(Day, now(), myDate) DESC -- min differenz
-- ORDER BY DateDiff(Day, now(), myDate) ASC -- max differenz
I need to add a case statement in a where clause. I want it to run either statement below depending on the value of TermDate.
Select *
from myTable
where id = 12345
AND TermDate CASE
WHEN NULL THEN
AND getdate() BETWEEN StartDate AND DATEADD(dd, 30, StartDate)
ELSE
AND GETDATE < TermDate
END
Why not just use an OR condition?
SELECT *
FROM myTable
WHEN id = 12345
AND ((TermDate IS NULL AND
getdate() BETWEEN StartDate AND DATEADD(dd, 30, StartDate)) OR
GETDATE() < TermDate)
Since we all posted three exact answers, obviously too much, here a version that uses your case when construction.
use this:
select *
from myTable
where id = 12345
AND case
when TermDate IS NULL
AND getdate() BETWEEN StartDate AND DATEADD(dd, 30, StartDate)
then 1
when GETDATE < TermDate
then 1
else 0
end
= 1
You can accomplish this using ANDs and ORs. Try the following query.
Select *
From myTable
where id = 12345
AND ((TermDate IS NULL
AND GETDATE() BETWEEN StartDate AND DATEADD(dd, 30, StartDate))
OR (GETDATE() < TermDate))
I try to count record which is on current date and pass date from date column
DATE
'2013-03-04 00:00:00'
'2013-02-04 00:00:00'
'2013-02-04 00:00:00'
if today is '2013-03-04 00:00:00'
the result should be
CURRENT_DATE = 1
PASS_DATE = 2
I Don't know how to query it form one resource but different where condition
1st - date >= '2013-03-04 00:00:00'
2nd - date < '2013-03-04 00:00:00'
PLEASE HELP
I suggest using two sub-queries within a SELECT clause
select
(select count(*) from MyDates where DateValue < getdate()) as PastDate,
(select count(*) from MyDates where DateValue >= getdate()) as CurrentDate
You can replace getDate() with a date parameter or a hard-coded value such as '2013-03-04' if you wish.
One way to do it is to query for all dates and place 1\0 according to your condition
then just query again from the table an sum the columns.
SELECT sum([CURRENT_DATE]), sum([PASS_DATE])
FROM
(
SELECT
ID,
case when myDate >= getDate() then 1 else 0 end as [CURRENT_DATE],
case when myDate < getDate() then 1 else 0 end as [PASS_DATE]
FROM mytable
) as SourcTbl
Here is a SQL Fiddle http://sqlfiddle.com/#!3/e9c19/8
If I have table with a Date column (Date field) called created_date, with values like "9/2/2010 5:25:42 PM".
I want to select all rows from a start_date to a end_date. However, the end_date may be null. In this case, I want to select all rows where created_date is greater than end_date.
Since toDate (which can be null) is a host variable, it's easier than the solutions already given (which are all wrong in that regard, btw)
select * from mytable
where created_date between v_fromdate
and nvl(v_todate, to_date('31.12.9999','dd.mm.yyyy'));
select *
from TABLE
where created_date >= '2010-09-02' and (created_date is NULL or created_date <= '2010-09-03')
Why just use a simple SQL query for that, like this one:
select xxx from table_names where created_date is null or (created_date >= to_date("02/09/2010", "dd/mm/yyyy") and created_date <= to_date("03/09/2010", "dd/mm/yyyy"));
Edit
You can define a query like the one defined by ammoQ, i.e. something like that:
select xxx from table_names where created_date is null or created_date >= start_date and created_date <= nvl(end_date, to_date("31/12/9999", "dd/mm/yyyy"));
However, as you are using PL/SQL, you can check the nullability of end_date parameter:
IF end_date IS NULL THEN
select xxx from table_names where created_date is null or created_date >= start_date;
ELSIF
select xxx from table_names where created_date is null or created_date >= start_date and created_date <= end_date;
END IF;
Note that you can remove the created_date is null condition if created_date is not a nullable column...
select * from yourtable
where created_date >= #StartDate AND created_date <=ISNULL(#EndDate,created_date)
SELECT *
FROM A_TABLE
WHERE CREATED_DATE >= &START_DATE AND
(CREATED_DATE <= &END_DATE OR
&END_DATE IS NULL)
If i took it right from your question
this should work:
SELECT *
FROM yourTable
WHERE created_date >= to_date('01.09.2010', 'dd.mm.yyyy')
AND (end_date <= to_date('02.09.2010', 'dd.mm.yyyy')
OR end_date IS NULL);