Datepart function question

  • Is there a way to get the following to return the seconds as 2 digits??

    SELECT DATEPART(ss, '2003-11-17 09:08:05.223')

    currently it returns 5 but I need to pad it with a 0.   Any suggestions??

    Thanks,

    Veronica

  • SELECT right(convert(varchar(8), cast('2003-11-17 09:08:05.223' as datetime), 108), 2)

     


    Ronald San Juan | SQL DBA
    ID 710124 ~ Code "Northwind"

  • select REPLACE(STR(5, 2), ' ', '0')

    "5" is a result of your DATEPART function.

    _____________
    Code for TallyGenerator

  • I'm not saying that this is any fster or better, but just another alternative:

    Right(

    '0' + Cast(datepart(ss, '2003-11-17 09:08:05.223') As varchar(2)), 2)


    My blog: SQL Soldier[/url]
    SQL Server Best Practices:
    SQL Server Best Practices
    Twitter: @SQLSoldier
    My book: Pro SQL Server 2008 Mirroring[/url]
    Microsoft Certified Master: SQL Server, Data Platform MVP
    Database Engineer at BlueMountain Capital Management[/url]

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply