I am trying to retrieve some data utilising SQL's now() function. It works problem free for monthly and weekly queries. However, when I try to pull daily data, the numbers are way off. Sample code I am using:
SELECT
COUNT(distinct be.booking_id)AS "Number of Inquiries"
FROM booking_events be
WHERE be.event = 'inquire'
AND DATE_PART('day', be.created_at) = DATE_PART('day', now())
I tried figuring it out for a couple of days, but without any luck.
Hmmm, if you want today's data, I might suggest:
SELECT COUNT(distinct be.booking_id)AS "Number of Inquiries"
FROM booking_events be
WHERE be.event = 'inquire' AND
be.created_at >= current_date and
be.created_at < current_date + interval '1 day';
I don't think date_part() helps you unless you want all records from the same day of the month -- say, Jan 25, Feb 25, Mar 25, and so on.
You might be thinking of date_trunc() instead:
WHERE be.event = 'inquire' AND
date_trunc(be.created_at) = current_date
Please explain what is your problem - here's example of working query you say does not work:
EDIT:
select created_at,extract(day from created_at),DATE_PART('day', now()),extract(day from now()),now() from be where DATE_PART('day', be.created_at) = DATE_PART('day', now())
EDIT2
just in case you are trying to compare today agaist date in row, then use date_trunc, like:
select date_trunc('day',created_at), date_trunc('day',now()) from be ;
Related
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
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);
New to SQL, but trying to learn/do a job for a friend. I have a query set up that returns the number of bookings for the day. Example snippet:
...
WHERE be.event IN ('instant_approve', 'approve') AND TO_CHAR(be.created_at, 'yyyy-mm-dd') BETWEEN '2017-06-26' AND '2017-06-26';
Now, this query is set up for just today. How can I set this up so that tomorrow the query is executed for '2017-06-27' and so on? Is this possible?
Built-in function now() gives you a timestamp of the beginning of your transaction (CURRENT_TIMESTAMP pseudo-constant is its "alias", a part of SQL standard, but I prefer using the function).
Another function, date_trunc() gives you a "truncated" timestamp and you can choose, how to truncate it. E.g., date_trunc('day', now()) will give you the date/time of beginning of the current day.
Also, you can add or subtract intervals easily, so here is an example that gives you the beginning of the current and the next days:
select
date_trunc('day', now()) cur_day_start,
date_trunc('day', now() + interval '1 day') as next_day_start
;
Also, I would not use to_char() or anything else on top of created_at column -- this will not allow Postgres planner use index on top of this field. If you do want to use to_char(), then consider adding a functional index over to_char(created_at, 'yyyy-mm-dd').
Here is how I would retrieve records generated at July 26, 2017:
where
created_at between '2017-06-26' and '2017-06-27'
(implicit type casting from text to timestamps here)
This is equivalent to
where
created_at >= '2017-06-26'
and created_at <= '2017-06-27'
-- so all timestamps generated for July 26, 2017 will match. And for such query Postgres will use a regular index created for created_at column (if any).
"Dynamic" version:
where
created_at between
date_trunc('day', now())
and date_trunc('day', now()) + interval '1 day'
Use current_date built-in function in the between condition and it will work only for today's bookings.
.........
WHERE be.event IN ('instant_approve', 'approve') AND TO_CHAR(be.created_at, 'yyyy-mm-dd') =current_date;
I would like to filter out the data using a sub query in the interval function
Following is the query i use
SEL * FROM my_table WHERE MY_DATE < CURRENT_DATE- INTERVAL '30' MONTH;
The above query works, However i want to parameterize the period '30' using a sub query. Please suggest how to achieve this.
Thanks in Advance
Don't use interval calculations with year/month as it will fail, e.g. DATE '2016-12-31' + INTERVAL '30' MONTH results in 2019-06-31 (according to Standard SQL) which obviously doesn't exist.
SELECT *
FROM my_table
WHERE MY_DATE < ADD_MONTHS(CURRENT_DATE, (SELECT -col FROM tab));
If col is actually an INTERVAL you need to cast it to an INT.
How do you check if a date is within this or the prior calendar year.
I.E. December 31, This Year > myDate > January 1, Last Year
The standard SQL way is to use EXTRACT:
select *
from t
where extract(year from t.thedate) = extract(year from CURRENT_TIMESTAMP) or
extract(year from t.thedate) = extract(year from CURRENT_TIMESTAMP) - 1
Not all databases support these functions; but all do have a way of extracting the date somehow. For instance, many support the year function.
You can do something like this
SELECT * FROM YourTable
WHERE TO_DATE("FROM-DATE") < DateColumn AND TO_DATE("END-DATE") > DateColumn
This is in Oracle, pretty sure MySQL SQLite or other databases have this function,
In SQL Server you could do this:
DECLARE #yourDate DATETIME;
SELECT #yourDate = '2012-06-02';
SELECT CASE YEAR(#yourDate)
WHEN YEAR(GETDATE()) THEN 'Same Year'
WHEN YEAR(GETDATE()) - 1 THEN 'Last Year'
ELSE 'Other Year'
END AS YourColumn;
You can also easily expand this if you had to look for future dates, or more than one year in the past.
Other RDBMS will offer similar functionality to check the year for your dates.
For SQL Server, you could use DATEDIFF:
WHERE DATEDIFF(year,myDate,CURRENT_TIMESTAMP) in (0,1)
DATEDIFF(year,... effectively computes the number of year transitions between the dates (or to put it another way, the number of new year's eves at midnight).
For larger tables where indexes may help your query, we'd need to instead transform CURRENT_TIMESTAMP and compare myDate against those computed values:
WHERE myDate >= DATEADD(year,DATEDIFF(year,'20010101',CURRENT_TIMESTAMP),'20000101') AND
myDate < DATEADD(year,DATEDIFF(year,'20010101',CURRENT_TIMESTAMP),'20020101')