March 24, 2006 at 3:20 pm
If you wanted to select all the records where join on a date time field.
And you only want todays records.
March 24, 2006 at 3:48 pm
SELECT *
FROM Table1
INNER JOIN Table2
ON Table1.Date = Table2.Date
WHERE CONVERT(varchar(10), Table1.Date, 101) = '03/24/2006'
March 24, 2006 at 4:52 pm
SELECT *
FROM Table1
INNER JOIN Table2
ON Table1.Date = Table2.Date
AND Table1.Date >= convert(datetime,'20060324') /*always pass universal date, with function on the right allows indexes to be used*/
AND Table1.date < dateadd(d,1,convert(datetime,'20060324'))
March 27, 2006 at 8:01 am
I want this to run every day looking for records with today's date in it.
Table xyz
datefield char(10)
select * from xyz where datefield = ??????
something like informix I could have
select * from xyz where datefield = today
what would I use in SQL server to replace today?
March 27, 2006 at 8:29 am
March 28, 2006 at 3:47 am
Just note that in sql dates always have assocated times. Getdate() returns date and time, not just date. There's no built in function that only returns date.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
March 28, 2006 at 8:35 am
Right, but using getdate() in combination with month, day, year, or even convert will get you a date only.
March 28, 2006 at 1:20 pm
Donald,
If datefield in table xyz is defined as char(10), it's not a datetime column. You'll have to convert today's date acquired getdate() to character to do the comparison. While you're looking up getdate() in BOL, look up convert.
Greg
Greg
April 1, 2006 at 7:49 pm
Try this, Donald...
select * from xyz where datefield >= DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0)
AND datefield < DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0)+1
The DateAdd/DateDiff thing strips the time from the date without the expense of a character conversion and without performing a calculation on the datefield... that way, an index will still be used if there is one on datefield.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply