Aiden Aiden - 21 days ago 7
SQL Question

UPDATE value of String in a column SQL Server

I have a VARCHAR column called PROPERTYDATA in table PERSON which has data like this

<java.lang.String valueOf="ALLOWUSER" />
<java.lang.String valueOf="true" />
<java.lang.String valueOf="STATUSTASK" />
<java.lang.String
valueOf="${param.TaskOpen eq true ? true : &apos;&apos;" />
<java.lang.String valueOf="PROPERTYCOLOR" />
<java.lang.String valueOf="1" />


This actually shows fields created in system with their values below it e.g. value of variable name ALLOWUSER is true.

The value of STATUSTASK will always be ${param.TaskOpen eq true ? true ...

I want to write two queries


  1. Update query which will UPDATE the value of field STATUSTASK to empty i.e. "" WHERE PROPERTYCOLOR value is 1 in EACH ROW. IF the ROW data has PROPERTYCOLOR not 1 then we should leave it as it is.



so result will be

<java.lang.String valueOf="ALLOWUSER" />
<java.lang.String valueOf="true" />
<java.lang.String valueOf="STATUSTASK" />
<java.lang.String
valueOf="" />
<java.lang.String valueOf="PROPERTYCOLOR" />
<java.lang.String valueOf="1" />



  1. DELETE the field name and value WHERE PROPERTYCOLOR value is 1 i.e. after the query the row should now be

    <java.lang.String valueOf="ALLOWUSER" />
    <java.lang.String valueOf="true" />
    <java.lang.String valueOf="PROPERTYCOLOR" />
    <java.lang.String valueOf="1" />



Should we do like this ?

UPDATE PERSON SET PROPERTYDATA = (SUBSTRING (PROPERTYDATA))... I am stuck, please help

Thanks,

Aiden

Answer

I don't have an answer so to speak, but I've had a look at this and have managed to write some queries that convert to XML and query the data, which might help you get to an eventual solution, so I'll post what I've done:

CREATE TABLE #temp
    (
      propertydata VARBINARY(MAX) ,
      propertyxml XML
    )

INSERT  INTO #temp
        ( propertydata ,
          propertyxml
        )
VALUES  ( CAST(' <java.lang.String valueOf="ALLOWUSER" />
    <java.lang.String valueOf="true" />
    <java.lang.String valueOf="STATUSTASK" />
    <java.lang.String
        valueOf="${param.TaskOpen eq true ? true : &apos;&apos;" />
    <java.lang.String valueOf="PROPERTYCOLOR" />
    <java.lang.String valueOf="1" />' AS VARBINARY(MAX)) ,
          CAST(' <java.lang.String valueOf="ALLOWUSER" />
    <java.lang.String valueOf="true" />
    <java.lang.String valueOf="STATUSTASK" />
    <java.lang.String
        valueOf="${param.TaskOpen eq true ? true : &apos;&apos;" />
    <java.lang.String valueOf="PROPERTYCOLOR" />
    <java.lang.String valueOf="1" />' AS XML)
        )

SELECT  propertydata ,
        propertyxml
FROM    #temp

SELECT  t2.Loc.query('.') XmlRow ,
        t2.Loc.query('.').value('(/java.lang.String/@valueOf)[1]',
                                'nvarchar(100)') AS AttributeValue
FROM    #temp
        CROSS APPLY propertyxml.nodes('java.lang.String') AS t2 ( loc )
WHERE   t2.Loc.query('.').value('(/java.lang.String/@valueOf)[1]',
                                'nvarchar(100)') IS NOT NULL

DROP TABLE #temp

Produces:

XmlRow                                                       AttributeValue
<java.lang.String valueOf="ALLOWUSER" />                     ALLOWUSER
<java.lang.String valueOf="true" />                          true
<java.lang.String valueOf="STATUSTASK" />                    STATUSTASK
<java.lang.String valueOf="${param.... />                     ${param....
<java.lang.String valueOf="PROPERTYCOLOR" />                 PROPERTYCOLOR
<java.lang.String valueOf="1" />                             1