How to select last 7 days from data from today's date? - sql

I know are many ways to get what I searching, actually I found the solution here on the forum, but the problem is that I don't really know why is not working, this is my try:
select * From ModeloBI.CORP.T_LOGI_RN_Planeacion_Entregas
Where ModeloBI.CORP.T_LOGI_RN_Planeacion_Entregas.Fecha_de_modificacion >= dateadd (day, -7, GetDate());
(where Fecha_de_Modificacion is the field with the date of the record)
When I execute this query I get next error:
"Column 'day' does not exist"
Do you know guys, who is this happening? or if there are other method to get what I want?
Thanks by the way, have a nice day,

Use interval:
ModeloBI.CORP.T_LOGI_RN_Planeacion_Entregas.Fecha_de_modificacion >= current_date - interval '7' day

Try this:
select * From ModeloBI.CORP.T_LOGI_RN_Planeacion_Entregas
Where ModeloBI.CORP.T_LOGI_RN_Planeacion_Entregas.Fecha_de_modificacion >= dateadd(dd,-7,getdate());

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

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

mssql select GETDATE - all day from 00:00 - 23:59

Running MS SQL Server 2008
I have this query:
select count(*) from dbo.study
where study_datetime >= (GETDATE() -1)
that comes back with all of yesterdays exams written to my study table. How would I make it come back with everything done 'today' up to the current time I asked for it? For example I would everything for today from 00:00:00.000 - current time
my values in the 'study_datetime' column look like: 2014-05-06 10:40:31.000
I can't seem to figure this one out. I have tried replacing the '-1' with a '0' but I get back 0 results.
thanks
unfortunately there is no trunc() like in oracle, but since you have the 2008 version you can use:
select count(*) from dbo.study
where study_datetime >= cast(getDate() As Date)
If I understand well (values from same day), I think you can use DATEDIFF function, using the day as datepart.
select count(*) from dbo.study
where datediff(dd, study_datetime, GETDATE()) = 0
and study_datetime <= GETDATE() -- if you need a check for the "future" (datetime after GETDATE() )
To get everything that strictly happened today, just use:
select count(*) from dbo.study
where study_datetime >= cast(getDate() As Date)
and study_datetime < cast(DATEADD(day,1,getdate()) as Date)
When you're working with continuous data, it's almost always better to switch to using semi-open intervals, to ensure that data falls into one and exactly one interval. Usually, when you want "all day", you don't want to exclude things that occurred during the final minute of the day (at e.g. 23:59:37.223 or even at 23:59:59.993). So you'd normally write your query to be >= midnight at the start of the day and < midnight at the start of the following day (note the different types of comparisons)
This is usually a far better idea than trying to compute the last moment of today and use <= (or BETWEEN) for your comparisons.
select count(*) from dbo.study
where study_datetime between :2014-05-06 00:00:00 and :2014-05-06 23:59:59.
It might help you

SQL Query to find out records created in last hour (working with epoch datetime format)

I want to find out the records created in last one hour.
Here is my WHERE clause
WHERE Create_DateTime > (DATEDIFF(s,'1970-01-01 00:00:00', GETDATE())-(3600))
The Create_DateTime is in epoch integer format.
It does run but I do not get the expected result.
Anyone knows a proper query for this?
Thanks
I am not very familiar with SQL functions but I tried GETUTCDATE() in place of GETDATE() and it gave me the expected result.
GETDATE() is giving me the time that is 8 hours earlier from my current time. I am in PST.
please try
SELECT * FROM myTable WHERE
Create_DateTime > DATEDIFF(minute,CAST(yourvarcharcolumn as datetime),GETDATE())<=60
Wouldn't this work ?
select * from myTable where Datediff(minute, Create_DateTime, getutcdate()) <= 60