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')
Related
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.
I'm developing a SQL Query, as a restriction, I want that the query returns data from the first day of the last month until now.
For example: If the query is executed today, the query will only return data entered from 01/03/2019 until today.
To do this, i created the following restriction:
...
SF1010.F1_DTDIGIT>=concat(YEAR(GETDATE()),'0',month(GETDATE())-1,'01') AND
...
The problem is in the months 01, 11 and 12:
If the month is "11", the algorithm will return "1901001" and not "191001".
If the month is "01", the algorithm will return "190001" and not "181201".
Thanks for any help.
SF1010.F1_DTDIGT BETWEEN $yourDate AND date_add($yourDate,interval -DAY($yourDate)+1 DAY)
This gets dates between your date and the first day of the month. first day logic works by subtracting the DAY part to get first of month.
This query will do the comparison of your DTDIGIT with the first day of the previous month in the DATE format:
SF1010.F1_DTDIGIT >= STR_TO_DATE(CONCAT(YEAR(DATE_SUB(NOW(), INTERVAL 1 MONTH)),'-',MONTH(DATE_SUB(NOW(), INTERVAL 1 MONTH)),'-','1'), '%Y-%m-%d')
Explanation
DATE_SUB(NOW(), INTERVAL 1 MONTH) returns the date exactly one month ago. We use that date to get the previous month
YEAR() and MONTH() functions use the date explained above
STR_TO_DATE() function converts a string representing a date to the
MySQL DATE format, using the mask (in this example, it's
'%Y-%m-%d').
How about this?
SF1010.F1_DTDIGT >= date_add(date_add(curdate(), interval 1 - day(curdate()) day), interval -1 month)
This assumes the date is not in the future. You can add another condition if that is possible.
If you happen to be using SQL Server, you can do this weird form of date arithmetic:
SF1010.F1_DTDIGT >= dateadd(month, datediff(month, 0, getdate()) - 1, 0)
I am working on an Excel project where I use SQL query to extract some data from SQL Server 2012.
When I select the data without filters, everything works fine.
However, when I use the DATEFORMATPARTS formula below, I get this error:
Cannot construct data type date some of the arguments have values which are not valid
A.[Invoice date] is the correct date format.
WHERE
A.[Customer] NOT IN ('100', '398', 399)
AND A.[Item] LIKE '1%'
AND A.[Invoice date] >= DATEFROMPARTS(Year(DATEADD(yyyy, -1, GETDATE())), Month(DATEADD(yyyy, -1, GETDATE()) >) + 1, 1)
I've tried a lot of different stuff, but without luck.
Any guesses what is wrong in the above.
Thanks a lot in advance!
/ T
Thanks for the input
it was precisely the month: 12 + 1
that was the issue! thanks
You're going to have problems with that month calculation if you get a december and it adds a month to get month 13 of the year, this obviously isn't valid. If you want 11 months ago then do this;
DATEFROMPARTS(Year(DATEADD(YYYY,-1,GETDATE())),Month(DATEADD(mm,-11,GETDATE())),1)
Which (today, December 2016) returns '2015-01-01'
I believe > sign in the expression is a typo
Month(DATEADD(yyyy,-1,GETDATE()))+1
This is resulting 13 which is a invalid month so you are getting that error.
Removing > and +1 from Month part will fix your issue
select DATEFROMPARTS(Year(DATEADD(yyyy,-1,GETDATE())),Month(DATEADD(yyyy,-1,GETDATE())),1)
If you are trying to find last year last month first date then
select datefromparts(year(getdate())-1,12,1)
I have a SQL query that prompts for a startdate and enddate, Between #startdate And #enddate. But what I am trying to do is have the same query automatically look 3 months forward from today's month using a dateadd / getdate code, but can not figure out how to integrate the two?
In sql-server, which I assume because you are referencing GETDATE() and DATEADD()
DATEADD(MONTH,3,GETDATE())
IN Oracle based on Why is the GETDATE() an invalid identifier, and Equivalent function for DATEADD() in Oracle
ADD_MONTHS(SYSDATE,3)
Based on your comment and if Siebel is on sql-server you would need
Where vwS_CoPrimPolicy.S_PrimPolicyExpDT Between #startdate And #enddate And vwS_Co.S_CoStatus = 'Customer' And vwS_Co.S_CoType = 'Automotive
to become
where vwS_CoPrimPolicy.S_PrimPolicyExpDT Between
DATEFROMPARTS(YEAR(DATEADD(MONTH,3,GETDATE())),MONTH(DATEADD(MONTH,3,GETDATE())),1)
AND DATEADD(DAY,-1,DATEFROMPARTS(YEAR(DATEADD(MONTH,4,GETDATE())),MONTH(DATEADD(MONTH,4,GETDATE())),1))
And vwS_Co.S_CoStatus = 'Customer' And vwS_Co.S_CoType = 'Automotive
To Break that down a little because you are actually looking for the start of the month 3 months from today to the end of the month 3 months from today and those months could be 28,29,30,or 31 days long you actually need to find the date 3 months ahead then get the beginning of the month DATEFROMPARTS() is useful for that. Then for the end of the month, find the date 4 months out and go back a day.
I know this one is pretty easy but I've always had a nightmare when it comes to comparing dates in SQL please can someone help me out with this, thanks.
I need to get the month and year of now then compare it to a date stored in a DB.
Time Format in the DB:
2015-08-17 11:10:14.000
I need to compare the month and year with now and if its > 12 months old I will increment a count. I just need the number of rows where this argument is true.
I assume you have a datetime field.
You can use the DATEDIFF function, which takes the kind of "crossed boundaries", the start date and the end date.
Your boundary is the month because you are only interested in year and month, not days, so you can use the month macro.
Your start time is the value stored in the table's row.
Your end time is now. You can get system time selecting SYSDATETIME function.
So, assuming your table is called mtable and the datetime object is stored in its date field, you simply have to query:
SELECT COUNT(*) FROM mtable where DATEDIFF(month, mtable.date, (SELECT SYSDATETIME())) > 12