0

I have DateTime saved as Integer in row of table in format 20111111. I want to display it by select to : 2011-11-11.

Please give me some as simpliest solution.

The Datetime is in table and looks like:

Table_name
20111212
20111113
20111212

and i want to display it like

Table_name
2011-12-12
2011-11-13
2011-12-12

MS SQL Server 2012

Michal
  • 563
  • 3
  • 14
  • 24

4 Answers4

1

Please try

declare @a int
    set @a ='20111212'

    select stuff(stuff(@a,len(@a)-3,0,'-'),len(@a)-0,0,'-')

    --> 2011-12-12

fiddle demo

Nunser
  • 4,512
  • 8
  • 25
  • 37
vhadalgi
  • 7,027
  • 6
  • 38
  • 67
0

You can cast it to varchar, then to datetime, then use CONVERT with the proper style:

SELECT CONVERT(VARCHAR(10),CAST(CAST(20111111 AS VARCHAR(10))AS DATETIME), 120)

Demo

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

Try this

CONVERT(varchar, Year(column)) +'_' + CONVERT(varchar, Month(column)) +'_'+ 
  CONVERT(varchar, Date(column))
ranjeet
  • 540
  • 7
  • 16
0

Problem solved by:

CONVERT(date, CAST(dbo.database_name.table_name AS CHAR(12)), 112) AS table_name
Michal
  • 563
  • 3
  • 14
  • 24