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
 Date........

Author  Topic 

siumui
Yak Posting Veteran

54 Posts

Posted - 2012-04-04 : 15:02:15

Hello all,

I have to retrieve records based on date. I have to pull all records for each day starting 9/1/2011 to the current date, which is today.

I'm stuck and I don't even know where to begin.
Please help.

Thank you.


siumui

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2012-04-04 : 15:03:37
SELECT someColumns from yourTable WHERE dateField >= '20110901';
Go to Top of Page

siumui
Yak Posting Veteran

54 Posts

Posted - 2012-04-04 : 15:25:20
quote:
Originally posted by russell

SELECT someColumns from yourTable WHERE dateField >= '20110901';



Thank you. It's that simple and I forgot (sigh...)

siumui
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2012-04-04 : 15:48:08
You're welcome. We all need a jump-start sometimes
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-04-04 : 15:51:55
NO


SELECT someColumns from yourTable BETWEEN dateField >= '20110901' AND <= CONVERT(date,GETDATE)

What's te data type of you date column? date? datetime? smalldatetime? datetime2?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2012-04-04 : 16:21:26
ok, you're right IF there are records with a date later than today
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-04-04 : 18:17:26
better to add it too as we dont know which table we're dealing...it may contain futuristic values like case of forecast,budget etc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2012-04-04 : 19:16:25
quote:
Originally posted by visakh16

better to add it too as we dont know which table we're dealing...it may contain futuristic values like case of forecast,budget etc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





Or just outright bad data sometimes
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-04-04 : 19:35:39
quote:
Originally posted by russell

ok, you're right



I SWEAR...I never get tired of hearing that

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-04-17 : 06:03:59

OR

SELECT someColumns from yourTable WHERE dateField >= '20110901' AND < dateadd(day,datediff(day,0,getdate()),1)


Madhivanan

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

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-04-17 : 13:11:20
quote:
Originally posted by madhivanan


OR

SELECT someColumns from yourTable WHERE dateField >= '20110901' AND dateField < dateadd(day,datediff(day,0,getdate()),1)


Madhivanan

Failing to plan is Planning to fail



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-04-18 : 05:48:35
quote:
Originally posted by visakh16

quote:
Originally posted by madhivanan


OR

SELECT someColumns from yourTable WHERE dateField >= '20110901' AND dateField < dateadd(day,datediff(day,0,getdate()),1)


Madhivanan

Failing to plan is Planning to fail



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/




Thanks

Madhivanan

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

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-04-19 : 01:12:53
np

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -