简体   繁体   English

将日期转换为数字数据类型

[英]Convert date to number data type

I am trying to convert date to number like below, not sure which function works better.我正在尝试将日期转换为数字,如下所示,不确定哪个函数效果更好。

Database used is SQL Server.使用的数据库是 SQL Server。

Table details表详细信息

create table test 
(
    id varchar(255),
    call_date varchar(255)
);

insert into test('26203', '14-Aug-2020');

I need output as 4405726203 -- its concatenation of date ( 14-Aug-2014 ) + id ( 26203 )我需要输出为 4405726203——它的日期( 14-Aug-2014 )+ id( 26203 )的串联

This is too long for a comment.评论太长了。

SQL Server allows you to convert a datetime to a float. SQL Server 允许您将datetime时间转换为浮点数。 That would be:那将是:

select cast(dte as float)
from (values (convert(datetime, '14-Aug-2020'))) v(dte)

However, the corresponding floating point value is 44055 ( https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=d142a64db0872e7572eb4fbd6d5d5fe7 ).但是,对应的浮点值为 44055 ( https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=d142a64db0872e7572eb4fbd6d5d5fe7 )。 It is a bit of mystery what your intention is.你的意图是什么有点神秘。

You could subtract 2, but that seems arbitrary.你可以减去 2,但这似乎是任意的。 You could calculate the number of days since 1899-12-30.您可以计算自 1899-12-30 以来的天数。 But that also seems arbitrary.但这似乎也是任意的。

In any case, once you figure out how to convert the date to the number you want, just use concat() to combine the values.无论如何,一旦您弄清楚如何将日期转换为您想要的数字,只需使用concat()来组合这些值。

我找到了解决方案:

convert(varchar,CAST(CONVERT(datetime,call_date) as bigint)) + id

Under the hood, a SQL Server DateTime is a tuple of 2 32-bit integers:在幕后,SQL Server DateTime是一个由 2 个 32 位整数组成的元组:

  • The first integer is a count of days since since the epoch, which for SQL Server is 1 January 1900第一个整数是自纪元以来的天数,对于 SQL Server,它是1 January 1900
  • The second integer is a count of milliseconds since start of day ( 00:00:00.000 ).第二个整数是自一天开始 ( 00:00:00.000 ) 以来的毫秒数。 Except that the count ticks up in 3- or 4-milliscond increments.除了计数以 3 或 4 毫秒的增量递增。 Microsoft only knows why that decision was made.微软只知道为什么做出这个决定。

You can get the count of days since the epoch with您可以获得自纪元以来的天数

convert( int, convert( date, t.call_date ) )

[wrap that in convert(varchar, ... ) to turn it into a string] [将其包装在convert(varchar, ... )以将其转换为字符串]

Looks like your id is already a varchar , so you can say:看起来你的id已经是一个varchar ,所以你可以说:

select compound_key = convert(varchar,
                        convert(int,
                          convert(date,
                            call_date
                          )
                        )
                      )
                    + t.id
from test t

I would suggest padding both fields with leading zeros to a fixed length so as to avoid possible collisions (assuming you're trying to generate a key here).我建议用前导零填充两个字段到固定长度,以避免可能的冲突(假设您尝试在此处生成密钥)。 Signed 32-bit integer overflows a 2.1 billion-ish, so 9 digits for each field is sufficient.有符号的 32 位整数溢出 21 亿,因此每个字段 9 位就足够了。

This works这有效

select concat(datediff(d, 0, cast(call_date as date)), id)
from 
  (values ('26203','14-Aug-2020')) v(id, call_date);

Results结果

4405526203

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM