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
 Number of Days

Author  Topic 

gavakie
Posting Yak Master

221 Posts

Posted - 2008-04-16 : 11:29:50
How would I do a date range from the first day of this year to the getdate(), and so it will change once the new year hits as well?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-04-16 : 11:38:43
SELECT CAST('01/01/'+YEAR(getdate()) AS datetime) AS StartDate,getdate() AS EndDate
Go to Top of Page

gavakie
Posting Yak Master

221 Posts

Posted - 2008-04-16 : 11:41:22
Server: Msg 245, Level 16, State 1, Line 1
Syntax error converting the varchar value '01/01/' to a column of data type int.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-04-16 : 11:44:36
change it to
SELECT CAST('01/01/'+ CAST(YEAR(getdate()) as varchar(4)) AS datetime) AS StartDate,getdate() AS EndDate
Go to Top of Page

gavakie
Posting Yak Master

221 Posts

Posted - 2008-04-16 : 11:47:46
Thanks
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2008-04-16 : 12:59:44
There are many ways, but here is another way to get the first of the year without string conversion:
SELECT DATEADD(YEAR, YEAR(CURRENT_TIMESTAMP) - 1900, 0)
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-04-16 : 15:29:05
SELECT *
FROM Table1
WHERE Col1 >= DATEADD(YEAR, DATEDIFF(YEAR, '19000101', GETDATE()), '19000101')
AND Col1 < DATEADD(DAY, DATEDIFF(DAY, 18991231', GETDATE()), '19000101')



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -