Get Time only from a Datetime Column

  • Hi guys,

    A small query...I am trying to get the Time part only from a DateTime column, I am using it in my S.P

    I have right(Login,7) but the problem i face is i cant display it in my datagrid in ASP.net since the column I am referencing is Login is not found?

    how do i resolve this issue?

  • Who did tell you datetime is the same as varchar?

    Why are you trying to use varchar functions for datetime values?

    There is a UDF to solve the problem:

    IF EXISTS (SELECT * FROM sysobjects WHERE  name = N'TimeOnly')

     DROP FUNCTION TimeOnly

    GO

    CREATE FUNCTION dbo.TimeOnly

     (@DT datetime)

    RETURNS datetime

    AS

    BEGIN

     RETURN convert(datetime, @dt - convert(int, @dt - 0.5))

    END

    GO

    SELECT dbo.TimeOnly (getdate())

    GO

    _____________
    Code for TallyGenerator

  • select CONVERT(VARCHAR(10), getdate(),8) ;

     

    Try this

  •  

    If you want to get the only time part, then

    use the following Query :-

     

    select convert(varchar,getdate(),108)

     

  • and this one is much better

    SELECT RIGHT(CONVERT(VARCHAR,GETDATE()),7) AS TIME

     

  • Yes, I think the main problem is naming the time column. Use "as xxx", like Veeresh above. Also explicit conversion from datetime to varchar is preferable...

  • very useful post

Viewing 7 posts - 1 through 6 (of 6 total)

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