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, e.g 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?