sql query to get last 6 months of data - sql

I am trying to get the data for the last 6 months.
This is what I have used:
WHERE d_date > DATEADD(m, -6, current_timestamp)
and I am getting this error.
ERROR: CLI prepare error: SQL0206N "M" is not valid in the context where it is used
also tried
WHERE d_date > current date -180
and got this error:
ERROR: CLI prepare error: SQL0171N The data type, length or value of the argument for the parameter in
position "2" of routine "-" is incorrect. Parameter name: "". SQLSTATE=42815
Please advice.

Based on Andriy's eagle-eyes, here is (I think) the DB2 syntax:
WHERE d_date > current_date - 6 MONTHS
And here is a link to a pretty good function to mirror DATEADD in DB2.
Also, since you mentioned SAS, here is the SAS syntax to do the same thing:
WHERE d_date > intnx('MONTH', today(), -6, 'SAME');
Although you say you are running this with SAS Enterprise Guide, the syntax you show is not SAS. The error message you are getting suggests you are submitting "pass-thru" code directly to a database.

In DB2, it should be something like
WHERE TIMESTAMPDIFF(64, CAST(CURRENT_TIMESTAMP- d_date AS CHAR(22))) <= 6
Remove the SQL-Server tag, that's for MS SQLServer questions.

You can also try this
SELECT *
From PIS.dbo.PIS_tblEmployeeGenInfo
WHERE dtJoiningDate > DATEADD(m, -6, current_timestamp)

I'm using in code the following:
INSERT INTO Employee(I_EMP, T_CRT, C_SYS, D_HIRED)
VALUES (21,CURRENT TIMESTAMP, 10,CURRENT DATE - 6 MONTH);
This is in DB2, and it is working with no issues.
Hope that will help.

Related

DATEADD in JDBC template

