Showing posts with label ssis. Show all posts
Showing posts with label ssis. Show all posts

Tuesday, February 14, 2012

DATEADD with 8 byte int?

I have a bigint column called "MillisecondsSince1970" that I need to convert to a date - SSIS is erroring out when I use DATEADD with the 8 byte int (if I use 4 byte it works but the column is bigger than 4 byte). The error is really lame:

[Derived Column [79]] Error: The "component "Derived Column" (79)" failed because error code 0xC0049067 occurred, and the error row disposition on "output column "Date" (100)" specifies failure on error. An error occurred on the specified object of the specified component.

Anyone have a way around it... a VB.NET equivalent of DATEADD or something else I can do?

Why not do it in stages? Convert milliseconds to say, days, and then add that to your date via dateadd. Then take the remaining milliseconds and add to the new date field. For each step, cast to INT and then pass into dateadd.

So, 12636598775 milliseconds = 146 days, 22198775 milliseconds

In a derived column, for instance:
days = (DT_I4)(DT_NUMERIC, 30, 8)([millisecond_column] / 1000 / 60 / 60 / 24)

In a second derived column:
newdatecolumn = dateadd("d", [days], [date_column])
finaldatecolumn = dateadd("ms", (DT_I4)([millisecond_column] - ([days] * 24 * 60 * 60 * 1000)),[newdatecolumn])|||Phil that is an excellent idea... I kept trying ways to do it in one object - didn't think about doing it like that. (FYI VB.NET doesn't have milliseconds as a DateInterval so you can't do it in script without doing something like phil suggests)

DateAdd expression works in tsql but doesn't work in ssis

Hi There,

I am trying to set a variable with this default value using expression. This works in tsql but doesn't in ssis. Can anybody tell me what is wrong with this?

dateadd("dd", -1, datediff("dd", 0, getdate()))

Thanks.

Some more info please. What do you mean by "it doesn't work"? Do you get an error or the wrong result?

If the latter, tell us what you result you get and also what result you are expecting to get.

Thanks

-Jamie

|||DateDiff returns an integer while DateAdd expects a datetime in that position. T-SQL is able to implicitly cast dates to integers, while SSIS cannot.

|||

Ok..If you run the below query in query analyzer..

select dateadd("dd", -1, datediff("dd", 0, getdate()))

it gives me.."2007-05-08 00:00:00.000". I would like to get the same value in ssis. In ssis, if I use the above as an expression for a variable, I get a design time error. "The expression for variable failed evaluation, there was an error in the expression".

Thanks for responding.

|||

Ok..you are right..so can i cast it like this..

dateadd("dd", -1, (DT_DBTIMESTAMP)(datediff("dd", 0, getdate()))). This doesn't work either. How do I cast it?

Thanks.

|||

Sam_res03 wrote:

Ok..you are right..so can i cast it like this..

dateadd("dd", -1, (DT_DBTIMESTAMP)(datediff("dd", 0, getdate()))). This doesn't work either. How do I cast it?

Thanks.

You'd have to use DateAdd to perform the cast from integer to date and thus define 0 as 1/1/1900 the way T-SQL does.

dateadd("dd", -1,
dateadd("dd",
datediff("dd",
dateadd("dd",0,(DT_DBDATE)"1/1/1900")
, getdate())
,(DT_DBDATE)"1/1/1900")
)

|||

Hi Jay,

Thanks for your reply. I really appreciate it. Event though your sol works, I thought I would use this instead..

(DT_DBTIMESTAMP)((DT_WSTR,4) Year( DateAdd("d",-1,getdate())) + "-"+(DT_WSTR,4) Month( DateAdd("d",-1,getdate()))+"-"+(DT_WSTR,4) Day(DateAdd("d",-1,getdate())) + (DT_WSTR,12)" 00:00:00") as this was much readable. I am sure this works for all situations.

So

(DT_DBTIMESTAMP)((DT_WSTR,4) Year( DateAdd("d",-1,getdate())) + "-"+(DT_WSTR,4) Month( DateAdd("d",-1,getdate()))+"-"+(DT_WSTR,4) Day(DateAdd("d",-1,getdate())) + (DT_WSTR,12)" 00:00:00")

gives 5/8/2007 00:00:00

and

(DT_DBTIMESTAMP)((DT_WSTR,4) Year( DateAdd("d",-1,getdate())) + "-"+(DT_WSTR,4) Month( DateAdd("d",-1,getdate()))+"-"+(DT_WSTR,4) Day(DateAdd("d",-1,getdate())) + (DT_WSTR,12)" 23:59:59")

gives 5/8/2007 11:59 PM

I am not sure which one is efficient though, probably yours...

Thanks