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 Parameter

Author  Topic 

werhardt
Constraint Violating Yak Guru

270 Posts

Posted - 2008-04-02 : 08:39:04
Hi,

My boss would like to see on our report a code that would pull the date 01-01-2008 to the present. They want to be able to run this report whenever they wanted to. What is the best way to create this code.

Could I just use my field, which is "clm_dout" to say Clm_dout >= or is there another type of code I should use?

Thanks

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2008-04-02 : 08:50:03
Assuming that your field is a datetime data type, clm_dout > = '01/01/2008' should work just fine.
clm_dout > = '01/01/2008' and clm_dout < current_timestamp just to make sure you don't get any
future dates.


Jim
Go to Top of Page

elancaster
A very urgent SQL Yakette

1208 Posts

Posted - 2008-04-02 : 09:00:08
how future-proof do you want it? when it gets to january next year will it still be right or do you want build it so it's always 'current year to date'?

Em
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2008-04-02 : 09:02:47
SELECT DATEADD(year,DATEDIFF(year,0,current_timestamp),0)

Jim
Go to Top of Page

werhardt
Constraint Violating Yak Guru

270 Posts

Posted - 2008-04-02 : 09:03:01
I want to build it so it always is a current year to date.


quote:
Originally posted by elancaster

how future-proof do you want it? when it gets to january next year will it still be right or do you want build it so it's always 'current year to date'?

Em

Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2008-04-02 : 09:08:58
SELECT DATEADD(year,DATEDIFF(year,0,current_timestamp),0)
will always give you 01/01 of the current year

Jim
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-04-02 : 09:49:10
[code]
where datecol>=dateadd(year,datediff(year,0,getdate()),0) and
datecol<dateadd(day,datediff(day,0,getdate())+1,0)[/code]

Madhivanan

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

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2008-04-02 : 10:14:52
There are quite a few possibilities, here's my contribution:

...
WHERE YEAR(clm_dout) = YEAR(GETDATE())

--
Lumbago
"SELECT Rum, Coke, Lime, Ice FROM bar WHERE ClosingTime = 'Late' AND FemaleMaleRatio > 4"
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2008-04-02 : 10:19:40
Lumbago,
Your solution puts a function on the column, which will slow things down as well prevent an index (if there is one) from being used. Madhi's is the way to go.


Jim
Go to Top of Page
   

- Advertisement -