Welcome

首页 / 数据库 / SQLServer / 如何使用SQL生成DateTime.Ticks

如何使用SQL生成DateTime.Ticks2015-02-18在项目中我需要使用到一个随机数(Random Number),该随机数将作为 Hashtable 中的 Key 用于唯一索引数据,所以需要保持单机唯一性。

同时该随机数还需要具备可排序性以便对数据进行排序。

此时,我可以考虑使用随机字符串《C#生成MongoDB中的ObjectId》,这里的 ObjectId 具备单机唯一性和可排序性。

但另一个需求是,我需要该随机数为 int 或 long 类型,以便与依赖的 Service 进行通信,部分取代 TransactionId 或 Token 的职责。

所以,很自然的我就想到了时间。

查看了 MSDN 上关于 DateTime.MinValue 和 DateTime.Ticks 的定义,考虑到我所设计的服务每秒不会产生多于 10000000 的数据,

我决定使用 DateTime.Ticks 作为随机数的起始点,这样就有了下面这段代码。

public static class UniqueId{private static long _sequence = DateTime.UtcNow.Ticks;public static long Generate(){return Interlocked.Increment(ref _sequence);}public static long Sequence{get{return _sequence;}}}
业务逻辑中会将 UniqueId 存储到 SQL Server 数据库中,使用 bigint 类型进行存储。

1 CREATE TABLE [TableName](
2     [UniqueId] [bigint] NOT NULL,
3 );

这样,在数据库中,该 UniqueId 即可作为索引,也可进行排序。

现在问题来了,我需要将一个较老的服务升级到新的服务中,做 Date Migration。

老的服务中没有 UniqueId 逻辑和数据,这就需要我在数据迁移时为每条表数据生成该 UniqueId。

显然,SQL Server 不会帮我做这件事,我需要使用 SQL Script 来生成 UniqueId。

DateTime.Ticks 的定义是:

A single tick represents one hundred nanoseconds or one ten-millionth of a second. There are 10,000 ticks in a millisecond.

1 Tick 等于千万分之一秒,或万分之一毫秒。

The value represents the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001, which represents DateTime.MinValue.

DateTime.Ticks 代表着从时间 "00:00:00.0000000, January 1, 0001" 至当前时间的 Tick 总数。

这样,我们就可以根据其原理通过 SQL 来计算其值。

DECLARE @current_datetime DATETIME;DECLARE @days_from_0001_to_1900 BIGINT;DECLARE @ticks_per_millisecond BIGINT;DECLARE @ticks_per_day BIGINT;DECLARE @millisecond_of_time BIGINT;DECLARE @ticks_of_days BIGINT;DECLARE @ticks_of_time BIGINT;DECLARE @ticks BIGINT;SET @current_datetime = GETUTCDATE();SET @days_from_0001_to_1900 = 693595;SET @ticks_per_millisecond = 10000;SET @ticks_per_day = 24 * 3600 * 1000 * @ticks_per_millisecond;SET @millisecond_of_time = DATEPART(MILLISECOND, @current_datetime) + (DATEPART(SECOND, @current_datetime)) * 1000 + (DATEPART(MINUTE, @current_datetime)) * 60000 + (DATEPART(HOUR, @current_datetime)) * 3600000;SET @ticks_of_days = CAST(@days_from_0001_to_1900 + DATEDIFF(DAY, 0, @current_datetime) AS BIGINT) * @ticks_per_day;SET @ticks_of_time = @millisecond_of_time * @ticks_per_millisecond;SET @ticks = @ticks_of_days + @ticks_of_time;SELECT @current_datetime, @ticks

这段 SQL 脚本的运行结果是:

我们通过下面这段 C# 代码来验证结果。

class Program{static void Main(string[] args){DateTime d1 = new DateTime(2014, 04, 01, 02, 52, 54, 153, DateTimeKind.Utc);DateTime d2 = DateTime.Parse("2014-04-01 02:52:54.153");DateTime d3 = DateTime.Parse("2014-04-01 02:52:54.153",CultureInfo.CurrentCulture, DateTimeStyles.AdjustToUniversal);Console.WriteLine(d1.Ticks);Console.WriteLine(d2.Ticks);Console.WriteLine(d3.Ticks);Console.ReadKey();}}
瞧,它们的结果一致为 635319175741530000。