Using Dates as a SQL Filter - sql

I have a table of data that goes quite some time back, but I only want to results in my query for the last 13 weeks. There is a date column in that table.
I can use
SELECT DATEADD(Week, -13, GETDATE())
to get the date of 13 weeks back as a separate query - but I am having trouble linking that into the initial select to only return me the last 13 weeks of data. I have used that query as the data should refresh every day to go back 13 weeks to that date.
Is there any way I can do that?
Thanks in advance

This should be what you are looking for:
SELECT *
FROM TABLE_NAME
WHERE date_field >
(SELECT DATEADD(Week, - 13, GETDATE()))

I'm a bit confused on what your issue is. You should be able to use the dateadd() in your where clause:
SELECT *
FROM TABLE
WHERE DATECOLUMNTOCOMPARE > DATEADD(WEEK,-13,GETDATE())

Related

Databricks SQL syntax for previous six months in where statement

I'm trying to figure out how to look for data in the last six months in the where statement of a SQL query in Databricks, but I'm having a lot of issues with the syntax.
Right now I have:
Select * from table
where datediff(add_months(date_column, -6), date_column) = 1
The query doesn't throw an error, but returns no results.
I think you're expecting the wrong thing from datediff. Datediff tells you the number of days between two dates. In your case, you're comparing your date_column to your date_column - 6 months. That's always going to be 6 months or ~180 days.
Try this.
WHERE date_column > DATEADD(MONTH, -6, CURRENTDATE())
AKA, where your date column is greater than the current date minus 6 months.

Pulling records based on Date in JDBC

How can I return all customers whose last appointment_date was 11 months ago from the date today? Or 12 months ago from the date in a month from today?
The first statement is working where I get the appointment from_date and compare it to the current date and return all the appointments that are happening tomorrow:
SELECT appointment.id,
appointment.from_date
WHERE (julianday('now') - julianday(appointment.from_date)) = 1
But for the second statement I cant figure out how to return all customers whose last appointment date was 11 months ago from the current date?
SELECT customer.id,
customer.last_appointment_date
FROM customer
WHERE datediff(month, customer.last_appointment_date, DATEADD(month, getDate())) = 12
datediff() doesn't work because I am using SQLite and it is not a recognised function.
Any help would be greatly appreciated.
EDIT: I am running these query's in my code in netbeans i am using the sqllitejdbc driver to run them through prepared statements
I have edited, its because i am running through netbeans, everytime i use datediff(month, customer.last_appointment_date, DATEADD(month, getDate())) = 12 it returns month not a valid column - it doesnt recognise it as a valid date part?
returned: Caused by: java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (no such column: month)
Calculate the date to compare your field with, instead of calculating the difference and comparing to a constant, that way the database can make use of an index to locate records.
Use the date function instead of dateadd (see SQLite equivalent of SQL Server DateAdd function):
SELECT customer.id,
customer.last_appointment_date
FROM customer
WHERE customer.last_appointment_date = date('now', '-11 month')

Month to date query

How do I make a query run for current month and current day of that month?
For example :
Select
installed_date,
cust_no,
sum(Price) as daily_price
From
table1
Group By
installed_date,
cust_no
I want this query to always return current month's data so in this case from 1st Dec 2013 till 3rd Dec 2013.
Thanks
select installed_date,cust_no,sum(Price) as daily_price
from table1
where installed_date >= dateadd(month, datediff(month,0, current_timestamp), 0)
group by installed_date,cust_no
Click on this link
Check above link.
I have created a table with three column. Schema of same is added to this link.
Also wrote a query which will resolve your problem. Have a look. I am not mysql developer but tried to convert mssql query to mysql. If you want MSSQL query then revert.

MS Access SQL query for testing dates for last day of the quarter

OK, so I have a load of records in a table and they have many different dates.
I want to retern only those records whose date falls on the last day of whatever quarter it's in.
I.e. I basically need the equivalent of a lastDayOfQuarter(date) function that calculates the date that is the last day in the quarter for the date passed to it.
e.g. lastDayOfQuarter(#16/05/2013#) = #30/06/2013#
My query might look like:
SELECT * FROM mytable
WHERE mytable.rdate = lastDayOfQuarter(mytable.rdate);
This query will be run over PDO so no VBA allowed. Native MS Access only.
I would also prefer to not use string manipulation as there is a difference between US and EU dates which might cause issues down the line.
I'm answering myself as, with the help of HansUp answering a previous question of mine for finding month-end records, I found out quite an easy way to acheive this:
WHERE DateValue(m.rdate) = DateSerial(Year(m.rdate), Month(m.rdate) + 1, 0)
AND Month(m.rdate) IN(3,6,9,12)
the "last day of the quarter" could be different for different users. You may be best to build a table of "lastdays" based on your business rules, then use that table in your query.
And here as short answer...Try
Select DateAdd(day, -1, dateadd(qq, DATEDIFF(qq, 0, 'year-month-day'), 0))
For today it should give you
2013-06-30 00:00:00.000
SO for your Table you should use :
SELECT * FROM mytable
WHERE mytable.rdate = DateAdd(day, -1, dateadd(qq, DATEDIFF(qq, 0, mytable.rdate), 0));

SQL Select data by this week

Hi how do I get the data by current week?
Select * from Transaction where transactionDate ....
In SQL Server based on week of year. Please see DATEPART for ##DATEFIRST etc. for example, this is all trades since Sunday in US/UK settigs:
WHERE DATEPART(week, transactionDate) = DATEPART(week, GETDATE())
Edit:
For Access, use this DatePart and use "ww" for the part of date you want.
In answer to the comment, "week" is not a variable; it's the bit of the date you want
So:
WHERE DatePart("ww", transactionDate) = DatePart("ww", GETDATE())
In Microsoft Access
Last n days:
SELECT *
FROM Transaction
WHERE transactionDate >=Date()-7
If you have indexes and this type of difference suits, it will be faster because it is sargable
This week by week difference:
SELECT *
FROM Transaction
WHERE DateDiff("w",[transactionDate],Date())=0
BTW It is considered bad practice to use *
DateDiff: http://office.microsoft.com/en-us/access/ha012288111033.aspx
Simple but portable:
SELECT *
FROM Transaction
WHERE transactionDate >= ?
AND transactionDate <= ?
Calculate the two parameters in your server-side code to whatever definition of 'week' you need.
In IBM DB2
SELECT *
FROM Transaction
WHERE transactionDate BETWEEN CURRENT TIMESTAMP - 7 days AND CURRENT TIMESTAMP;
In Access, if you want to run a query to find records that fall in the current week, use
SELECT *
FROM table
WHERE table.DateField Between (Date()-Weekday(Date())+1) And (Date()-Weekday(Date())+7);
That runs Sunday through Saturday. Use +2 and +6 instead if you want the workweek.
mySQL (standard date stamp)
SELECT *
FROM Transaction
WHERE WEEK(NOW())=WEEK(transactionDate);
mySQL (unix timestamp stamp)
SELECT *
FROM Transaction
WHERE WEEK(NOW())=WEEK(FROM_UNIXTIME(transactionDate));
Bit of a unoptimized query. Could be a more efficient way.
Note: This isn't a rolling 7 days. Just the current week of the year.
EDIT: Sorry I didn't see the ms-access tag. ignore all of this :|