Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (Flatly)
  • No Skin
Collapse
Brand Logo

NetXMS Forum

  1. Home
  2. Development
  3. Handy reporting functions (sqlserver)

Handy reporting functions (sqlserver)

Scheduled Pinned Locked Moved Development
windows
1 Posts 1 Posters 24.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C
    C
    cmatthews
    wrote on last edited by
    #1

    I made some web based drillable jasperReports against the event_log tables and found these functions to be handy for date logic. I didn't write them (found them) but since they are useful for netxms table reporting thought I'd share.

    CREATE function [dbo].[ufn_DATETIME_TO_UNIX_TIME]
        ( @DAY datetime )
    returns  int
    as
    /*
    Function: ufn_DATETIME_TO_UNIX_TIME

        Finds UNIX time as the difference in seconds between
        1970-01-01 00:00:00 and input parameter @DAY after
        rounding @DAY to the neareast whoie second.

        Valid datetime range is 1901-12-13 20:45:51.500 through
        2038-01-19 03:14:07.497.  This range is limited to the smallest
        through the largest possible integer.

        Datetimes outside this range will return null.
    */
    begin
    declare @wkdt datetime

    -- Return null if outside of valid UNIX Time range
    if @DAY < '1901-12-13 20:45:51.500' or  @DAY > '2038-01-19 03:14:07.497'
        return null

    -- Round off datetime to nearest whole second
    select @wkdt = dateadd(ms,round(datepart(ms,@DAY),-3)-datepart(ms,@DAY),@DAY)

    -- If date GE 1901-12-14
    if @wkdt >= 712 return datediff(ss,25567,@wkdt)

    -- Handles time GE '1901-12-13 20:45:52.000 and LT 1901-12-14
    return -2147472000-datediff(ss,@wkdt,712)

    end

    CREATE function [dbo].[ufn_UNIX_TIME_TO_DATETIME]
        ( @UNIX_TIME int )
    returns  datetime
    as
    /*
    Function: ufn_UNIX_TIME_TO_DATETIME

        Converts UNIX time represented as the difference
        in seconds between 1970-01-01 00:00:00 to a datetime.

        Any valid integer -2,147,483,648 through 2,147,483,647
        can be converted to datetime.
    */
    begin

    return  dateadd(ss,@UNIX_TIME,'1969-12-31 16:00:00')

    end

    1 Reply Last reply
    0

    Hello! It looks like you're interested in this conversation, but you don't have an account yet.

    Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

    With your input, this post could be even better 💗

    Register Login
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    Powered by NodeBB Contributors
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups