Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hii,,

I am using custom format to store the date in the database .It should be stored in this form MM/dd/yyyy. I have to retrieve the results based on the date.My table looks like this:

Id Name Date

1 Diya 09/10/2012

2 Naitik 09/10/2012

Now to retrieve this data if I write a query to select the records on 9/10/2012 then it is showing null results ..What I want is whatever the date format I give(9/10/2012 or 09/10/2012) I should be able to retrieve the results .How can I write the SQL query for this?

Thanks,,,
Posted
Comments
_Amy 10-Sep-12 7:17am    
Currently which query you are using? Can I see that?
VIPR@T 10-Sep-12 7:21am    
show your query..
cutie1 10-Sep-12 7:23am    
create proc GetDetails @Date Datetime as select * from Details where Date=@Date.This is my stored procedure

Assuming you are using SQL Server, you can use DATEDIFF[^] function to compare your dates.
In your where clause you can write it as
SQL
DATEDIFF(DAY, Date, @Date) = 0

Where @Date is the date you are comparing with the date column in your table.

With this approach you don't have to worry about the format in which the date is stored in database.
 
Share this answer
 
Comments
cutie1 10-Sep-12 7:40am    
Thanks I got it..
_Amy 10-Sep-12 7:45am    
5'ed! :)
__TR__ 10-Sep-12 7:50am    
Thanks Amit.
Maciej Los 10-Sep-12 16:49pm    
Interesting trick ;)
+5!
__TR__ 11-Sep-12 2:55am    
Thanks losmac :)
If you have custom date format and your query looks like:
SQL
SELECT *
FROM Details
WHERE Date=@Date

use SET DATEFORMAT[^] command before SELECT statement.

Example:
SQL
SET DATEFORMAT mdy;
SELECT *
FROM Details
WHERE Date=@Date
 
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