Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
HI guysI 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 querySELECT SUBSTRING('20120217',5,2)+'/'+LEFT('20120217',4)--------------------------http://connectsql.blogspot.com/
w1102157
Yak Posting Veteran
80 Posts
Posted - 2012-09-07 : 11:02:56
this isnt working
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;