Assuming our date column is char
or varchar
, you could do something like this
update foo
set dates = right(dates,4) + left(dates,4)
Or this
update foo
set dates = replace( convert(varchar,convert(datetime,dates,112),110) , '-' , '' )
If all you want to do is display your text differently, the easiest way is
select ... ,
dates = right(dates,4) + left(dates,4)
from foo
Or create a view and use that instead of the original table:
create view foo_view
as select id ,
dates = right(dates,4) + left(dates,4)
from foo
But if you use actual date/time data types, users of your data will get them mapped to suitable date/time types in the client and can then choose how best to display them for their needs.
The other advantage of using date/time types is that they enforce data integrity. Wait until somebody adds or changes a date to make it invalid — say, `20142331'. Then, when you need to display that data in a form with a month name ('Jan 22, 2014, say), hilarity will ensue when you get an exception trying to map the month number to a month name.
If you're not going to use a date/time type, then store year, month and day individually as integer values with suitable check constraints to enforce integrity:
create table foo
(
id int not null identity(1,1) primary key ,
yyyy int not null check ( yyyy between 1900 and 2100 ) ,
mm int not null check ( mm between 1 and 12 ) ,
dd int not null check ( dd between 1 and ( case mm
when 4 then 30
when 6 then 30
when 9 then 30
when 11 then 30
when 2 then case
when yyyy % 400 = 0 then 29
when yyyy % 100 = 0 then 28
when yyyy % 4 = 0 then 29
else 28
end
else 31
end
)
)