简体   繁体   English

从MS SQL Server 2008中的三个字段制作datetime2

[英]Making a datetime2 from three fields in Ms SQL Server 2008

I am stock whit a huge table containg more than 13,8k of rows. 我有一张巨大的桌子,里面有超过13,8k行。 Its layout is something like, 它的布局就像

CREATE TABLE table1(
[id] [int] NOT NULL,
[regnr] [char](10) NULL,
[kilde] [char](100) NULL,
[dd] [char](10) NULL,
[mm] [char](10) NULL,
[yyyy] [char](16) NULL,

)

The new table where the date part is clean up will have layout like this, plus other fields that are not important. 日期部分被清理的新表将具有这样的布局以及不重要​​的其他字段。

create table table2(id int primary key identity not null, regnr nvarchar(10), kilde nvarchar(100), dato datetime2)

I have then made a cursor and befor I execute it I turns off 然后我做了一个游标,然后在我执行它之前我将其关闭

SET IDENTITY_INSERT navneregister on

The cursor look like this but the interresting thing i the parsing of three fields into a new datetime2. 光标看起来像这样,但最有趣的是将三个字段解析为新的datetime2。

declare @id int, @regnr nvarchar(10), @kilde nvarchar(100), @composeddate nvarchar(max), @dd char(10), @mm char(10), @yyyy char(16)
declare p cursor
for select id, regnr, kilde, dd, mm, yyyy from table2
open p
fetch p into @id, @regnr, @kilde, @dd, @mm, @yyyy
while @@FETCH_STATUS != -1
    begin
    begin
    if((@mm = '0' or @mm = '00') and (@dd = '0' or @dd = '00') and (@yyyy ='0000'))
        set @composeddate = null
    end
    if(@mm = '0' or @mm = '00')
        set @mm = '01'
    if(@dd = '0' or @dd = '00')
        set @dd = '01'

    begin
    if(@yyyy = '')
        set @composeddate = null
    else
        set @composeddate = CAST(CAST(@yyyy AS char(16)) + '-' + CAST(@mm AS char(10)) + '-' + CAST(@dd AS char(10)) AS DATETIME2)
    end
    begin
        insert into table1(id, regnr, kilde, dato) 
        values (@id, @regnr, @kilde, @composeddate)
    end
    fetch p into @id, @regnr, @kilde, @dd, @mm, @yyyy
    end
close p
deallocate p

I works partly, but there are some edge case where it fails, eg if dd = 00, mm = 00 and yyyy = 0000. Also there are loads of special case like where the year aint set, but month and day are set, and my plan is the just to set the year to 0000. Also there some case where all three fields are empty. 我部分工作,但是在某些情况下会失败,例如,如果dd = 00,mm = 00和yyyy =0000。还有一些特殊情况,例如设置了年aint,但是设置了月和日,并且我的计划是仅将年份设置为0000。在某些情况下,所有三个字段都为空。 Andbody that can offer up some good advice? 可以提供一些好的建议的人吗?

What are you trying to accomplish? 你想达到什么目的? If you have bad data coming in, what data do you want to go out? 如果有坏数据输入,您想输出什么数据? Also, there is no "year 0000" because there is no year 0 . 另外,因为没有年份0 ,所以没有“年份0000”。 That puts you in an interesting position: if you deliberately create bogus dates, what do you plan to do with them? 这使您处于一个有趣的位置:如果您故意创建假日期,您打算如何处理它们? If you have a month and a day, you could group data by month and day (sort of), but what happens when someone wants to find the earliest or latest records? 如果您有一个月零一天,则可以按月和日(某种程度)对数据进行分组,但是当某人想要查找最早或最新记录时会发生什么呢? Oops. 哎呀。 Can't really do that. 真的不能那样做。

13,8K rows is actually very small, so I wonder if you have logs or anything lying around that will allow you to reconstruct the missing data? 13.8万行实际上很小,所以我想知道您是否有日志或周围的任何东西可以让您重建丢失的数据? Trying to create "good" data from bad data almost invariably means that someone's going to come along and assume your data is reliable when it's not. 试图从坏数据中创建“好”数据几乎总是意味着有人会来,并假设您的数据不是可靠的。 Identify your use cases for this data and try to figure out which ones can't work with incomplete data and proceed from there. 确定这些数据的用例,并尝试找出哪些数据不适合使用不完整的数据,然后从那里继续进行。

Also, do you have the option of eliminating the rows with bad data? 此外,您是否可以选择删除数据错误的行? There may not be much you can do with them anyway, assuming the date is important. 假设日期很重要,您可能无法对它们进行任何处理。

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

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