Jamie Taylor
Jamie Taylor

Reputation: 3530

Convert string(integer) to date in SQL

I have this query at the moment

SELECT 
  year_start_1

FROM 
  table1

But i need to convert it to date

Currently it outputs just a string like this 20100731 but I want it to look like this 31/07/2010

Any ideas

Thanks

Jamie

Upvotes: 1

Views: 4135

Answers (3)

SELECT convert(datetime,   convert(varchar, year_start_1))

Upvotes: 0

Andomar
Andomar

Reputation: 238078

Convert the column to varchar:

cast(year_start_1 as varchar(16))

Then convert the result to a datetime:

convert(datetime, '20100731', 103)

Combining the two:

select  convert(datetime, cast(year_start_1 as varchar(16)), 103)
from    table1

Upvotes: 1

anishMarokey
anishMarokey

Reputation: 11397

 SELECT convert(varchar,   convert(datetime,'20100731'), 103)

for different format: http://anubhavg.wordpress.com/2009/06/11/how-to-format-datetime-date-in-sql-server-2005/

Upvotes: 3

Related Questions