Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


convert DB2 datetimes to SQL Server datetimes


convert DB2 datetimes to SQL Server datetimes

Author
Message
bagofbirds-767347
bagofbirds-767347
Valued Member
Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)

Group: General Forum Members
Points: 68 Visits: 417
Hi guys, It seems there would be a simple solution to this problem but i've looked all over the place and can't find the fix.

I am trying to convert a column of DB2 datetimes (like: 2012-02-14-17.25.16.016164) to SQL Server datetimes. i know i could go through it with charindex/replace/left/right type functions but that is a bit messy.

I'm hoping someone knows an easier way of doing this? or perhaps has written a function i can use?

Much thanks in advance.

edit: forgot to mention, all the data is loaded into a sql server (2005) table, so i am looking to use t-sql to do this.
bagofbirds-767347
bagofbirds-767347
Valued Member
Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)

Group: General Forum Members
Points: 68 Visits: 417
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search