Click here to Skip to main content
15,917,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i am writing a query to retrieve the records based on the condition. the input parameters are datetime datetype.

My query is

Select * from tblname where fromdate = 'parameter1' and todate = 'parameter2'

Input values are

1. Parameter1 = '26/04/2013 00:00:00'
2. Paramater2 = '27/04/2013 00:00:00'

If i pass the parameters like '26-APR-13' and '27/03/13', i can retrieve the records. But if i try to pass direct input, I am not getting it. I tried by using TO_DATE(Parameter1 , 'DD/MM/yyyy hh24:mi:ss') function. Even then i cannot retrieve it.


Anyone please help me to resolve this?
Posted
Comments
Mike Meinz 27-Apr-13 8:46am    
I like to use a datetime format of yyyy-MM-dd hh:mm:ss because it eliminates the ambiguity of month first vs. day first.

Also, if your database columns fromdate and todate also contain the time, then your comparison could fail if the times are not absolutely exact.
Member 8112455 3-May-13 1:02am    
Thank you for your suggestion. I solved it by converting month into "mmm" format.
Bernhard Hiller 2-May-13 3:19am    
Which programming language is use (C#, VB, Java, ...) for the program to communicate with the Oracle database? Most of them offer parameterized queries, and you do not need to worry about formatting the input. Or is it some Oracle specific editor?
Member 8112455 3-May-13 1:03am    
Thank you for your suggestion. I solved it by converting month into "mmm" format.

1 solution

SQL
Hi, 

--Convert any date input to the stored procedure to datetime
--Example:

declare @tempStartDate datetime
declare @tempEndDate datetime

select @tempStartDate = convert(datetime, @InputStartDate)
select @tempEndDate = convert(datetime, @InputEndDate)

--write the query to retrieve records
select * from dbo.TempTable where fromDate >= @tempStartDate and toDate <= @tempEndDate

--of course, your data will be displayed as result based on whatever value records you have (fromDate and toDate with time component or without time component)


Hope this helps.

Happy coding..!!
 
Share this answer
 
Comments
Member 8112455 3-May-13 1:04am    
Thank you for your code. My database is oracle. I solved it by converting month into "mmm" format.

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