How to concatentate date and time fields and convert to datetime stamp

Carlton Patterson 761 Reputation points
2022-03-06T12:28:01.923+00:00

Hello Community,

I am trying to concatentate date and time values which are currently created as Integer type fields and place the results into another field in the format YYYY-MM-dd hh:mm:ss

So, I have the following table

180395-tab1.png

I would like concatenate LAST_MODE_DATE and LAST_MOD_TIME and place the results in LAST_MODIFICATION_DT, so that it appears as follows:

180357-tab2.png

The data for the above is as follows:

CREATE TABLE #tmpTable (  
    LAST_MOD_DATE int,  
    LAST_MOD_TIME int,  
    LAST_MODIFICATION_USER varchar(50),  
    LAST_MODIFICATION_DT varchar(255))  
  
INSERT #tmpTable VALUES  
(NULL,NULL,NULL,NULL),  
(20181214,142254,'14843',NULL),  
(20210129,193402,'AHARDY4',NULL),  
(NULL,NULL,NULL,NULL)  
  
SELECT * FROM #tmpTable  
  

Thanks

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Viorel 125.7K Reputation points
    2022-03-06T15:09:34.547+00:00

    Try these acrobatics too:

    update #tmpTable
    set LAST_MODIFICATION_DT = format(LAST_MOD_DATE, '0000"-"00"-"00" "') + format(LAST_MOD_TIME, '00":"00":"00')
    

2 additional answers

Sort by: Most helpful
  1. Erland Sommarskog 128.9K Reputation points MVP Volunteer Moderator
    2022-03-06T13:19:12.13+00:00

    Why varchar(255) for the LAST_MODIFICATION_DT column? What's wrong with datetime2(0)? Storing date/time values in strings is a lousy idea. Sooner or later you will be looking at 2023-02-29 25:12:76 or something equally silly.

    Anyway, here is a query for you:

    UPDATE #tmpTable
    SET    LAST_MODIFICATION_DT = convert(varchar(255),
                                     convert(datetime2(0), 
                                       convert(char(8), LAST_MOD_DATE) + ' ' +
                                       convert(char(2), LAST_MOD_TIME / 10000) + ':' +
                                       convert(char(2), (LAST_MOD_TIME / 100) % 100) + ':' +
                                       convert(char(2), LAST_MOD_TIME % 100)
                                    ),
                                121)
    
    0 comments No comments

  2. Carlton Patterson 761 Reputation points
    2022-03-06T13:30:53.477+00:00

    ErlandSommarskog,

    Thanks for getting in touch.

    The "LAST_MODIFICATION_DT " will be datetime2, not VARCHAR(255) - that is my error.

    However, I was hoping you could just take concatentate the two fields and place the results in the LAST_MODIFICATION_DT field.

    The solution you provided seems very long.


Your answer

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