Getting interval Monday-Sunday in SQL BIGQUERY - sql

I'm trying to get the data between last weeks monday and last week sunday. I'm having trouble with getting the relative part. I'm trying like this:
where date <= LASTWEEKSUNDAY OR date >= LASTWEEKMON
The closest I got to what I seek was using now(), but it returned also some days from the current week. Thanks in advance

You are describing:
where date >= date_sub(date_trunc(current_date, week(Monday), interval 1 week) and
date < date_trunc(current_date, week(Monday))
Although the function calls change, the same logic works on datetimes and timestamps.
Of course week(Monday) is the default for isoweek, so you can use:
where date >= date_sub(date_trunc(current_date, isoweek, interval 1 week) and
date < date_trunc(current_date, isoweek)

I think it's what you want
where date between DATE_SUB(DATE_TRUNC(CURRENT_DATE(), WEEK(SUNDAY)), interval 6 day) and DATE_TRUNC(CURRENT_DATE(), WEEK(SUNDAY))
You shouldn't use OR in the where statement, it'll cover all the days if you use OR. Instead, you can prefer using AND or between.

Related

Getting records between current date and next x days in Presto

Hope someone can help me out. I'm trying convert a line of SQL to work with Presto. Currently in SQL I do the following to get all records that are due in the next 0-5 days:
((EventStartDate)between getdate()-1 and dateadd(day, 5, getdate()))
I thought it would be something like this in Presto
EventStartDate between current_date and interval '5' day
But get the following error in AWS Athena: Cannot check if date is BETWEEN date and interval day to second
Thanks,
Mark
Interval needs a date or timestamp to be used and BETWEEN can only be made between to equal entities to dates twp timestamps two numbers
So do this instead
EventStartDate between current_date and current_date + interval '5' day

count the number of days of current month from day 1 until yesterday

I am trying to calculate the number of days of the current of month from day 1 until yesterday without the need of changing the count manually. The original SQL as below:
select order_id
from orders
where date > dateadd(-23 to current_date) and date < 'today'
the desired code is something like
select order_id
from orders
where date > dateadd(datediff(day,firstdayofthemonth,current_date) to current_date) and date < 'today'
Appreciate any help
In firebird you could do:
WHERE
date >= DATEADD(1 - EXTRACT(DAY FROM CURRENT_DATE) DAY TO CURRENT_DATE)
AND date < CURRENT_DATE
In addition to the answer provided by Mark, you can also use BETWEEN (starting with Firebird 2.0.4)
WHERE
date BETWEEN current_date - extract(day from current_date) + 1
AND current_date - 1
P.S. all those answers rely upon DATE data type (thus, date column and CURRENT_DATE variable) having no time part. Which is given for modern SQL dialect 3. But if Dialect 1 would get used it is not given.
https://firebirdsql.org/file/documentation/reference_manuals/fblangref25-en/html/fblangref25-commons-predicates.html
https://firebirdsql.org/file/documentation/reference_manuals/fblangref25-en/html/fblangref25-background.html#fblangref25-structure-dialects
In addition to the answer provided by GMB, you can also use fact that Firebird allows addition of days to a date without needing to use dateadd:
date > current_date - extract(day from current_date)
and date < current_date

Trying to get the first day of last month, need a Postgresql implementation

I need to calculate the first day of last month, and the last day of last month as part of a SQL query, I found the exact answer to what I am looking for in this post
for instance
select DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0) --First day of previous month
While I can follow the logic and it seems to work in SQL Server, I am using Postgresql/Redshift and I am getting the error
[42883][500310] [Amazon](500310) Invalid operation: function
pg_catalog.date_diff("unknown", integer, timestamp without time zone) does
not exist;
Can someone explain to me why Postgresql is throwing an error and how I can modify the code to get the same solution in Postgres?
Simpler in Postgres: Use date_trunc() to get the first day of the month, and then ...
subtract "one month" for the first day of the last month.
or subtract "one day" for the last day of the last month.
SELECT date_trunc('month', now()) - interval '1 month' AS last_month_first_day
, date_trunc('month', now()) - interval '1 day' AS last_month_last_day;
Returns timestamp or timestamptz, depending on input. timestamptz for now() as input.
To return type date:
SELECT (date_trunc('month', now()) - interval '1 month')::date AS last_month_first_day
, (date_trunc('month', now()))::date - 1 AS last_month_last_day;
You can subtract integer from a date (but not from a timestamp) to subtract days.
db<>fiddle here
Related:
How do I determine the last day of the previous month using PostgreSQL?
How to get the end of a day?
Second edit: okay I finally have a solution. This works in postgresql
BETWEEN DATEADD(days, (DATEPART(day, CURRENT_DATE) - 1), DATEADD(month,
-1, CURRENT_DATE)) AND DATEADD(days, (DATEPART(day, CURRENT_DATE) - 1),
CURRENT_DATE)
I figured it out. You can't put an int in the date field of a datediff or dateadd in Postgresql. If you take the SQL Server solution from above and replace the 0's in each function with two of the same date, it doesn't matter what date it is but they have to be the same, it will produce the desired output.
edit: Actually this only helps get the first of last month, not the last of last month, as I haven't figured out a way to replicate the purpose of the -1 included in the second SQL script in the link above. I can't replace this int with an equivalent date. Help would still be appreciated.

How to display SQL dates in the last 30 days?

I want to display the dates only in the past 30 days. for my SQL command. I have a DATETIME field called statement_to_date and I want to find all of the columns in the past 30 days for statement_to_date. Here's what I have so far:
SELECT Statement_TO_DATE, STATEMENT_FROM_DATE
FROM claim;
SELECT DATE_ADD(NOW(), INTERVAL -30 DAY)
I thought I could plug Statement_TO_DATE where INTERVAL is, but it's not working. Any ideas?
You are missing the where clause:
SELECT Statement_TO_DATE, STATEMENT_FROM_DATE
FROM claim
WHERE statement_to_date >= DATE_ADD(NOW(), INTERVAL -30 DAY); -- assumes the value is never in the future
Normally, when working with timespans in dates, you don't want the time component of the current date. So, this is more typical:
SELECT Statement_TO_DATE, STATEMENT_FROM_DATE
FROM claim
WHERE statement_to_date >= DATE_ADD(CURDATE(), INTERVAL -30 DAY);
Note that MySQL also has DATE_SUB(), if you don't want a negative time interval.
From w3schools you can see that you're not using DATE_ADD() correctly.
Also like Gordon Linoff said you're missing a WHERE clause, try:
SELECT Statement_TO_DATE, STATEMENT_FROM_DATE
FROM claim
WHERE statement_to_date >= DATE_ADD(day, -30, GETDATE());
Select DATEADD(Month, -1, getdate())

MySQL date_add() how to use month into this?

Hallo all, i have this SQL
SELECT DATE_ADD( '2009-'+ MONTH( NOW() ) +'-01' , INTERVAL -1 MONTH );
i can't get it to work, what i make wrong here?
tanks for help.
SELECT CONCAT_WS('-', '2009', MONTH(NOW()), '01') - INTERVAL 1 MONTH
It's the concatenation of the date that doesn't work. It converts the strings to numbers, so you get 2009+11+-1 = 2019, which then fails to convert to a date.
Instead of concatenating a date from strings, you can use the last_day function to get the last day of the current month, add one day to get to the next day of the next month, then subtract two months to get to the first day of the previous month:
select last_day(now()) + interval 1 day - interval 2 month;
Plus is an arithmetical operator, you have to use concat.
SELECT DATE_ADD( concat('2009-',MONTH(NOW()),'-01') , INTERVAL -1 MONTH )
or better
select date(now()) - interval day(NOW())-1 day - interval 1 month;
(this will also work in 2010)