locked
convert varchar(4) to time RRS feed

  • Question

  • Dear.

    in database I have char(4) which I need to change into time

    for example: in my column i have record like 0900 or 2300...how can I convert that values into time 09:00:00 or 23:00:00

     

    thanks for helping me

    Tuesday, January 4, 2011 11:54 PM

Answers

  • DECLARE @a char(4) = '0900';
    SELECT CAST(STUFF(@a, 3, 0, ':') AS time);

    Hugo Kornelis, SQL Server MVP
    My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
    • Proposed as answer by Naomi N Wednesday, January 5, 2011 12:27 AM
    • Unproposed as answer by KJian_ Thursday, January 6, 2011 3:49 AM
    • Proposed as answer by KJian_ Thursday, January 6, 2011 3:49 AM
    • Marked as answer by KJian_ Tuesday, January 11, 2011 8:08 AM
    Wednesday, January 5, 2011 12:10 AM
  • Hi,

    On SQL 2008 and above you can use

    declare @i char(4)
    select @i = '0900'
    
    select CAST( SUBSTRING(@i,1,2) + ':00' as time(0))
    
    

    -Chintak (My Blog)

    • Edited by Chintak Chhapia Wednesday, January 5, 2011 12:14 AM Formatted
    • Proposed as answer by Naomi N Wednesday, January 5, 2011 12:26 AM
    • Marked as answer by KJian_ Tuesday, January 11, 2011 8:08 AM
    Wednesday, January 5, 2011 12:13 AM
  • Dear.

    in database I have char(4) which I need to change into time

    for example: in my column i have record like 0900 or 2300...how can I convert that values into time 09:00:00 or 23:00:00

     

    thanks for helping me

    Hi,

    Which version of SQL Server are you using? 

    The time data type was first introduced in SQL Server 2008. Prior to SQL Server 2008, we only have datetime and smalldatetime to store date and time values. They have both date and time parts. If we don’t specify a date part, SQL Server will use ‘1900-01-01’ as the date part.

    Please see:

    DECLARE @t CHAR(4)
    SET @t = '0900'
    
    SELECT CONVERT(DATETIME,STUFF(@t,3,0,':')+':00')

    For more information, please also refer to: http://www.karaszi.com/SQLServer/info_datetime.asp


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by KJian_ Tuesday, January 11, 2011 8:08 AM
    Thursday, January 6, 2011 3:57 AM

All replies

  • DECLARE @a char(4) = '0900';
    SELECT CAST(STUFF(@a, 3, 0, ':') AS time);

    Hugo Kornelis, SQL Server MVP
    My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
    • Proposed as answer by Naomi N Wednesday, January 5, 2011 12:27 AM
    • Unproposed as answer by KJian_ Thursday, January 6, 2011 3:49 AM
    • Proposed as answer by KJian_ Thursday, January 6, 2011 3:49 AM
    • Marked as answer by KJian_ Tuesday, January 11, 2011 8:08 AM
    Wednesday, January 5, 2011 12:10 AM
  • Hi,

    On SQL 2008 and above you can use

    declare @i char(4)
    select @i = '0900'
    
    select CAST( SUBSTRING(@i,1,2) + ':00' as time(0))
    
    

    -Chintak (My Blog)

    • Edited by Chintak Chhapia Wednesday, January 5, 2011 12:14 AM Formatted
    • Proposed as answer by Naomi N Wednesday, January 5, 2011 12:26 AM
    • Marked as answer by KJian_ Tuesday, January 11, 2011 8:08 AM
    Wednesday, January 5, 2011 12:13 AM
  • Dear.

    in database I have char(4) which I need to change into time

    for example: in my column i have record like 0900 or 2300...how can I convert that values into time 09:00:00 or 23:00:00

     

    thanks for helping me

    Hi,

    Which version of SQL Server are you using? 

    The time data type was first introduced in SQL Server 2008. Prior to SQL Server 2008, we only have datetime and smalldatetime to store date and time values. They have both date and time parts. If we don’t specify a date part, SQL Server will use ‘1900-01-01’ as the date part.

    Please see:

    DECLARE @t CHAR(4)
    SET @t = '0900'
    
    SELECT CONVERT(DATETIME,STUFF(@t,3,0,':')+':00')

    For more information, please also refer to: http://www.karaszi.com/SQLServer/info_datetime.asp


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by KJian_ Tuesday, January 11, 2011 8:08 AM
    Thursday, January 6, 2011 3:57 AM
  • Here is another solution.

    DECLARE 
    @StrTime CHAR(4) = '1600',
    @Time TIME
    SELECT @Time = STUFF(@StrTime,3,1,':')
    SELECT CONVERT(VARCHAR(20), @Time, 100)

    • Proposed as answer by Ashish.Upadhyay Tuesday, September 24, 2013 10:58 PM
    Tuesday, September 24, 2013 10:58 PM