0

I have a value of 20070807000000 i tried CONVERT (datetime,convert(char(8),20070807000000)) but i'm getting the error Arithmetic overflow error converting expression to data type varchar.

What can i do in order to get this to a datetime?

3 Answers 3

0

Can you try this,

select 
    CONVERT (datetime, convert(char(8), left(ltrim(20070807000000), 8)))

Result:2007-08-07 00:00:00.000

0

This should work:

SELECT CAST(SUBSTRING(CAST(20070807000000 as varchar(20)),1,8) as datetime)

Result: August, 07 2007 00:00:00+0000

Demo

2
  • 2
    Oh noes! VARCHAR without length? Commented Aug 21, 2014 at 13:41
  • Also why not just CONVERT(DATETIME, CONVERT(CHAR(8), then you don't have to mess with substring... Commented Aug 24, 2014 at 18:55
0

An extra answer. You can provide a datetime format to your output:

SELECT  CONVERT(VARCHAR, CAST( SUBSTRING(CAST(20070807000000 AS VARCHAR),1,8) AS DATETIME ), 103)

Result: 07/08/2007


SELECT  CONVERT(VARCHAR, CAST( SUBSTRING(CAST(20070807000000 AS VARCHAR),1,8) AS DATETIME ), 111)

Result: 2007/08/07


For more information about Date formats: CAST and CONVERT (Transact-SQL)

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.