Need help converting String format to Date in Sybase

I am trying to convert two types of string to a date format, but not able to do either of them.

The problematic input lines with the expected output are as follows:


Entrance 1: 20100614191522

Expected Result 1: 12/14/2010 7:15:22 PM


Entrance 2: 2010/12

Expected Result 2: 12/1/2010 12:00:00 AM


I tried,

select convert(datetime,'20100614191522',109) 

I tried with various style options with the "convert" function. But I always get the following errors. Syntax error when explicitly converting the VARCHAR value '20100614191522' to the DATETIME field. Msg: 249, Level: 16, Condition: 1

Could you help me how to achieve the same.

Thanks in advance.

+3
1

, , datetime data . .

1

declare @str varchar(20)
set @str = '20100614191522'
select convert(datetime,substring(@str, 5, 2 ) + '/' + substring(@str, 7, 2 ) + '/' + substring(@str, 1, 4 )  + ' ' + substring(@str, 9, 2 )  + ':' + substring(@str, 11, 2 ) + ':' + substring(@str, 13, 2 ) )

Jun 14 2010  7:15PM

2

declare @str varchar(20)
set @str = '2010/12'
select convert(datetime, @str + '/01')

Dec  1 2010 12:00AM
+2

Source: https://habr.com/ru/post/1751148/


All Articles