• If you know how many 100-nanosecond intervals (ticks) are in a day (864000000000) and how many days difference there are between 1-1-1601 and 1-1-1900 which is SQL Servers base date (109207) this query gets a little bit easier.

    Divide the value you get from the Active Directory query by the number of ticks in a day to get the number of days since 1-1-1601, then take away the number of days between 1-1-1601 and 1-1-1900 and the convert to a datetime.

    For example, say my AD query returned the value 129941783963332926 for one of my logins run this query below to return the value "2012-10-08 13:59:56.330"

    SELECT CONVERT(DATETIME, (129941783963332926 / 864000000000)- 109207 )

    You can check this converts to the correct value using the Windows Time Service command line utility. e.g run [font="Courier New"]w32tm.exe /ntte 129941783963332926[/font] from a command prompt.

    Here is how to do the conversion using a table.

    SET NOCOUNT ON;

    IF OBJECT_ID('tempdb..##logon','U') IS NOT NULL DROP TABLE ##logon;

    CREATE TABLE ##logon (last_logon BIGINT);

    INSERT INTO ##logon (last_logon) VALUES ('130606598434647275');

    INSERT INTO ##logon (last_logon) VALUES ('130619908502831361');

    INSERT INTO ##logon (last_logon) VALUES ('129941783963332926');

    INSERT INTO ##logon (last_logon) VALUES ('130621100686409404');

    INSERT INTO ##logon (last_logon) VALUES ('130621789182632438');

    INSERT INTO ##logon (last_logon) VALUES ('130621388365566800');

    SET NOCOUNT OFF;

    SELECT

    last_logon

    , CONVERT(DATETIME, (last_logon / 864000000000)- 109207 )

    FROM ##logon;

    DROP TABLE ##logon;

    Here is how I calculated the number of days between 1-1-1601 and 1-1-1900 (of course I could have used a newer version of SQL instead of 2005 and the datetime2 datatype to work this out).

    IF OBJECT_ID('tempdb..##years','U') IS NOT NULL DROP TABLE ##years;

    CREATE TABLE ##years(TheYear smallint, DayCount smallint);

    WITH YearsCTE (theyear)

    AS

    (

    SELECT theyear = 1601

    UNION ALL

    SELECT theyear = theyear + 1 FROM YearsCTE WHERE theyear < 1899

    )

    INSERT INTO ##years

    SELECT

    theyear

    , CASE

    WHEN (theyear % 4) <> 0 THEN 365

    WHEN (theyear % 4) = 0 AND (theyear % 100 <> 0) THEN 366

    WHEN (theyear % 4) = 0 AND (theyear % 100 = 0 ) AND (theyear % 400 = 0) THEN 366

    ELSE 365

    END AS [Days]

    FROM YearsCTE OPTION (maxrecursion 0);

    select SUM(DayCount) FROM ##years

    If you have a newer version of SQL you could run this:

    DECLARE @date1 DATETIME2 = '16010101'

    DECLARE @date2 DATETIME2 = '19000101'

    SELECT DATEDIFF(DAY,@date1, @date2)

    Some URLs I found useful for this are below.

    Method to determine whether a year is a leap year

    http://support.microsoft.com/kb/214019

    How to convert date/time attributes in Active Directory to standard time format

    https://support.microsoft.com/kb/555936?