Converting string datetime to datetime EST from Oracle

Beacon77 131 Reputation points
2022-09-26T16:07:51.233+00:00

Hi All,
I am trying to convert datetime in string format from oracle to T SQL. The data is of the format
201808010415
In ORACLE I have the conversion as below
TO_DATE(TO_CHAR( FROM_TZ( CAST( TO_DATE(u1.P_DTM , 'YYYYMMDDHH24MI') AS TIMESTAMP ), 'UTC' ) AT TIME ZONE 'America/New_York', 'YYYYMMDDHH24MI' ) , 'YYYYMMDDHH24MI')) > ADD_MONTHS(TRUNC(SYSDATE,'MM'),-1)
I tried in T SQL as
CONVERT(VARCHAR(14),TRY_CAST(u1.P_TM AS DATETIME,102)
I am getting all NULLS.
Please advice.
Thanks

Developer technologies Transact-SQL
{count} votes

Accepted answer
  1. Wilko van de Velde 2,236 Reputation points
    2022-09-27T06:25:47.263+00:00

    If you want to switch to a different time zone use "AT TIME ZONE", more info:
    https://learn.microsoft.com/en-us/sql/t-sql/queries/at-time-zone-transact-sql?view=sql-server-ver16

    for example (thnx to @NikoXu-msft for the script):

     declare @example varchar(max) = '201808010415'  
     set @example= SUBSTRING(@example, 1, 4)+'-'+ SUBSTRING(@example, 5, 2)+'-'+SUBSTRING(@example, 7, 2)+' '+SUBSTRING(@example,9,2)+':'+SUBSTRING(@example,11,2)  
     --select @example  
     select CONVERT(datetime, @example) AT TIME ZONE 'UTC' AT TIME ZONE 'Eastern Standard Time' as dataconverted   
    

2 additional answers

Sort by: Most helpful
  1. NikoXu-msft 1,916 Reputation points
    2022-09-27T02:04:31.293+00:00

    Hi @Beacon77 ,

    Try this code:

    declare @example varchar(max) = '201808010415'  
    set @example= SUBSTRING(@example, 1, 4)+'-'+ SUBSTRING(@example, 5, 2)+'-'+SUBSTRING(@example, 7, 2)+' '+SUBSTRING(@example,9,2)+':'+SUBSTRING(@example,11,2)  
    --select @example  
    select CONVERT(datetime, @example) as dataconverted  
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    0 comments No comments

  2. Beacon77 131 Reputation points
    2022-09-27T13:36:07.41+00:00

    Thanks All
    I have used this function
    CONVERT(DATETIME,STUFF(STUFF(STUFF(STUFF(u1.P_DTM,5,0,'-'),8,0,'-'),11,0,' '),14,0,':'))
    Thanks again for your time.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.