Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
String car_id = txtcarid.getText();

pst = con.prepareStatement("SELECT Car_ID,Cust_ID,Rental_Due,DATEDIFF(GETDATE(),Rental_Due) as Days_Elapse FROM CarRental WHERE Car_ID =? ");
pst.setString(1, car_id);
rs = pst.executeQuery();

What I have tried:

What is wrong with the statement? Please help
Posted
Updated 22-Jan-20 6:21am

Your datediff function requires three arguments in the format:
DATEDIFF(interval, date1, date2)
and you've only supplied two.
Take a look here at some examples. SQL Server DATEDIFF() Function[^]
 
Share this answer
 
v2
Comments
Kurt Jimenez 22-Jan-20 12:24pm    
Thank you!
Quote:
What is wrong with the statement?
Exactly what it says. Go to the documentation for DATEDIFF to see what parameters are required: DATEDIFF (Transact-SQL) - SQL Server | Microsoft Docs[^]
 
Share this answer
 
Comments
Kurt Jimenez 22-Jan-20 12:24pm    
Thank you!
The error is in your actual SQL statement. The DateDiff function requires 3 parameters; and is in the format DATEDIFF(datepart, startdate, enddate )

Based on the alias you have assigned to the function (Days_Elapse), I am guessing you would want to be using something like this:
SQL
SELECT Car_ID
,      Cust_ID
,      Rental_Due
,      DATEDIFF(DD, GETDATE(),Rental_Due) as Days_Elapse -- DD: Days
FROM   CarRental
WHERE Car_ID =? 

Reference: DATEDIFF (Transact-SQL) - SQL Server | Microsoft Docs[^]
 
Share this answer
 
Comments
Kurt Jimenez 22-Jan-20 12:23pm    
It worked! Thank you for the effort!
Quote:
What is wrong with the statement?

Exactly what the message said: the datediff function requires 3 argument(s).
and your code only provide 2 arguments.
You need to read documentation to see how datediff works.
SQL Server DATEDIFF() Function[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900