Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query

Author  Topic 

spinoza
Starting Member

49 Posts

Posted - 2006-01-01 : 06:36:17
Happy new year to everyone,

I migrated recently from Access to SQL... However I am facing the following problem with the this query...

strSQL = "SELECT * FROM TODAY_IN_HISTORY WHERE ((([TODAY_IN_HISTORY].Date)= Date()));"

It does return nothing... In Access worked like a charm...

I have the feeling that it has to do with reserved words...but i am not sure...Can anyone help please?

Kristen
Test

22859 Posts

Posted - 2006-01-01 : 06:42:59
Does "Date()" represent today in Access?

If so you need GetDate() in SQL Server.

GetDate() will return the current date and the time.

If you just want the date the fastest (but somewhat hard to read!) method is:

DateAdd(Day, DateDiff(Day, 0, GetDate()), 0)

If your TODAY_IN_HISTORY.Date column stores the time too, but you want everything "today", then you will also need to strip off the time part of the value before you compare it:

SELECT *
FROM TODAY_IN_HISTORY
WHERE DateAdd(Day, DateDiff(Day, 0, TODAY_IN_HISTORY.[Date]), 0)
= DateAdd(Day, DateDiff(Day, 0, GetDate()), 0)

"Date" is a reserve word, so as your column is called "Date" you may need to use [ ] around it, but SQL Server is pretty smart so as the usage here is unambiguous I don't think you will need to explicitly escape the column name in that way, but it would do no harm!.

Kristen
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-01-02 : 00:40:18
>>It does return nothing... In Access worked like a charm...

Because it doesnt have Time.

But SQL Server DateTime column will have both date and Time. So you need to do some extra work to get result when using DateTime column in where Clause

Also refer this
http://www.sql-server-performance.com/fk_datetime.asp

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -