Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Obtaining year and week numbers from a date in SQL Expand / Collapse
Author
Message
Posted Wednesday, May 14, 2008 9:57 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, February 22, 2013 8:51 AM
Points: 16, Visits: 254
Hi does anyone know how to create a query in SQL 2000 or 2005 to get the year and week number together for a date i.e. the 01/01/2008 would be 200801. I know how to get them seperately using datepart but i need them joined together.

Any help would be much appreciated.
Post #500664
Posted Wednesday, May 14, 2008 10:06 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, May 29, 2012 11:22 AM
Points: 1,755, Visits: 4,652
select ltrim(year('01/01/2008')) + right('0' + ltrim(datepart(week, '01/01/2008')), 2)




Ryan Randall

Solutions are easy. Understanding the problem, now, that's the hard part.
Post #500685
Posted Wednesday, May 14, 2008 10:14 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, May 29, 2012 11:22 AM
Points: 1,755, Visits: 4,652
If you want a whole bunch of them, you should consider making use of this date function...

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=61519

select DATE, ISO_YEAR_WEEK_NO from dbo.F_TABLE_DATE('1/1/2008', '1/1/2009')

Better still, use that function to create a permanent, fully-indexed table, and then use that.



Ryan Randall

Solutions are easy. Understanding the problem, now, that's the hard part.
Post #500699
Posted Wednesday, May 14, 2008 12:51 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Wednesday, October 8, 2014 8:52 AM
Points: 438, Visits: 909
select year(getdate()) * 100 + datepart(week,getdate()) as year_week

Post #500839
Posted Wednesday, May 14, 2008 3:37 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, February 22, 2013 8:51 AM
Points: 16, Visits: 254
Thanks guys i will try your suggestions and let you know the outcome.
Post #500932
Posted Wednesday, May 14, 2008 10:19 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 8:38 PM
Points: 35,371, Visits: 31,912
Ack... double post...

http://www.sqlservercentral.com/Forums/Topic500305-5-1.aspx?Update=1


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #500994
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse