views:

46

answers:

1

Hi, I have a column of type 'nvarchar(max)' that should now hold XML information instead of just a string.

Say: col1 has value 'abc' Now it has values, with additional info:

<el1>abc</el2>
<el2>someotherinfo</el2>

Storing the information to the column is fine, since it can still be pushed in as a string. However, extracting the same information and also using/replacing the same information 'abc' from this column that is being used in various other joins from other tables, is something I'm not able to figure out.

how can I also push in this information into abcd when it comes from another table's value 'abcd' without losing other information?

I am building an XML from the application side and updating it in a column of type nvarchar(). All the columns have been replaced to hold the XML, so the safe assumption is that the col1 only holds XML similar to that mentioned above. Just push the XML as is and it works fine. However, how should I extract the information to use it in joins?

How do I extract a particular element from this nvarchar() string to use it in a join?? Previously, this column 'Col1' was just used as a string, and a check was done like this:

where tablex.colx = table1.col1

or Update Table2 where

+4  A: 

Once you cast the NVARCHAR data to the XML data type, you can use XML functions to get element/attribute values for joining to:

WITH xoutput AS (
  SELECT CONVERT(xml, t.nvarchar_column) AS col
    FROM YOUR_TABLE t)
SELECT x.*
  FROM TABLE x
  JOIN xoutput y ON y.col.value('(/path/to/your/element)[1]', 'int') = x.id

It won't be able to use indexes, because of the data type conversion...

Alternate version, using IN:

WITH xoutput AS (
   SELECT CONVERT(xml, t.nvarchar_column) AS col
     FROM YOUR_TABLE t)
SELECT x.*
  FROM TABLE x
 WHERE x.id IN (SELECT y.col.value('(/path/to/your/element)[1]', 'int')
                  FROM xoutput)
OMG Ponies
will this work? Am trying but it doesn't seem to return anything! col2 is of type int.col1 is the XML and from this, now col2 value is the value between <el1>abc</el1> <el2>somethingelse,/el2>select convert(XML, col1) as col1 from table1;select table2.col2 = convert(int, ISNULL(col2.value(('/el1[1]'),'int'), -1)
Amy
@Amy: I fixed a typo, but those are two separate statements, but I'd check that SELECT CONVERT(XML col2).value('(/el1)[1]', 'int') FROM YOUR_TABLE` is returning correct info before I'd try JOINing to another table with it.
OMG Ponies
HiYes I'm getting the values: <br/> SELECT CONVERT(XML, col1).value('(/root/el1)[1]','int') As el1
Amy
@Amy: The next step is to confirm that the values returned from that query, exist in the table.column you're wanting to join onto.
OMG Ponies
yes they do. Also, I'm not able to do the 'where' condition for this..Like say SELECT * FROM table2 WHERE table2.col2 = CONVERT(XML, Table1.Col1).value('(/root/el1)[1]','int'); it fails on: The multi-part identifier "Table1.Col1" could not be bound.
Amy
@Amy: Because you can't reference a column in a table that is not present in a FROM clause. I updated my answer - there's an alternate version I think you'll want to see.
OMG Ponies
hey that's right. sometimes when you are in the middle of something you can't see things very clearly. It works now!!! thanks soo much!
Amy
@Amy: It's all about a second set of eyes :)
OMG Ponies