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

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

Related

Getting interval Monday-Sunday in SQL BIGQUERY

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.

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.

H2 Get Date 1 day old from current time

I'm currently working on a really fun problem. I want to get a date that is one day old (from current date) and then compare it to now.
The exact way to do this in PostreSQL is this:
select * from table WHERE date < now() - '1 day'::interval;
How do I do this in H2 JDBC? Does anybody know?
Grateful for any assistance!
Simply subtract the number of days from current_date
select *
from the_table
where the_date_column < current_date - 1;
The above would work in Postgres just as well.
You can try the DATEADD function. It works for addition and subtraction:
select * from table WHERE date < DATEADD('DAY', -1, CURRENT_DATE);

How to find records from yesterdays time till todays time in sql?

I am trying to find records from yesterdays 10:30 PM till today's 10:30 PM with SQL query. Please help me with sql query to find such records.
Maybe its a duplicate question, if so please link me to that. Don't want any pl-sql function.
A simple way to do this is to subtract times and compare dates. So, one way is:
select t.*
from t
where trunc(datecol) = trunc(sysdate - 1.5/24);
It is more efficient to use a direct comparison (because Oracle can more readily use an index):
select t.*
from t
where datecol >= trunc(sysdate) - 1.5/24 and
datecol < trunc(sysdate) + 1 - 1.5/24;
Note: You can also use interval for this purpose, if you are less old-fashioned than I am:
select t.*
from t
where datecol >= trunc(sysdate) - interval '90' minute
datecol < trunc(sysdate) + interval '1' day - interval '90' minute;
You can get the yesterday date with SYSDATE - 1. You would need something like this:
SELECT ...
FROM ...
WHERE date_field BETWEEN SYSDATE-1 AND SYSDATE

Retrieve a date record which is with in a 30 days range by given date SQL

I am having trouble to find the records which are with in a 30 days range in a given date
Ex: I have record with the date of 10/31/2014 and I have a given specific Due date of 11/28/2014.
I would like to retrieve this record(10/31/2014) when my current date is 10/28/2014 until my current date becomes 11/28/2014 (i.e with in 30 days range). If my current date is 11/29/2014 then I no need retrieve this record.
I have spend almost 3 hours of my time. It will be greatly appreciated if you can give me a query for it.
Thanks,
VJ.
The general format is something to the effect of:
where duedate >= CURRENT_DATE - interval '30' day and duedate <= CURRENT_DATE
This is standard syntax and will work in MySQL and Postgres. An Oracle equivalent is:
where duedate >= trunc(sysdate) - 30 day and duedate <= trunc(sysdate)
And a SQL Server equivalent is:
where duedate >= cast(getdate() - 30 as date) and duedate <= cast(getdate() as date)