how to add one second

  • i need to add one second to this

    select datepart(second,Getdate())

    how ??

    thnks ilan

  • Use DATEADD.

    I cant remember the exact format, but its something like:

    DATEADD(ss,1,GETDATE())

  • Something like:

    SELECT (datepart(s,1,getdate())

  •  I meant to write:

    Something like:

    SELECT (dateadd(s,1,getdate()))

  • There should be no need to use something like DATEADD. Since DATEPART returns an INTEGER you can simply do:

    SELECT

     DATEPART(SECOND,GETDATE())+1

    to add a second.

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • You need DateAdd; 59 seconds + 1 = 0 seconds, not 60

    So this is the construction to use:

    SELECT

     DATEPART(SECOND,DATEADD(s,1,GETDATE()))

  • Actually this depends on the requirements, which haven't been stated yet.

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • Frank is correct. The question was how to add 1 (second) to

    select datepart(second,Getdate())

    which returns an integer whose units are seconds. Thus,

    SELECT DatePart(s, Getdate()) + 1

    If the problem is how to add 1 second to the current datetime, then

    SELECT DateAdd(s, 1, GetDate())

    would be the expression to use.

     

  • Why do you want to do this?  What are the requirements?

     

    Here is a fun "Select"

     

    SELECT DatePart(s, Getdate()), DatePart(s, Getdate()) + 1, GetDate(), DateAdd(s, 1, GetDate())   

  • The max number of seconds in a minute is 59, thus adding a second to 59 should indeed be 0 (as long as you're dealing with seconds as a datetime datatype)

    /Kenneth

  • That's right, but why would you bother to extract the seconds out of a DATETIME and add 1 if if all you really want is to add 1 second to the current time? But we can guess and guess over this requirement, the only one who can shed a light on this is the original questioner, who hasn't responded since his question.

     

     

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • I guess that lies in the untold requirements..?

    Since the poster extracted only the seconds 'counter' from the time, then you won't know the minutes. And since 59 + 1 seconds resets seconds to 0 and increments the minute by 1, this is the way it works as long as we're talking about seconds and not about primitive numbers

    /Kenneth

  • but what if this is for some kind of duration measurement? Might sound strange, but who knows. And in that case resetting the minutes to 0 is really smart, since you can't tell how many minutes have elapsed since beginning. Questions over questions. Slow day in Sweden today?

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • It's getting dark if nothing else.. and I'm heading for home now.. 'nuff work for this year now. See you next year

    /Kenneth

  • Yes,I know! Your fellow swedish MVP's Erland and Tibor told me that it's getting dark around 15:00 during winter. They thought I'm crazy to even consider moving to Sweden.

    Happy new year!

     

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

Viewing 15 posts - 1 through 15 (of 19 total)

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