Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6551845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:23:15+00:00 2026-05-25T12:23:15+00:00

I need to convert a DateTime type value to BIGINT type in .Net ticks

  • 0

I need to convert a DateTime type value to BIGINT type in .Net ticks format (number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001).

The conversion should be perform in Sql server 2008 using T-SQL query

For example:

DateTime value - 12/09/2011 00:00:00

will convert to:

BIGINT value - 634513824000000000
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:23:16+00:00Added an answer on May 25, 2026 at 12:23 pm

    I have found a CodeProject article that may assist: Convert DateTime To .NET Ticks Using T-SQL

    I enclose the SQL function from the above article (I hope this is ok? As it requires registration.)

    CREATE FUNCTION [dbo].[MonthToDays365] (@month int)
    RETURNS int
    WITH SCHEMABINDING
    AS
    -- converts the given month (0-12) to the corresponding number of days into the year (by end of month)
    -- this function is for non-leap years
    BEGIN 
    RETURN
        CASE @month
            WHEN 0 THEN 0
            WHEN 1 THEN 31
            WHEN 2 THEN 59
            WHEN 3 THEN 90
            WHEN 4 THEN 120
            WHEN 5 THEN 151
            WHEN 6 THEN 181
            WHEN 7 THEN 212
            WHEN 8 THEN 243
            WHEN 9 THEN 273
            WHEN 10 THEN 304
            WHEN 11 THEN 334
            WHEN 12 THEN 365
            ELSE 0
        END
    END
    
    GO
    
    CREATE FUNCTION [dbo].[MonthToDays366] (@month int)
    RETURNS int 
    WITH SCHEMABINDING
    AS
    -- converts the given month (0-12) to the corresponding number of days into the year (by end of month)
    -- this function is for leap years
    BEGIN 
    RETURN
        CASE @month
            WHEN 0 THEN 0
            WHEN 1 THEN 31
            WHEN 2 THEN 60
            WHEN 3 THEN 91
            WHEN 4 THEN 121
            WHEN 5 THEN 152
            WHEN 6 THEN 182
            WHEN 7 THEN 213
            WHEN 8 THEN 244
            WHEN 9 THEN 274
            WHEN 10 THEN 305
            WHEN 11 THEN 335
            WHEN 12 THEN 366
            ELSE 0
        END
    END
    
    GO
    
    CREATE FUNCTION [dbo].[MonthToDays] (@year int, @month int)
    RETURNS int
    WITH SCHEMABINDING
    AS
    -- converts the given month (0-12) to the corresponding number of days into the year (by end of month)
    -- this function is for non-leap years
    BEGIN 
    RETURN 
        -- determine whether the given year is a leap year
        CASE 
            WHEN (@year % 4 = 0) and ((@year % 100  != 0) or ((@year % 100 = 0) and (@year % 400 = 0))) THEN dbo.MonthToDays366(@month)
            ELSE dbo.MonthToDays365(@month)
        END
    END
    
    GO
    
    CREATE FUNCTION [dbo].[TimeToTicks] (@hour int, @minute int, @second int)  
    RETURNS bigint 
    WITH SCHEMABINDING
    AS 
    -- converts the given hour/minute/second to the corresponding ticks
    BEGIN 
    RETURN (((@hour * 3600) + CONVERT(bigint, @minute) * 60) + CONVERT(bigint, @second)) * 10000000
    END
    
    GO
    
    CREATE FUNCTION [dbo].[DateToTicks] (@year int, @month int, @day int)
    RETURNS bigint
    WITH SCHEMABINDING
    AS
    -- converts the given year/month/day to the corresponding ticks
    BEGIN 
    RETURN CONVERT(bigint, (((((((@year - 1) * 365) + ((@year - 1) / 4)) - ((@year - 1) / 100)) + ((@year - 1) / 400)) + dbo.MonthToDays(@year, @month - 1)) + @day) - 1) * 864000000000;
    END
    
    GO
    
    CREATE FUNCTION [dbo].[DateTimeToTicks] (@d datetime)
    RETURNS bigint
    WITH SCHEMABINDING
    AS
    -- converts the given datetime to .NET-compatible ticks
    -- see https://msdn.microsoft.com/en-us/library/system.datetime.ticks(v=vs.110).aspx
    BEGIN 
    RETURN 
        dbo.DateToTicks(DATEPART(yyyy, @d), DATEPART(mm, @d), DATEPART(dd, @d)) +
        dbo.TimeToTicks(DATEPART(hh, @d), DATEPART(mi, @d), DATEPART(ss, @d)) +
        (CONVERT(bigint, DATEPART(ms, @d)) * CONVERT(bigint,10000));
    END
    
    GO
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the number 20080331. I need to cast/convert this into a datetime so
I need to convert a value which is in a DateTime variable into a
I have a DateTime object that I need to print in a custom gridlike
Both queries below translates to the same number SELECT CONVERT(bigint,CONVERT(datetime,'2009-06-15 15:00:00')) SELECT CAST(CONVERT(datetime,'2009-06-15 23:01:00')
Does mysql understand correctly 'YYYY-MM-DDThh:mm:ss' format for dateTime type? I have a few date
I am trying to convert my string formatted value to date type with format
I need to convert Datetime fields to a specifically formatted INT type. For example,
For some reason I need to cast/convert a DateTime into one of many custom
I need to convert a zope 2 DateTime object into a Python datetime object.
I need to convert from a SQLVARCHAR to a string data type Variable definitions

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.