0

I have a table that contain column with VARBINARY(MAX) data type. That column represents different values for different types in my c# DB layer class. It can be: int, string and datetime. Now I need to convert that one column into three by it's type. So values with int type go to new column ObjectIntValue and so on for every new column.

But I have a problems with transmitting data to datetime column, because the old column contains datetime value as a long received from C# DateTime.ToBinary method while data saving.

I should make that in TSQL and can't using .NET for convert that value in new column. Have you any ideas?

Thanks for any advice!

4
  • Can you use the CLR integration ?
    – schglurps
    Commented Dec 11, 2011 at 14:46
  • What version of sql server, 2005 onwards you can use .net in sql server. It can be a bit slow but for a one off job like this it will be okay. Commented Dec 11, 2011 at 14:51
  • MSSQL Version is 2008 R2 but how can I use .NET class methods? For example DateTime.FromBinary() ? Commented Dec 11, 2011 at 15:06
  • As a matter of fact I should make this in tSQL quary becouse in our project have a update sql script file for DB version control. That file is a litle bit specific and we can't use TSLQ operators like GO in it. So I think it will be difficult to enable CLR integration in that script. Commented Dec 11, 2011 at 15:14

1 Answer 1

0

Using CLR in T_SQl

Basically you use Create Assembly to register the dll with your function(s) in it, Then create a user defined function to call it, then you can use it. There's several rules depending on what you want to do, but as basically you only want DateTime.FromBinary(), shouldn't be too hard to figure out.

Never done it myself, but these guys seem to know what they are talking about

CLR in TSQL tutorial

This is a one off convert right? Your response to @schglurps is a bit of a concern.

If I get you there would have to be break in your update script, ie the one you have woukld work up to when you implement this chnage, then you's have a one off procedure for this manouevre, then you would be updating from a new version.

If you want to validate it, just check for the existnec or non-existance of the new columns.

Other option would be to write a wee application that filled in the new columns from the old one and invoke it. Ugh...

If this isn't one off and you want to keep and maintain the old column, then you have problems.

3
  • I've found solution here Commented Dec 11, 2011 at 18:22
  • Might work after some twidling But DateTime.ToBinary is Kind as well as ticks. Commented Dec 11, 2011 at 21:12
  • In function body just after begining I've added: ` set @Ticks = @Ticks&cast(4611686018427387903 as bigint);` and everythings is became fine. Commented Dec 11, 2011 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.