Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table named accounts and under this table, entryDate is a column which data type is datetime. I need a SELECT command that can select all data of a particular date (like '12-02-2013' ). I use this But not work.
SQL
SELECT * FROM [accounts] WHERE entryDate='12-02-2013' 

Thanks in advance, your help is greatly appreciated.
Posted

In SQL database you cannot store and retrieve date values directly as dd/MM/yyyy, because in database the datetime will store as yyyy-MM-dd HH:mm:ss,

So my suggestion for you to use convert function in your sql query.

SQL
SELECT * FROM [accounts] where entryDate=convert(varchar(10),'12-02-2013',103)

(or)

SELECT * FROM [accounts] where entryDate=convert(datetime,'12-02-2013',103)


I hope your doubt is cleared, feel free to ask any doubts if you have.
 
Share this answer
 
Comments
sazzad37 15-Dec-13 6:57am    
In your command what is 103?
M.Abdul Rahman 15-Dec-13 7:19am    
this is a code to get date format in (dd/MM/yyyy)
follow this link you'll get to know all

http://www.w3schools.com/sql/func_convert.asp
"But not work" - is not informative at all!

Depending on locale settings[^] SQL Server can store date data types in different formats:
- dd-mm-yyy
- MM/dd/yyy
- mm-dd-yyy
- etc.

To change it use SET DATEFORMAT[^] command, for example:
SQL
SET DATEFORMAT dmy;
SELECT * FROM [accounts] WHERE entryDate='12-02-2013'
 
Share this answer
 
Further to the solutions above, SQL Server actually stores datetime types in a set, 8 byte format, regardless of the locale settings. Full details on in SQL Books on Line, but this article covers most of the relevant points[^]

A company I worked for used to continuously get problems with dates in where clauses as it was multi-national. We adopted the use of unambiguous formats to overcome the issues ... e.g. '12-FEB-2013' or '02-DEC-2013' depending on which one you meant.

We also had issues where our datetime literally was a datetime - i.e. included hours, minutes, seconds etc so things like WHERE entryDate='12-FEB-2013' would fail anyway. We got around that particular problem something like this ...
SQL
WHERE dateadd(dd, datediff(dd, 0, entryDate), 0) = '12-FEB-2013'
We got the formatting stuff from this blog on sqlservercentral.com[^]
 
Share this answer
 
hey bro just check database field date and then write same date in you code
 
Share this answer
 
check your system date format....and give the same format in your query...
 
Share this answer
 
Comments
An@nd Rajan10 16-Dec-13 1:20am    
hey...why your down voting ,what is the problem ???

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