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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Select query get todays records only

Author  Topic 

cplusplus
Aged Yak Warrior

567 Posts

Posted - 2009-01-15 : 13:57:01
How can i use in my select query condition to get just todaye's date records only.

where sent_date = Todays_Date()

Thank you..

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2009-01-15 : 14:08:59
look for getdate()
Go to Top of Page

rohitkumar
Constraint Violating Yak Guru

472 Posts

Posted - 2009-01-15 : 14:12:41
remember to exclude the timestamp while comparing two dates
Go to Top of Page

cplusplus
Aged Yak Warrior

567 Posts

Posted - 2009-01-15 : 14:13:39
How can i exclude timestamp() from Getdate()...

Thank you for the information.
quote:
Originally posted by rohitkumar

remember to exclude the timestamp while comparing two dates

Go to Top of Page

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2009-01-15 : 14:20:17
[code]where sent_date =DATEADD(DAY, 0, DATEDIFF(DAY, 0, getdate())) [/code]
Go to Top of Page

rohitkumar
Constraint Violating Yak Guru

472 Posts

Posted - 2009-01-15 : 14:23:12
and I would exclude timestamp from sent_date too in a similar way
Go to Top of Page

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-01-15 : 23:18:07
Try this also,
select dateadd(d,0,datediff(d,0,current_timestamp))
Go to Top of Page

Jai Krishna
Constraint Violating Yak Guru

333 Posts

Posted - 2009-01-16 : 00:01:47
select * from urtable where datediff(d,sent_date,getdate()) = 0



Jai Krishna
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-16 : 00:06:25
quote:
Originally posted by sakets_2000

where sent_date =DATEADD(DAY, 0, DATEDIFF(DAY, 0, getdate())) 




will work only if you dont have timepart for date values
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-16 : 00:09:04
quote:
Originally posted by Jai Krishna

select * from urtable where datediff(d,sent_date,getdate()) = 0



Jai Krishna



if you want to use index on datefield use

select * from urtable where sent_date>=dateadd(dd,datediff(dd,0,getdate()),0) and sent_date<dateadd(dd,datediff(dd,0,getdate()),1)
Go to Top of Page

thiyagu_rind
Starting Member

46 Posts

Posted - 2009-01-16 : 00:27:42
Hi,

Try the below also,

WHERE CONVERT(VARCHAR(10), SENT_DATE,103) = CONVERT(VARCHAR(10), GETDATE(),103)

Regards
Thiyagarajan
www.sqlhunt.blogspot.com

Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-01-16 : 01:28:32
TRY this
select * from URTABLE
WHERE LEFT(SENT_DATE,11)= LEFT(GETDATE(),11)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-16 : 02:50:54
please dont convert date values to varchar for comparison. use any of the other suggestions given. Try to preserve the datetime datatype.
Go to Top of Page

ra.shinde
Posting Yak Master

103 Posts

Posted - 2009-01-16 : 03:03:15
Converting to varchar in where clause will not change datatype in result

Rahul Shinde
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-16 : 03:17:25
quote:
Originally posted by ra.shinde

Converting to varchar in where clause will not change datatype in result

Rahul Shinde


that i know
its better to preserve the original datatype of field especially when you're using comparison operators like >,<,...
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-01-16 : 03:51:21
good suggestion visakh,

try this
where dateadd(d,0,datediff(d,0,sent_Date)) = dateadd(d,0,datediff(d,0,getdate()))
Go to Top of Page

thiyagu_rind
Starting Member

46 Posts

Posted - 2009-01-16 : 04:07:43
hi,

I hope it will better to use the code given Jai Krishna. You can try the below method also.

Code Snippet:
Where datediff(d,0,sent_date) = datediff(d,0,getdate())


Regards
Thiyagarajan
www.sqlhunt.blogspot.com
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-01-16 : 06:04:57
quote:
Originally posted by thiyagu_rind

hi,

I hope it will better to use the code given Jai Krishna. You can try the below method also.

Code Snippet:
Where datediff(d,0,sent_date) = datediff(d,0,getdate())


Regards
Thiyagarajan
www.sqlhunt.blogspot.com



Thiyagarajan,

It works but you need to use index As well. So it is better to go with Visakh's approach.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-01-16 : 07:22:45
quote:
Originally posted by bklr

TRY this
select * from URTABLE
WHERE LEFT(SENT_DATE,11)= LEFT(GETDATE(),11)



Dont convert date values to VARCHARs
You have been already told this
Learn from the answers posted

Madhivanan

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

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-01-16 : 07:25:16
quote:
Originally posted by madhivanan

quote:
Originally posted by bklr

TRY this
select * from URTABLE
WHERE LEFT(SENT_DATE,11)= LEFT(GETDATE(),11)



Dont convert date values to VARCHARs
You have been already told this
Learn from the answers posted

Madhivanan

Failing to plan is Planning to fail


after that i have given this sol also
where dateadd(d,0,datediff(d,0,sent_Date)) = dateadd(d,0,datediff(d,0,getdate()))
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-01-16 : 07:36:33
quote:
Originally posted by bklr

quote:
Originally posted by madhivanan

quote:
Originally posted by bklr

TRY this
select * from URTABLE
WHERE LEFT(SENT_DATE,11)= LEFT(GETDATE(),11)



Dont convert date values to VARCHARs
You have been already told this
Learn from the answers posted

Madhivanan

Failing to plan is Planning to fail


after that i have given this sol also
where dateadd(d,0,datediff(d,0,sent_Date)) = dateadd(d,0,datediff(d,0,getdate()))


See Visakh's second reply
Thats most likely make use of index
Avoid using functions on columns in the WHERE clause

Madhivanan

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

- Advertisement -