Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getdate() default value 1

Status
Not open for further replies.

MikeT

IS-IT--Management
Feb 1, 2001
376
0
0
US
I have a date/time field named EntryDate with a default value of getdate() so when a record is created the current date and time are recorded. The timestamp looks like this: 6/8/2005 10:47:49 AM

if I issue these
Code:
select * from mytable where entrydate='6/8/2005 10:47:49 AM'
select * from mytable where entrydate='6/8/2005'
select * from mytable where entrydate like '6/8/2005%%'
I get 0 results. However, this would give me results:
Code:
select * from mytable where entrydate BETWEEN '6/7/05' AND '6/9/05'
How can I access these records using "entrydate=..." instead of "entrydate BETWEEN..."?
 
select <cols> from <table>
where
convert(char(10), <date col>, 101) = '06/08/2005'
 
When searching datetime data you are better off NOT using a function over the column as this will stop any index being used. Instead try something like this (to find all entries on June 8th):

Code:
WHERE entrydate >= '20050608'
  AND entrydate < '20050609'

--James
 
6/8/2005

Is that June 8th or 6th of August???

How is SQL Server supposed to know which it is. When you use dates that are not YYYYMMDD, add SET DATEFORMAT to your script.

SET DATEFORMAT DMY
SET DATEFORMAT MDY
SET DATEFORMAT YMD

Just use whichever one of the above applies to how your dates are being provided.

-SQLBill

Posting advice: FAQ481-4875
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top