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 format

Author  Topic 

jimdarter
Starting Member

14 Posts

Posted - 2012-05-15 : 13:52:36
Hi - I've a table that has a date column which has values written in the following format.
20110812
20110930
20110107

How can I change the format of the date to make it look something like this (or may be any other date format)
2011/08/12
2011/09/30
2011/01/07

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2012-05-15 : 14:00:17
[code]



select LEFT('20110812',4)+'/'+SUBSTRING('20110812',5,2)+'/'+right('20110812',2)

[/code]

Vijay is here to learn something from you guys.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-15 : 21:58:43
ideally you should be doing it at front end. However if its not possible you can use

SELECT REPLACE(CONVERT(varchar(11),date,121),'-','/') FROM table


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-05-16 : 05:11:23
1 Always use proper DATETIME datatype to store dates
2 Use your front end application to do the formation

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2012-05-16 : 05:45:18
quote:
Originally posted by visakh16

SELECT REPLACE(CONVERT(varchar(11),date,121),'-','/') FROM table

Why not style 111 and get rid of replace function?]
SELECT CONVERT(CHAR(10), Date, 111) FROM dbo.Table



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-05-16 : 06:42:24
If the server is later than 2008, use format function

declare @d varchar(8)
set @d='20110812'
select format(convert(datetime,@d,112),'yyyy/MM/dd')


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -