Current week number for current month - sql

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

Related

Can you use logical operators in BigQuery to select different dates depending on current date?

At my work we run a report a couple times a week to pull some information from BigQuery.
We run the report every Monday and Thursday.
I'd like to automate the report to run on these days and want to know if I can put in some logic so that if I run the report on a Monday, it runs the data for the previous business week (Sunday - Saturday), and if I run the report on a Thursday, it runs the report for the current business week so far (Sunday - Wednesday).
On another report where I only run the report for previous week I use:
select last_day(current_date - 14, week(monday)) as lw_week_start, last_day(current_date - 7, week(sunday)) as lw_week_end
And to get the current week dates I can use:
select last_day (current_date -7, week(monday)), (current_date -1)
So can I put both of these in my query, and use some sort of logic to say, if I run on a Monday use the first one, if I run on a Thursday, use the second one?
Thanks
You can define the period as a CTE (or if you prefer as variables) and then use that information in the query:
with period as (
select (case when extract(dayofweek from current_date) = 2
then last_day(date_add(current_date, interval -14 day), week(monday))
when extract(dayofweek from current_date) = 5
then last_day(date_add(current_date, interval -7 day), week(monday))
end) as lw_week_start,
(case when extract(dayofweek from current_date) = 2
then last_day(date_add(current_date, interval -7 day), week(sunday)
when extract(dayofweek from current_date) = 5
then date_add(current_date, interval -1 day)
end) as lw_week_end
)
select . . .
from period cross join
. . .
Notes:
This only includes Mondays and Thursdays. I imagine you want to extend this to the other days of the week.
current_date is the current date UTC. You might want to include your timezone:
select current_date('America/New_York')
/* if current_date is monday then it will return previous week report
else it will give report for present week for any other current_date */
IF (EXTRACT (DAYOFWEEK FROM CURRENT_DATE)) = 2 THEN
select last_day(current_date - 14, week(monday)) as lw_week_start, last_day(current_date - 7, week(sunday)) as lw_week_end;
ELSE
select last_day (current_date -7, week(monday)) as week_start, (current_date -1) as previous_day ;
END IF
Scripting on BigQuery
BigQuery Date fuctions
Simply add below to your where clause
where your_date_column in unnest(
case extract(dayofweek from current_date())
when 2 then generate_date_array(last_day(current_date() - 14, week(monday)), last_day(current_date() - 7, week(sunday)))
when 5 then generate_date_array(last_day (current_date() - 7, week(monday)), current_date() - 1)
end
)

Has a particular 'DayOfMonth' occurred in between 'Date' and today?

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'

MSSQL query for all records between two date range of current day

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
)

invalid value "(202" for "yyyy" error in generate_series() with dates in PostgreSQL

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

Returning a Date Range based on Day of the Month

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')
)