I have 2 columns:
DayOfMonth (which is simply an integer between 1 and 31)
Date
I'm trying to write a CASE statement which returns true if the DayOfMonth has occurred in between 'Date' and today's date.
For example, if DayOfMonth = 18 and Date = '2020-04-14', the CASE statement would return True because the 18th April occurred between today and 14th April.
Thanks
Most databases support a day() function to return the date of a month.
where (DayOfMonth >= day('2020-04-14') and
DayOfMonth <= day(current_date)
) or
'2020-04-14' <= datedd(month, -1, current_date)
Some databases spell day() as extract(day from <date>). Some have alternative ways of writing current_date.
EDIT:
This is more complicated than I was thinking, because the day of month could be 31 -- but a month may not have 31 days. The following does not handle handle February, but it gives the idea:
where -- first the trivial case
(dayOfMonth <= day(current_date) and
(#date <= date_trunc('month', current_date) or
dayOfMonth >= day(#date)
)
) or
-- then the previous month case
(date_trunc('month', #date) = date_trunc('month', current_date) - interval '1 month' and
dayOfMonth >= day(#date) and
dayOfMonth <= day(last_date(#date))
) or
-- Days at the end of month but not necessarily in the previous month.
#date < date_trunc('month', current_date) - interval '1 month'
Related
How to query (SELECT) in Postgresql, so that the results of a column with different dates, are between Sunday and Saturday of the current week.
Query fake example:
SELECT * FROM table WHERE datecolumn BETWEEN CURRENT WEEK
In another query, I have the number of the week in the year. How to make a SELECT for these dates, applying in the WHERE clause the specific week number in the specific year.
Query fake example:
SELECT * FROM table WHERE datecolumn BETWEEN WEEK15 FROM year 2020
Perhaps you can use something like this:
SELECT *
FROM table
WHERE
EXTRACT(week FROM datecolumn) = EXTRACT(week FROM NOW())
AND
EXTRACT(isoyear FROM datecolumn) = EXTRACT(isoyear FROM NOW())
The week is ISO-8601 week number. By definition, ISO weeks start on Mondays and the first week of a year contains January 4 of that year. In other words, the first Thursday of a year is in week 1 of that year.
In the ISO week-numbering system, it is possible for early-January dates to be part of the 52nd or 53rd week of the previous year, and for late-December dates to be part of the first week of the next year.
For example, 2005-01-01 is part of the 53rd week of year 2004, and 2006-01-01 is part of the 52nd week of year 2005, while 2012-12-31 is part of the first week of 2013.
It's recommended to use the isoyear field together with week to get consistent results.
If you need custom (non-ISO) week numbering - you will have to craft your own calculation.
I would recommend the following pair of conditions:
where
date_column >= current_date - extract(dow from current_date) * interval '1 day'
and date_column < current_date - (extract(dow from current_date) - 8) * interval '1 day'
Postgres' date_trunc(week, ...) starts weeks on Monday, so we need something a little more complicated, using extract(dow from ...), which returns 0 on Sundays.
The advantage of this approach is that it is SARGeable, since no function is applied to the column being filtered. This means that this would happily take advantage of an index on the date column.
I would use date_trunc(), but like this. For the current week:
where datecolumn >= date_trunc('week', now()) and
datecolumn < date_trunc('week', now()) + interval '1 week'
For the nth week of the year, this is trickier. I think this does what you want:
where datecolumn >= (date_trunc('week', now()) -
(extract(week from now()) - 1) * interval '1 week' +
<n> * interval '1 week'
) and
datecolumn < (date_trunc('week', now()) -
(extract(week from now()) - 1) * interval '1 week' +
(<n> + 1) * interval '1 week'
)
Both of these are structured so the computations are NOT on the columns, so they are compatible with using indexes.
I might not be asking this right, but basically I need a query that when ran, returns all records entered from the 1st till the 15th of the current month. And when the 15 passes only return the records from the 16th till the end of the current month.
I've tried to build something like this but its for bigquery and not sql, and I can't seem to find something similar for mssql 2016.
select sample_id
from dbo.table
WHERE date_entered BETWEEN DATE_ADD(CURRENT_DATE(), -15, 'DAY') AND CURRENT_DATE()
or
WHERE date_entered BETWEEN CAST(eomonth(GETDATE()) AS datetime) AND CURRENT_DATE()
Regardless of the today's date, I need the 1st till today, until the 15th. Then the 16th till today, until the end of the month. Sorry I'm new to SQL.
UPDATE: I was able to solve this issue with the example provided by #GordonLinoff . Thank you Gordon!
SELECT rowguid, ModifiedDate
FROM [AdventureWorks2017].[Person].[Person]
WHERE Year(ModifiedDate) =Year(getdate()) and month(ModifiedDate) =month(getdate()) and
((day(getdate()) <= 15 and day(ModifiedDate) <=15))
Or
((day(getdate()) >= 16 and day(ModifiedDate) >=16))
The description of your logic is a bit hard to follow, but you seem to want something like this:
where date_entered >= datefromparts(year(getdate(), month(getdate(), 1)) and -- this month
(day(getdate()) <= 15 or
day(getdate()) > 15 and day(date_entered) > 15
)
This was MySQL:
SELECT *
FROM dbo.table
WHERE date BETWEEN CASE WHEN DAY(CURRENT_DATE) <= 15
THEN DATE_FORMAT(CURRENT_DATE, '%Y-%m-01')
ELSE DATE_FORMAT(CURRENT_DATE, '%Y-%m-16')
END
AND CASE WHEN DAY(CURRENT_DATE) <= 15
THEN DATE_FORMAT(CURRENT_DATE, '%Y-%m-15')
ELSE LAST_DAY(CURRENT_DATE)
END
Big Query:
SELECT *
FROM table
WHERE date BETWEEN CASE WHEN EXTRACT(DAY FROM CURRENT_DATE) <= 15
THEN DATE_TRUNC(CURRENT_DATE, MONTH)
ELSE DATE_ADD(DATE_TRUNC(CURRENT_DATE, MONTH), INTERVAL 15 DAY)
END
AND CASE WHEN EXTRACT(DAY FROM CURRENT_DATE) <= 15
THEN DATE_ADD(DATE_TRUNC(CURRENT_DATE, MONTH), INTERVAL 14 DAY)
ELSE DATE_ADD(CURRENT_DATE, INTERVAL 31 DAY)
END
This should give
date between 1 and 15
or date between 16 and last_of_the_month
I've tried to build something like this but its for bigquery
Whatever example you use - it is not working in BigQuery either!
Below is working example for BigQuery Standard SQL and uses some "tricks" to avoid using redundant code fragments
#standardSQL
SELECT sample_id
FROM `project.dataset.table`,
UNNEST([STRUCT(
EXTRACT(DAY FROM date_entered) AS day,
DATE_TRUNC(date_entered, MONTH) AS month
)])
WHERE DATE_TRUNC(CURRENT_DATE(), MONTH) = month
AND IF(
EXTRACT(DAY FROM CURRENT_DATE()) < 16,
day BETWEEN 1 AND 15,
day BETWEEN 16 AND 99
)
I'm trying to generate dynamic dates based on the current date. I want to use generate_series() to populate dates between start and end dates (interval = 1 day).
If current date is before 10/1, start date is 10/1 in previous year
If current date is after 10/1, start date is 10/1 in the current year
end date is 9/30 in year 4. For example,
current date = 5/22/2019 -> start date = 10/1/2018, end date = 9/30/2021
current date = 11/1/2019 -> start date = 10/1/2019, end date = 9/30/2022
select generate_series(
to_date(cast(start_date as text), 'yyyy-mm-dd'),
to_date(concat(extract(year from to_date(cast(start_date as text), 'yyyy-mm-dd')+3),'-','09','-', 30), 'yyyy-mm-dd'),
'1 day'
)
from (
select case
when extract(month from current_date) <= 10 then concat(extract(year from current_date) -1,'-',10,'-', '01')
when extract(month from current_date) > 10 then concat(extract(year from current_date),'-',10,'-', '01')
end) as start_date
ERROR: invalid value "(202" for "yyyy"
DETAIL: Value must be an integer.
SQL state: 22007
It's complaining about year isn't integer. Which parts do I need to modify to run this query?
select case
when date_trunc('month', current_date) ::date < make_date(extract(year from current_date) ::int, 10, 1) then
generate_series(make_date((extract(year from current_date) - 1) ::int, 10, 1)
,make_date((extract(year from current_date) + 2) ::int, 10, 1) - 1
,'1 day') ::date
else
generate_series(make_date(extract(year from current_date) ::int, 10, 1)
,make_date((extract(year from current_date) + 3) ::int, 10, 1) - 1
,'1 day') ::date
end as dt;
Here In place of current_date you can use as below: current_date => '11/1/2019'::date or '05/22/2019'::date
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')
)
I used following query to get current week
select extract(week from current_timestamp) and it is showing 34 thats fine.
But how do i get the current week number of the current month.
You can find the first day of this week with:
trunc('week', current_date)
And the first day of the first week of this month with:
trunc('week', date_trunc('month', current_date))
Subtract the two to find the in-month weeknumber:
extract('day' from date_trunc('week', current_date) -
date_trunc('week', date_trunc('month', current_date))) / 7 + 1
Try the following
SELECT to_char('2016-05-01'::timestamp, 'W');
You can find week number by using day as well like:
select ((date_part('day', current_date)::integer - 1) / 7) +1;
Im not sure on how does this work on postgreSql (http://www.w3cyberlearnings.com/PostgreSQL_DATE_PART)
but in sql server the example is something like this...
>SELECT date_part('week', date)
This will give accurate results
CREATE OR REPLACE FUNCTION week_no(date)
RETURNS integer AS
$BODY$
SELECT
CASE WHEN EXTRACT(DAY FROM $1::TIMESTAMP)::INTEGER = 1
THEN 1
ELSE extract(week from $1:: TIMESTAMP)::integer
- extract(week from ( date_trunc('month', $1::TIMESTAMP) + interval '1 day' ) )::integer + 1
END
$BODY$
LANGUAGE sql IMMUTABLE STRICT
COST 100;
Example: week_no('2017-01-01') = 1
Maybe late.. but this works too
where cast(call_start as Date) = Date 'today'
or cast(call_start as Date)= Date 'today' -1
or cast(call_start as Date)= Date 'today' -2
or cast(call_start as Date)= Date 'today' -3
or cast(call_start as Date)= Date 'today' -4
or cast(call_start as Date)= Date 'today' -5
or cast(call_start as Date)= Date 'today' -6
or cast(call_start as Date)= Date 'today' -7
;
this gives me it