DATEADD in JDBC template - sql

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

Related

SQL: Trying to select records 7 days before date

I have a table with a date field called oppo_installdate. This is a date in the future, and I basically want to select records where this date is 7 or fewer days from the current date. I have tried to do this with the query below but it is older returning dates from 2019 as well, and I'm not sure why that's happening.
select * from [CRM_Test].[dbo].[Opportunity]
where (GETDATE() >= DATEADD(day,-7, oppo_installdate))
Could anyone suggest a better way of doing this?
Whenever you're using a WHERE always try to apply any functions to constants, or other functions, never your columns. DATEADD(day,-7, oppo_installdate) will make the query non-SARGable, and could slow it down as any indexes won't be able to be used.
It seems like what you simply want is:
SELECT *
FROM [dbo].[Opportunity]
WHERE oppo_installdate >= DATEADD(DAY, 7, GETDATE());
Note that GETDATE returns a datetime, so if you want from midnight 7 days ago, you would use CONVERT(date,GETDATE()) (or CAST(GETDATE() AS date)).
Use below condition-
select * from [CRM_Test].[dbo].[Opportunity]
where oppo_installdate>= DATEADD(day,-7, GETDATE()) and oppo_installdate<=getdate()
This should give you the records where oppo_installdate is 7 or fewer days away from now:
SELECT *
FROM [dbo].[Opportunity]
WHERE oppo_installdate <= DATEADD(DAY, 7, GETDATE())
and oppo_installdate > getdate();

How to add Floating(decimal) hour in DateTime's DATEADD function?

I am trying to add some hours in SQL SERVER using DATEADD function. But when I try this,
SELECT DATEADD(Hour, 0.5, GETDATE())
It is not adding 0.5 hour. How to solve this?
You can't. It's well describer on documentation: DATEADD (Transact-SQL)
number
Is an expression that can be resolved to an int that is added
to a datepart of date. User-defined variables are valid.
If you specify a value with a decimal fraction, the fraction is truncated and
not rounded.
UPDATE
You could try that:
SELECT DATEADD(Second, 0.5 * 60 * 60, GETDATE())
Of course - you can change DATEPART and multiplier to get desired precision.
You can't add parts of hours, just full hours. Use minute for half an hour
SELECT DATEADD(minute, 30, GETDATE())
I have found another approach to this where adding a number to the date instead of using the function works for fractions. For example:
GETDATE() + n
where n is the number of days. For 1 half hour, you can use:
GETDATE() + 0.5/24

SQL to Return Dates for Selected Week Only

