locked
Save only time in database not date RRS feed

  • Question

  • User1052662409 posted

    Hi All,

             for saving time in database which datatype I need to take..?

             I took smalldatetime then How I can save Now's time in databse... ?

    please let me know

    thanks

    Gaurav

    Tuesday, March 27, 2012 6:45 AM

Answers

  • User-62323503 posted

    If using Sql Server 2008 or above take TIME datateype. Then fetch time part with convert function from the date and store it in the Time Field

    Example for your rererence

    DECLARE @TIME TIME
    SET @TIME = CONVERT(VARCHAR,GETDATE(),108)
    SELECT @TIME



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 27, 2012 6:54 AM
  • User-1316079624 posted

    Try something like that:

     

    update MyTable set MyColumnTime = Cast(GetDate() as Time)
    where Id = 123
    

    I hope this helps.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 27, 2012 7:21 AM

All replies

  • Tuesday, March 27, 2012 6:50 AM
  • User2105156359 posted

    Convert( VarChar( 10 ), GetDate(), 114 )


    or

    select Convert( VarChar( 10 ), getdate(), 114 )



    Tuesday, March 27, 2012 6:51 AM
  • User1052662409 posted

    first I need to insert it.

    So your mean I need to insert Datetime.Now...?

    then

    select Convert( VarChar( 10 ), getdate(), 114 )

    is it..?
    Tuesday, March 27, 2012 6:53 AM
  • User-957431521 posted

    hello

    here is the complete explanation of available date and time data types and their relevant functions:

    http://msdn.microsoft.com/en-us/library/ms186724.aspx

    if you're getting the current time using .net framework class DateTime.Now then you don't need to get it using t-sql syntax. just a simple insert will do the job.

    Tuesday, March 27, 2012 6:53 AM
  • User377791177 posted

    if you are using sql server 2008 it has a time only datatype.

    http://msdn.microsoft.com/en-us/library/bb677243.aspx

    Tuesday, March 27, 2012 6:53 AM
  • User-62323503 posted

    If using Sql Server 2008 or above take TIME datateype. Then fetch time part with convert function from the date and store it in the Time Field

    Example for your rererence

    DECLARE @TIME TIME
    SET @TIME = CONVERT(VARCHAR,GETDATE(),108)
    SELECT @TIME



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 27, 2012 6:54 AM
  • User1052662409 posted

    If using Sql Server 2008 or above take TIME datateype.

    there is time(7) datatype, so Can I insert DateTime.Now to in time(7) datatype..?

    Tuesday, March 27, 2012 6:58 AM
  • User-1316079624 posted

    Try something like that:

     

    update MyTable set MyColumnTime = Cast(GetDate() as Time)
    where Id = 123
    

    I hope this helps.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 27, 2012 7:21 AM