How to display SQL dates in the last 30 days? - sql

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

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.

BigQuery SQL WHERE Date Between Current Date and -15 Days

I am trying to code the following condition in the WHERE clause of SQL in BigQuery, but I am having difficulty with the syntax, specifically date math:
WHERE date_column between current_date() and current_date() - 15 days
This seems easy in MySQL, but I can't get it to work with BigQuery SQL.
Use DATE_SUB
select *
from TableA
where Date_Column between DATE_SUB(current_date(), INTERVAL 15 DAY) and current_date()
Remember, between needs the oldest date first
You should probably switch the two around - the syntax should be the following:
WHERE date_column BETWEEN DATE_ADD(CURRENT_DATE(), -15, 'DAY') AND CURRENT_DATE()
This works for me.
WHERE DATE(date_column) BETWEEN DATE(DATE_ADD(CURRENT_DATE(), -15, 'DAY'))
AND CURRENT_DATE()

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 with date intevals

Hi I need help to create a query that return a result based on date interval but I can't get it to work correctly.
I would like to achieve a result giving me the records with a date that are within a historic time span:
day -1 to -7 */from yesterday and -7 days */
day -8 to -14 */the date is between -8 and -14 days from today
For the first interval I use this where clause:
...
where `invoiceExpDate` >= date_add(now(), INTERVAL - 7 DAY)
how can I modify this to NOT give me the records for today??
For the second interval I use:
...
where datediff(invoiceExpDate,now())<= 14
AND datediff(invoiceExpDate,now())> 7
AND `invoiceExpDate` > now()
I can't get them to work. CAn you help me with the correct where clause to return what I want?
Thanks
I think you can combine date_add() and BETWEEN
For your first clause
...
WHERE `invoiceExpDate` BETWEEN
date_add(now(), INTERVAL - 7 DAY) AND
date_add(now(), INTERVAL - 1 DAY)
Similar pattern for the second.
LogDate BETWEEN DATEADD(dd, -7, GETDATE()) AND
DATEADD(dd, -1, GETDATE())

DateDiff in SQL Server asking for help

I am using SQL Server 2008. I have a table which has a datetime type column called CreateTime. I need to select all the records from this table whose CreateTime is more than 3 days and half an hour from current time (UTC time). I am using T-SQL store procedure.
BTW: The CreateTime column is some time in the past time.
I have taken quite some time to learn and search for help from MSDN for DateDiff, but cannot figure out. Could anyone show me a sample please?
thanks in advance,
George
You can select and add a WHERE clause with a DATEDIFF using minutes:
SELECT (fields)
FROM (table)
WHERE
DATEDIFF(MINUTE, CREATETIME, getutcdate()) <= (3*24*60 + 30)
And of course, if you only wants those rows which are MORE than 3 days and 30 minutes away, just use the opposite:
WHERE
DATEDIFF(MINUTE, CREATETIME, getutcdate()) > (3*24*60 + 30)
A sample:
SELECT
DATEDIFF(MINUTE, '2009-08-01 08:00:00', getutcdate()),
DATEDIFF(MINUTE, '2009-07-31 20:00:00', getutcdate()),
DATEDIFF(MINUTE, '2009-07-23 20:00:00', getutcdate())
gives as result:
96 816 12337
So the first two dates are still within your 4350 minute bracket (less than 3 days and 30 minutes ago), while the third date is further away.
Marc
You need DATEADD:
WHERE DATEADD(minute, 4350, CreateTime) <= getutcdate()
Or, as you mentioned, you can use DATEDIFF:
WHERE DATEDIFF(minute, CreateTime, getutcdate()) <= 4350
(4350 is '3 days and 30 minutes' in minutes)
One minor quibble with the given answers, though they are correct. Don't apply the function to the column: apply the function to the comparison value.
If CREATETIME is indexed then it's a scan rather than seek with the function on the column.
You don't need millions of rows for this to be a problem.
Adapting the answer of marc_s:
SELECT (fields)
FROM (table)
WHERE
CREATETIME <= DATEADD(MINUTE, - (3*24*60 + 30), getutcdate())