I'm writing a SQL query on a timesheet report. I need the report to return only the details for the week of the selected date.
E.g., if I pick 02/01/2012 (dd/MM/yyyy), then it should only return results between 02/01/2012 and 08/01/2012.
Thanks
SELECT
*
FROM
yourTable
WHERE
dateField >= #yourDate
AND dateField < #yourDate + 7
Some variations of SQL may have specific ways of adding 7 days to a datevalue. Such as...
- DateAdd(Day, 7, #date)
- DATE_ADD(#date, INTERVAL 7 DAYS)
- etc, etc
This option is both index friendly, and is resilient to database fields that have time parts as well as date parts.
You easiest is the equivalent of WEEK_OF_YEAR function in your SQL engine
But you can also use DATE_ADD
WHERE table.date BETWEEN target_date AND DATE_ADD(target_date,INTERVAL 7 DAY)
That depends on the database system you're using. MySQL has a function calles WEEK(), SQL Server can do something like this with the DATEPART() function:
MySQL:
SELECT
*
FROM table
WHERE WEEK(date_col) = WEEK('02/01/2012');
SQL SERVER:
SELECT
*
FROM table
WHERE DATEPART(WEEK, datecol) = DATEPART(WEEK,'02/01/2012');
SELECT * FROM table_name
WHERE date BETWEEN '1/02/2012' AND '1/08/2012';
you can replace the example date with your date and yourdate + 6.
Look here for example: http://www.roseindia.net/sql/sql-between-datetime.shtml

DATEPART not working like i think it must

select *
from Advertisements
where DepartureDate < DATEPART('dd.mm.yy', '09.10.2010');
but i get
Msg 1023, Level 15, State 1, Line 1
Invalid parameter 1 specified for datepart.
in plsql is this very simple here is so complicated...
Can someone tell me please how can i get all dates that are smaller than today.
You can use this to get the current date:
CONVERT(date, GETDATE())
See the documentation.
Can someone tell me please how can i get all dates that are smaller than today.
SELECT *
FROM Advertisements
WHERE DepartureDate < CONVERT(date, GETDATE())
You seem to be confusing DATEPART with FORMAT_DATE (which does not exist anyway).
DATEPART extracts certain part of a date. Exactly one part.
Dates that are smaller than today are < dbo.CropTime(getdate()), where CropTime is a function which can be written in different ways (such as those described in this question).
Or, in case you are using SQL Server 2008, it's as simple as < cast(getdate() as date).
Would that code really work in PL/SQL? The DATEPART in T-SQL function is used to extract individual portions of a date.
This will get you all the dates before now.
select * from Advertisements where DepartureDate < getdate()
If you're planning to hardcode the date (as your sample code suggests), you just need to format in a way that SQL Server will understand. eg.
select * from Advertisements where DepartureDate < '2010-10-09'
I've been told that date format works on every server regardless of its localization settings. It's certainly worked on every server I've tried it on - but I'm happy to be overruled :-)
What you are looking for I think is
select *
from Advertisements
where DepartureDate < Convert(Date, '09.10.2010', 102)
or possibly
SELECT *
FROM Advertisements
WHERE DepartureDate < Cast(CURRENT_TIMESTAMP as date)
DatePart is used for getting components of the date such as the month, year or day. To get dates that are smaller (older) than now I would do this.
select * from Advertisements where DepartureDate < GetDate();
If I wanted Departure dates that were yesterday or before I could do this.
select * from Advertisements where DepartureDate < Convert(DateTime,Convert(Char(10),GetDate(),121));
or
select * from Advertisements where DepartureDate < Convert(DateTime,floor(convert(int,GetDate())))

How to get one day ahead of a given date?

Suppose I have a date 2010-07-29. Now I would like to check the result of one day ahead. how to do that
For example,
SELECT *
from table
where date = date("2010-07-29")
How to do one day before without changing the string "2010-07-29"?
I searched and get some suggestion from web and I tried
SELECT *
from table
where date = (date("2010-07-29") - 1 Day)
but failed.
MySQL
SELECT *
FROM TABLE t
WHERE t.date BETWEEN DATE_SUB('2010-07-29', INTERVAL 1 DAY)
AND '2010-07-29'
Change DATE_SUB to DATE_ADD if you want to add a day (and reverse the BETWEEN parameters).
SQL Server
SELECT *
FROM TABLE t
WHERE t.date BETWEEN DATEADD(dd, -1, '2010-07-29')
AND '2010-07-29'
Oracle
SELECT *
FROM TABLE t
WHERE t.date BETWEEN TO_DATE('2010-07-29', 'YYYY-MM-DD') - 1
AND TO_DATE('2010-07-29', 'YYYY-MM-DD')
I used BETWEEN because the date column is likely DATETIME (on MySQL & SQL Server, vs DATE on Oracle), which includes the time portion so equals means the value has to equal exactly. These queries give you the span of a day.
If you're using Oracle, you can use the + and - operators to add a number of days to a date.
http://psoug.org/reference/date_func.html
Example:
SELECT SYSDATE + 1 FROM dual;
Will yield tomorrow's date.
If you're not using Oracle, please tell use what you ARE using so we can give better answers. This sort of thing depends on the database you are using. It will NOT be the same across different databases.
Depends of the DateTime Functions available on the RDBMS
For Mysql you can try:
mysql> SELECT DATE_ADD('1997-12-31',
-> INTERVAL 1 DAY);
mysql> SELECT DATE_SUB('1998-01-02', INTERVAL 31 DAY);
-> '1997-12-02'
If youre using MSSQL, you're looking for DateAdd() I'm a little fuzzy on the syntax, but its something like:
Select * //not really, call out your columns
From [table]
Where date = DateAdd(dd, -1, "2010-07-29",)
Edit: This syntax should be correct: it has been updated in response to a comment.
I may have the specific parameters in the wrong order, but that should get you there.
In PL SQL : select sysdate+1 from dual;