如何在T-SQL中将DateTime转换为精度大于天的数字?

发布于 2024-07-24 04:40:29 字数 335 浏览 4 评论 0原文

下面的两个查询都转换为相同的数字

SELECT CONVERT(bigint,CONVERT(datetime,'2009-06-15 15:00:00'))
SELECT CAST(CONVERT(datetime,'2009-06-15 23:01:00') as bigint)

结果

39978
39978

仅当日期不同时,生成的数字才会不同。 有什么方法可以将 DateTime 转换为更精确的数字,就像我们在 .NET 中使用 .Ticks 属性所做的那样?

我需要至少一分钟的精度。

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') as bigint)

Result

39978
39978

The generated number will be different only if the days are different. There is any way to convert the DateTime to a more precise number, as we do in .NET with the .Ticks property?

I need at least a minute precision.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

何必那么矫情 2024-07-31 04:40:30

好吧,我会这样做:

select datediff(minute,'1990-1-1',datetime)

其中“1990-1-1”是任意基准日期时间。

Well, I would do it like this:

select datediff(minute,'1990-1-1',datetime)

where '1990-1-1' is an arbitrary base datetime.

对岸观火 2024-07-31 04:40:30
SELECT CAST(CONVERT(datetime,'2009-06-15 23:01:00') as float)

产量 39977.9590277778

SELECT CAST(CONVERT(datetime,'2009-06-15 23:01:00') as float)

yields 39977.9590277778

你与昨日 2024-07-31 04:40:30
DECLARE @baseTicks AS BIGINT;
SET @baseTicks = 599266080000000000; --# ticks up to 1900-01-01

DECLARE @ticksPerDay AS BIGINT;
SET @ticksPerDay = 864000000000;

SELECT CAST(@baseTicks + (@ticksPerDay * CAST(GETDATE() AS FLOAT)) AS BIGINT) AS currentDateTicks;
DECLARE @baseTicks AS BIGINT;
SET @baseTicks = 599266080000000000; --# ticks up to 1900-01-01

DECLARE @ticksPerDay AS BIGINT;
SET @ticksPerDay = 864000000000;

SELECT CAST(@baseTicks + (@ticksPerDay * CAST(GETDATE() AS FLOAT)) AS BIGINT) AS currentDateTicks;
情话已封尘 2024-07-31 04:40:30

如果这样做的目的是从日期创建一个唯一的值,那么我会这样做

DECLARE @ts TIMESTAMP 
SET @ts = CAST(getdate() AS TIMESTAMP)
SELECT @ts

这获取日期并将其声明为一个简单的时间戳

If the purpose of this is to create a unique value from the date, here is what I would do

DECLARE @ts TIMESTAMP 
SET @ts = CAST(getdate() AS TIMESTAMP)
SELECT @ts

This gets the date and declares it as a simple timestamp

可遇━不可求 2024-07-31 04:40:30

为此,请使用 DateDiff

DateDiff (DatePart, @StartDate, @EndDate)

DatePart 从“年”降到“纳秒”。

更多信息请参见.. http://msdn.microsoft.com/en-us/库/ms189794.aspx

Use DateDiff for this:

DateDiff (DatePart, @StartDate, @EndDate)

DatePart goes from Year down to Nanosecond.

More here.. http://msdn.microsoft.com/en-us/library/ms189794.aspx

孤云独去闲 2024-07-31 04:40:30

这是相同的 bigint 版本

DECLARE @ts BIGINT 
SET @ts = CAST(CAST(getdate() AS TIMESTAMP) AS BIGINT)
SELECT @ts

And here is a bigint version of the same

DECLARE @ts BIGINT 
SET @ts = CAST(CAST(getdate() AS TIMESTAMP) AS BIGINT)
SELECT @ts
酒儿 2024-07-31 04:40:30

CAST 为浮点数或小数,而不是 int/bigint。

整数部分(小数点前)表示整天数。 小数点后面是小数天数(即时间)。

CAST to a float or decimal instead of an int/bigint.

The integer portion (before the decimal point) represents the number of whole days. After the decimal are the fractional days (i.e., time).

洒一地阳光 2024-07-31 04:40:30

您可以使用 T-SQL 在日期到达 .NET 程序之前对其进行转换。 如果您不需要在 .NET 程序中进行额外的日期转换,那么这通常会更简单。

DECLARE @Date DATETIME = Getdate()
DECLARE @DateInt INT = CONVERT(VARCHAR(30), @Date, 112)
DECLARE @TimeInt INT = REPLACE(CONVERT(VARCHAR(30), @Date, 108), ':', '')
DECLARE @DateTimeInt BIGINT = CONVERT(VARCHAR(30), @Date, 112) + REPLACE(CONVERT(VARCHAR(30), @Date, 108), ':', '')
SELECT @Date as Date, @DateInt DateInt, @TimeInt TimeInt, @DateTimeInt DateTimeInt

Date                    DateInt     TimeInt     DateTimeInt
------------------------- ----------- ----------- --------------------
2013-01-07 15:08:21.680 20130107    150821      20130107150821

You can use T-SQL to convert the date before it gets to your .NET program. This often is simpler if you don't need to do additional date conversion in your .NET program.

DECLARE @Date DATETIME = Getdate()
DECLARE @DateInt INT = CONVERT(VARCHAR(30), @Date, 112)
DECLARE @TimeInt INT = REPLACE(CONVERT(VARCHAR(30), @Date, 108), ':', '')
DECLARE @DateTimeInt BIGINT = CONVERT(VARCHAR(30), @Date, 112) + REPLACE(CONVERT(VARCHAR(30), @Date, 108), ':', '')
SELECT @Date as Date, @DateInt DateInt, @TimeInt TimeInt, @DateTimeInt DateTimeInt

Date                    DateInt     TimeInt     DateTimeInt
------------------------- ----------- ----------- --------------------
2013-01-07 15:08:21.680 20130107    150821      20130107150821
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文