How to get start date and end dates in a query from database.
Thanks,
Lico
http://sqltutorials.blogspot.com/2007/06/sql-first-and-last-day-of-month.html
Only thing that isn't covered is how to retrieve the current date... see rexem's post.
CURRENT_DATE[()] | CURDATE() | SYSDATE | TODAY = returns current date
Start of month:
DATE EXTRACT(YEAR FROM SYSDATE) +'-'+ EXTRACT(MONTH FROM SYSDATE) +'-01'
For completeness, start of month:
DATE EXTRACT(YEAR FROM SYSDATE) +'-'+ EXTRACT(MONTH FROM SYSDATE) +'-01'
End of month:
DATEADD('DAY', -1, DATEADD('MONTH', 1, DATE EXTRACT(YEAR FROM SYSDATE) +'-'+ EXTRACT(MONTH FROM SYSDATE) +'-01'))
Related
The filter below works for most months. However, in January I also want to pull December of the last year.
EXTRACT(MONTH FROM SYSDATE)-1 returns 0
and
EXTRACT(YEAR FROM SYSDATE)
How can I change this filter to make it more dynamic for every month of the year.
WHERE MONTH= EXTRACT(MONTH FROM SYSDATE)-1 AND YEAR = EXTRACT(YEAR FROM SYSDATE)
Even simpler:
WHERE month = TO_NUMBER(TO_CHAR(ADD_MONTHS(SYSDATE,-1),'MM'))
AND year = TO_NUMBER(TO_CHAR(ADD_MONTHS(SYSDATE,-1),'YYYY'))
Subtract a month before using EXTRACT:
WHERE MONTH = EXTRACT(MONTH FROM ADD_MONTHS(SYSDATE, -1))
AND YEAR = EXTRACT(YEAR FROM ADD_MONTHS(SYSDATE, -1))
Something like this, perhaps? ADD_MONTHS will subtract 1 month from sysdate (the result is DATE datatype so you have to apply to_char to it and, possibly, to_number (depending on what the month column contains)). Year, on the other hand, depends on current month - if it is January, take previous year; otherwise, take current year.
where month = to_char(add_months(sysdate, -1), 'mm')
and year = case when extract(month from sysdate) = 1 then extract(year from sysdate) - 1
else extract(year from sysdate)
end
Select *
From Orders
WHERE (
extract(day from sysdate)<=21
and
to_date(SCHEDULEDATE , 'yyyy/mm/dd') between
to_date((to_char(sysdate, 'YYYY')||'/'||cast((extract(month from sysdate)-1)as char)||'/22'),'yyyy/mm/dd') and to_date((to_char(sysdate,'YYYY')||'/'||cast((extract(month from sysdate))as char)||'/21'),'yyyy/mm/dd')
)
or
(
extract(day from sysdate)>21
and
to_date(SCHEDULEDATE , 'yyyy/mm/dd') between
to_date((to_char(sysdate, 'YYYY')||'/'||cast((extract(month from sysdate))as char)||'/22'),'yyyy/mm/dd') and to_date((to_char(sysdate, 'YYYY')||'/'||cast((extract(month from sysdate)+1)as char)||'/21'),'yyyy/mm/dd')
)
I'm trying to figure out a simple way of returning a set of date ranges based on the day of the Month. If the Day of the month of less than or Equal to I want it to return all orders the have a schedule date between the 22 of the Month before and the 21st of the Current month. If the Day of the month is greater than 21 I would like it return all orders that have a schedule date of the current month up to the end of the month. I've tried to use a case in the where with no luck. What I have now doesn't seem to work either. Any help would be appreciated.
I think this does what you want:
WHERE (extract(day from sysdate) <= 21 and
scheduledate >= add_months(trunc(sysdate, 'MON'), -1) + 21 and
scheduledate < trunc(sysdate, 'MON') + 21
) or
(extract(day from sysdate) > 21 and
trunc(scheduledate, 'MON') = trunc(sysdate, 'MON')
)
If I add 1 to extract(month from date), then does the result become 13 or 1 (January of next year)
I have the below code:
(extract(day from sysdate) >=1 and extract(month from sysdate)=12) and (targstartdate >= to_date(((extract(month from sysdate))|| '-1-' || (extract(year from sysdate)+1)) , 'mm-dd-yyyy') and targstartdate <= to_date(((extract(month from sysdate)+1)|| '-1-' ||(extract(year from sysdate)+1)) , 'mm-dd-yyyy')
You can use MOD
MOD(extract(month from sysdate)+1,12) + 1
If you are trying to get the month number of the next month, flip your logic. Instead of extracting the month number and adding 1, add one month to the date then extract the month. See the difference below. The second adds one month, then extracts the month of 1 (for January).
select extract( month from to_date('12/15/2017','MM/DD/YYYY')) + 1 from dual
union all
select extract (month from ADD_MONTHS(to_date('12/15/2017','MM/DD/YYYY'),1)) from dual;
Requirement is input date should produce first day of the month.
Condtions are:
If the date entered is between 16-nov to 30-nov, then the first day will be 16-nov.
if the date entered is between 1-nov to 15-nov , then the first day will be 01-nov.
for all other month it should return 01st day of corresponding month.
Building on Tim Biegeleisen's solution, simplifying it and avoid the date-to-text-to-date conversions. Note the use of TRUNC to get the first date of the period.
SELECT
CASE
WHEN EXTRACT(MONTH FROM DATE_COL) = 11 AND EXTRACT(DAY FROM DATE_COL) >= 16
THEN TRUNC(DATE_COL, 'MONTH') + 15
ELSE TRUNC(DATE_COL, 'MONTH')
END AS FIRST_OF_MONTH
FROM T1
I used a lengthy CASE expression to handle this, containing the logic for the three cases you mentioned in your question.
SELECT CASE WHEN EXTRACT(month FROM date) = 11 AND
EXTRACT(day FROM date) >= 16
THEN TO_DATE(EXTRACT(year FROM date) || '-11-16', 'yyyy-mm-dd')
ELSE TO_DATE(EXTRACT(year FROM date) || '-' || EXTRACT(month FROM date) ||
'-01', 'yyyy-mm-dd')
END AS newDate
FROM yourTable
Hello All
I have a table in my pg admin database.There is an employee table in this table.Having the field:-
1)name
2)date_of_birth
Now the scenario is that I want to know the birth day for current date and upcoming 20 days
For example if current date is 28-Jan-2013 then
1)from_date=28-Jan-2013
2)to_date=16-feb-2013
I want to select all the records from the table for which the
date_of_birth
lies between 28-Jan and 16-feb
Try this:
SELECT *
FROM bdaytable
WHERE bdate >= '2013-01-28'::DATE
AND bdate <= '2013-02-16'::DATE;
You may also try overlaps:
SELECT *
FROM bdaytable
WHERE (bdate, bdate)
OVERLAPS ('2013-01-28'::DATE, '2013-02-16'::DATE);
with extract, month, day:
SELECT *
FROM bdaytable
WHERE Extract(month from bdate) >= Extract(month from '2013-01-28'::DATE)
AND Extract(month from bdate) <= Extract(month from '2013-02-16'::DATE)
AND Extract(day from bdate) >= Extract(day from '2013-01-28'::DATE)
AND Extract(day from bdate) <= Extract(day from '2013-02-16'::DATE);
Incorporating Now() and interval to make the query dynamic with current date:
SELECT *
FROM bdaytable
WHERE Extract(month from bdate) >= Extract(month from Now())
AND Extract(month from bdate) <= Extract(month from Now() + Interval '20 day')
AND Extract(day from bdate) >= Extract(day from Now())
AND Extract(day from bdate) <= Extract(day from Now() + Interval '20 day');
select *
from employee
where
to_char(date_of_birth, 'MMDD') between
to_char(current_date, 'MMDD') and to_char(current_date + 20, 'MMDD')
I think this should work. Use interval.
SELECT *
FROM Employee
WHERE Date_Of_Birth >= now() AND Date_Of_Birth <= now() + interval '20 day'
If you want to get any birth date between these days (and year doesn't matter), then that would be slightly different.
EDIT
If year doesn't matter, while I'm sure there is a better way, this could work. Basically it's just converting all years to a common year (in this case 2000) -- you could do the same with your input parameter as well.
(Date_Of_Birth + (2000-EXTRACT(YEAR FROM Date_Of_Birth) || ' years')::interval)
Curious what others have used in the past as this is probably not the most efficient.
Good luck.