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 Formatting help

Author  Topic 

w1102157
Yak Posting Veteran

80 Posts

Posted - 2012-09-07 : 10:29:26
HI guys

I have a field which called 'datekey' which is an int, i have date in this field set like 20120217, what string function can i use to change that date to month year , e.g 02/2012

thanks guy

lionofdezert
Aged Yak Warrior

885 Posts

Posted - 2012-09-07 : 10:51:21
If length is fixed then just use following query
SELECT SUBSTRING('20120217',5,2)+'/'+LEFT('20120217',4)

--------------------------
http://connectsql.blogspot.com/
Go to Top of Page

w1102157
Yak Posting Veteran

80 Posts

Posted - 2012-09-07 : 11:02:56
this isnt working
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-09-07 : 11:07:33
If your column is of type INT, cast it to CHARand then use lionofdezert's code. Example:
CREATE TABLE #tmp(dt INT);
INSERT INTO #tmp VALUES (20120217);

SELECT SUBSTRING(CAST(dt AS CHAR(8)), 5, 2) + '/' + LEFT(CAST(dt AS CHAR(8)), 4)
FROM #tmp;

DROP TABLE #tmp;
Go to Top of Page
   

- Advertisement -