<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Handy reporting functions (sqlserver)]]></title><description><![CDATA[<p dir="auto">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.</p>
<p dir="auto">CREATE function [dbo].[ufn_DATETIME_TO_UNIX_TIME]<br />
    ( @DAY datetime )<br />
returns  int<br />
as<br />
/*<br />
Function: ufn_DATETIME_TO_UNIX_TIME</p>
<p dir="auto">    Finds UNIX time as the difference in seconds between<br />
    1970-01-01 00:00:00 and input parameter @DAY after<br />
    rounding @DAY to the neareast whoie second.</p>
<p dir="auto">    Valid datetime range is 1901-12-13 20:45:51.500 through<br />
    2038-01-19 03:14:07.497.  This range is limited to the smallest<br />
    through the largest possible integer.</p>
<p dir="auto">    Datetimes outside this range will return null.<br />
*/<br />
begin<br />
declare @wkdt datetime</p>
<p dir="auto">-- Return null if outside of valid UNIX Time range<br />
if @DAY &lt; '1901-12-13 20:45:51.500' or  @DAY &gt; '2038-01-19 03:14:07.497'<br />
    return null</p>
<p dir="auto">-- Round off datetime to nearest whole second<br />
select @wkdt = dateadd(ms,round(datepart(ms,@DAY),-3)-datepart(ms,@DAY),@DAY)</p>
<p dir="auto">-- If date GE 1901-12-14<br />
if @wkdt &gt;= 712	return datediff(ss,25567,@wkdt)</p>
<p dir="auto">-- Handles time GE '1901-12-13 20:45:52.000 and LT 1901-12-14<br />
return -2147472000-datediff(ss,@wkdt,712)</p>
<p dir="auto">end</p>
<p dir="auto">CREATE function [dbo].[ufn_UNIX_TIME_TO_DATETIME]<br />
    ( @UNIX_TIME int )<br />
returns  datetime<br />
as<br />
/*<br />
Function: ufn_UNIX_TIME_TO_DATETIME</p>
<p dir="auto">    Converts UNIX time represented as the difference<br />
    in seconds between 1970-01-01 00:00:00 to a datetime.</p>
<p dir="auto">    Any valid integer -2,147,483,648 through 2,147,483,647<br />
    can be converted to datetime.<br />
*/<br />
begin</p>
<p dir="auto">return  	dateadd(ss,@UNIX_TIME,'1969-12-31 16:00:00')</p>
<p dir="auto">end</p>
]]></description><link>https://forum.netxms.org/topic/1141/handy-reporting-functions-sqlserver</link><generator>RSS for Node</generator><lastBuildDate>Fri, 13 Nov 2026 06:29:17 GMT</lastBuildDate><atom:link href="https://forum.netxms.org/topic/1141.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 11 Feb 2011 19:00:19 GMT</pubDate><ttl>60</ttl></channel></rss>