I'm trying a query select with jdbc and spring boot,
I want select informations where my creation date value is LESS than now minus 30.
I tried like this :
SELECT *
FROM informations
WHERE lastTreatment < DATEADD(MINUTE, -30, GETDATE())
I have an error like this :
Caused by: org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar
Anyone has idea ? Thanks
You claim Oracle but looks like you write MS SQL Server or MySQL. There is no such function DATEADD in Oracle and neither is there a GETDATE function. To add x days in to a date in just do date+x. In this case you just need a fractional part of a day, so just add an x which yields the need fraction. For 30 minuets that would be 30/(60*24) or (30 minuets/ (60 minuets per hour * 24 hours per day). As for GETDATE just use SYSDATE.
So your query becomes:
SELECT *
FROM informations
WHERE lastTreatment < sysdate-(30/(60*24));
maybe missing ; at the end?
SELECT *
FROM informations
WHERE lastTreatment < DATEADD(MINUTE, -30, GETDATE());

DateAdd function in Excel SQL query not working

I am using Excel to make an SQL query (connecting to an ODBC).
I have a problem with the function DateAdd(), which just doesn't work as expected.
What I need to do is to get the data from the last six months.
My code is something like
SELECT blablabla
FROM blablabla and then I have this:
WHERE Note_0.Relate_key = Work_history_0.WO_Key AND Work_history_0.Order_date> DateAdd(Month, -6, Now())
I've searched in the internet and this syntax is supposed to work, but I get this error message
Column "MONTH" cannot be found or is not specified for query. (13865)
As if it didn't have the parameters I think it has, which are "interval, number, date", but something else.
Any idea about this?
This is what you need:
DateAdd("m", -6, Now)
Or even DateAdd("m", -6, Date), if you want to get rid of the hours, coming from Now.
Thus, you have to declare that you want the "Months" to be substracted.
DateAdd MSDN
WHERE Note_0.Relate_key = Work_history_0.WO_Key AND Work_history_0.Order_date > ADD_MONTHS(curdate(), -6)

Subtract table value from today's date - SQL

I am trying to return the number of years someone has been a part of our team based on their join date. However i am getting a invalid minus operation error. The whole getdate() is not my friend so i am sure my syntax is wrong.
Can anyone lend some help?
SELECT
Profile.ID as 'ID',
dateadd(year, -profile.JOIN_DATE, getdate()) as 'Years with Org'
FROM Profile
MySQL Solution
Use the DATE_DIFF function
The DATEDIFF() function returns the time between two dates.
DATEDIFF(date1,date2)
http://www.w3schools.com/sql/func_datediff_mysql.asp
This method only takes the number of days difference. You need to convert to years by dividing by 365. To return an integer, use the FLOOR method.
In your case, it would look like this
SELECT
Profile.ID as 'ID',
(FLOOR(DATEDIFF(profile.JOIN_DATE, getdate()) / 365)) * -1 as 'Years with Org'
FROM Profile
Here's an example fiddle I created
http://sqlfiddle.com/#!9/8dbb6/2/0
MsSQL / SQL Server solution
The DATEDIFF() function returns the time between two dates.
Syntax: DATEDIFF(datepart,startdate,enddate)
It's important to note here, that unlike it's MySql counterpart, the SQL Server version takes in three parameters. For your example, the code looks as follows
SELECT Profile.ID as 'ID',
DATEDIFF(YEAR,Profile.JoinDate,GETDATE()) as difference
FROM Profile
http://www.w3schools.com/sql/func_datediff.asp
Looks like you're using T-SQL? If so, you should use DATEDIFF:
DATEDIFF(year, profile.JOIN_DATE, getdate())
Using the method from this answer, you can get your result with the following query :
SELECT
Profile.ID as 'ID',
YEAR(getdate()) - YEAR(profile.JOIN_DATE) - (DATE_FORMAT(getdate(), '%m%d') < DATE_FORMAT(profile.JOIN_DATE, '%m%d')) as 'Years with Org'
FROM Profile
If this is MS SQL:
SELECT
Profile.ID as 'ID',
DATEDIFF(YEAR,Profile.JoinDate,GETDATE())
FROM Profile

Using DateDiff() to find the difference between getDate() and a concatonated value

I am trying to find the difference between today's date and a value that is a concatenation of mulitple values but begins with an 8 digit date without any dashes or forward slashes. There's something wrong with my syntax I believe, but I'm not yet skilled enough to see what I'm doing incorrectly. Here is what I have so far:
select DateDiff(dd, (select MIN(CAST(Left(batchid, 8) as Date)) from
[Table]), getdate()) from [Table]
This is returning the following error: "Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string."
I think your have data where the left 8 is not a valid date in yyyymmdd format. Your can run the following query to find them
select batchid, isdate(Left(batchid, 8))
from [Table]
where isdate(Left(date, 8)) = 0
This is the correct syntax to your query. Your original example had an extra parenthesis which I assume was a typo since your error appears to be data related.
select
datediff(dd, (select min(cast(left(batchid, 8) as date))
from [Table]), getdate())
Could you provide some more details.
Namely, what does batchid look like in 8 digit form? is it YYYYMMDD or DDMMYYYY or MMDDYYYY?
Also could you show us the result of the following?
select MIN(CAST(Left(batchid, 8) as Date)))
from [Table])
Sry for using an answer, i don't have the rep to add a comment directly below.
This was may error. I was working with another table and forgot batchID was not the same for both. The concatenated batchID in the table I posted a question about can't be converted to a date.

Please tell me what is error in my date comparison sql query

Please help me to find out error in my SQL query. I have created this query to compare dates
select * from Joinplans jp
where cast(convert(varchar,GETDATE(),103) AS datetime) BETWEEN
CASE(convert(varchar,jp.planstartDate,103) AS datetime) AND
CASE(convert(varchar,DATEADD(DAY,jp.planDays,jp.planstartDate),103) AS DATETIME)
It's giving me the error:
incorrect near 'AS'
I am using SQL Server 2005.
You wrote case instead of cast in two instances.
If planStartDate is actually a date, then there is no need to cast it to a character column:
Select ...
From Joinplans jp
where GetDate() Between planStartDate And DateAdd(day, jp.planDays, jp.planStartDate)
Now, if planStartDate is storing both date and time data, then you might want to use something like:
Select ...
From Joinplans jp
Where planStartDate <= GetDate()
And GetDate() < DateAdd(day, jp.planDays + 1, jp.planStartDate)
This ensures that all times on the last date calculated via the DateAdd function are included