Home Forums SQL Server 2005 Development convert DB2 datetimes to SQL Server datetimes RE: convert DB2 datetimes to SQL Server datetimes

  • so, if anyone else runs into this situation, i wrote a function to do this. it converts the db2 date, time, and datetimes into a sql server datetime. it does depend on the db2 datetime being exactly 26 chars long though.

    IF OBJECT_ID('fn_FixDateTime') IS NOT NULL

    DROP function fn_FixDateTime

    GO

    create function fn_FixDateTime (@datetime varchar(26))

    returns varchar(20)

    AS

    begin

    declare @dtdatetime varchar(20)

    set @datetime = replace(@datetime, '0001-', '1900-')

    -- is it a time only?

    if charindex('.', @datetime)>0 and charindex('-', @datetime)=0

    begin

    set @datetime = left(replace(@datetime, '.',':'),12)

    end

    -- is it a date only?

    if charindex('-', @datetime)>0 and charindex('.', @datetime)=0

    begin

    set @datetime = @datetime

    end

    -- is it a date and time?

    if charindex('.', @datetime)>0 and charindex('-', @datetime)>0

    begin

    set @datetime = replace(left(

    left(@datetime, 10) + ' ' + right(@datetime, 15)

    , len( left(@datetime, 10) + ' ' + right(@datetime, 15))-7)

    ,'.',':'

    )

    end

    set @dtdatetime = @datetime

    return @dtdatetime

    end

    go