简体   繁体   中英

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. 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.

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. 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 . 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? 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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