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 2012 Forums
 Transact-SQL (2012)
 convert char to date - week format

Author  Topic 

czajkuss
Starting Member

3 Posts

Posted - 2013-07-25 : 04:10:40
Hi, I have column with information about day of seeling goods in format 'YYYYMMDD' and I have to convert it on format YYYY-quater and YYYY-MM-week? Have you got any idea how handle with this case?? Thanks!!

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-25 : 04:41:53
whats the base datatype of the field?
if its datetime use

DATENAME(year,datefield) + '-' + DATENAME(quarter,datefield) AS [YYYY-quater]

DATENAME(year,datefield) + '-' + RIGHT('00' + CAST(DATEPART(month,datefield) AS varchar(2)),2) + '-' + DATENAME(week,datefield) AS [YYYY-MM-Week]


otherwise you need to first convert it to date before you apply above logic

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

czajkuss
Starting Member

3 Posts

Posted - 2013-07-25 : 05:04:59
It works, thanks!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-25 : 05:07:52
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2013-07-25 : 06:32:12
Remember, WEEK is not ISO Week number. If you want ISO Week number, replace Week parameter with ISO_WEEK instead.



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page
   

- Advertisement -