Update XML as Upper Case in SQL Server - sql

I store an XML string in a column in my SQL Server database. I'm trying to run an UPDATE statement that will make a certain field into Upper Case. I can do that easily during a SELECT but can't get it to work with an update.
This SELECT statement works
select Upper(XmlTest.value('(/CodeFiveReport/Owner/#UnitNumber)[1]', 'varchar(10)')) as UnitNumber
from uploadreport
But I want to update the XML as that permanently
Update table Set XmlString.Modify('replace value of (/Root/Node/#Field)[1] with ?

upper-case was added to SQL Server in SQL Server 2008, so there's no good solution prior to that unless you want to use cursors. The following works in SQL Server 2008:
declare #xml xml
set #xml = N'<CodeFiveReport><Owner UnitNumber="Mixed"/></CodeFiveReport>'
select T1.C1.value('upper-case((/CodeFiveReport/Owner/#UnitNumber)[1])', 'varchar(10)') from #xml.nodes('/') T1(C1)
SET #xml.modify('
replace value of (/CodeFiveReport/Owner/#UnitNumber)[1]
with xs:string(upper-case((/CodeFiveReport/Owner/#UnitNumber)[1]))
')
select #xml

Use the XQuery function upper-case

Related

How to declare variable in vectorWise like SQL Server?

I am new to VectorWise database.
I need to declare variable and pass value in it same like we do in a SQL Server database. Please help me how to do this in VectoreWise database. I am using Squirrel as SQL client.
I need to do it like we do in SQL Server:
Declare #name varchar (100)
set #name ='ABC'
select #name
Output: ABC
I am not particularly familiar with Actian Vector, so I'm not sure if it has a scripting language. I don't see declare as a supported statement.
If you only need a parameter in a single select, you can use a CTE. For your example:
with params as (
select 'ABC' as name
)
select params.name
from params;
I'm not sure if this helps you, but it might.

SQL Server Select XML Column Based on Content by User Input Values

I am developing an sql server 2012 based application.
I am a newbie to SQl server. But the requirement is to use it.
One of the table I am using contains an XML Datatype column. However the data containing in that column may vary as per the xml element. The only thing in common is the root: For instance this is a sample data:
<Tags>
<key1>Value1</key1>
<key2>Value2</key2>
<key3>Value3</key3>
<key4>Value4</key4>
<key5>Value5</key5>
<key6>Value6</key6>
</Tags>
What I want to do is to query the whole table and fetch records that will match a specific key and a specific values sent by the user.
Please assist me.
Well it sounds like you need to use some variables to build an XQuery, yes? So, assuming you build a stored procedure or something which takes a pair of string arguments for key and value you could use the following example I've knocked up in SQL Fiddle so you can try it out.
DECLARE #key nvarchar(20)
DECLARE #value nvarchar(20)
SET #key = N'key5'
SET #value = N'Value5'
SELECT
TagValue = T1.xmlcol.value('(/Tags/*[local-name()=sql:variable("#key")])[1]', 'varchar(10)')
FROM
dbo.T1
WHERE
T1.xmlcol.exist('/Tags/*[local-name()=sql:variable("#key")][text() = "Value5"]') = 1

SQL Server 2000 XML PATH('') error

SELECT
**FIELDS**
AS [text()] --Stops the XMLPATH line rendering output as XML
FROM #temp
WHERE **CONDITIONS**
FOR XML PATH('')
This doesn't work in SQL server 2000 (unsupported).
I have tried using FOR XML RAW but it returns loads of useless information. i.e:
<row text x0028 x0029="blah, blah"> <row text x0028 x0029="blah">
The above code currently returns a concatenated string(made up of multiple columns of varying types) from every single row in the table.
How can I achieve this in SQL Server 2000?
Concatenation in SQL Server 2000:
declare #s varchar(8000);
set #s = '';
select #s = #s + field1 + field2 + field3
from #temp
where ...
order by ...;
select #s;
The XML datatype isn't supported in 2000, it was introduced in 2005.
SQL 2000 did introduce the FOR XML, but it only supported RAW, AUTO, and EXPLICIT
You've to define some text or character inside FOR XML PATH(' ') like,
FOR XML PATH('abc'). You can also use FOR XML RAW('abc') for outputting raw XML.

How to get Sql Server XML variable into TEXT column

I need to update an XML document stored in a Microsoft SQL Server database, however the vendor of the product chose to store the XML in a TEXT column.
I've been able to extract the TEXT into an XML-type variable and perform the update I need on the xml within this variable, but when I try to UPDATE the column to push the change back to the database, I run into trouble.
Looking through the documentation it appears that it's not possible to simply CAST/CONVERT an XML type variable to insert it into a TEXT column, but I would think there is some way to extract the xml "string" from the XML-type variable and UPDATE the column using this value.
Any suggestions are appreciated, but I would like to keep the solution pure SQL that it can be run directly (no C# custom function, etc.); just to keep the impact on the database minimal.
(note: isn't it a bit absurd that you can't just CAST XML as TEXT? I'm just saying...)
Casting the XML as VARCHAR(MAX) works.
declare #xml xml
declare #tblTest table (
Id int,
XMLColumn text
)
insert into #tblTest
(Id, XMLColumn)
values
(1, '<MyTest><TestNode>A</TestNode></MyTest>')
set #xml = '<MyTest><TestNode>A</TestNode><TestNode>B</TestNode></MyTest>'
update #tblTest
set XMLColumn = cast(#xml as varchar(max))
where Id = 1
select Id, XMLColumn from #tblTest

Can I do a find/replace in t-sql?

I basically have an xml column, and I need to find and replace one tag value in each record.
For anything real, I'd go with xpaths, but sometimes you just need a quick and dirty solution:
You can use CAST to turn that xml column into a regular varchar, and then do your normal replace.
UPDATE xmlTable SET xmlCol = REPLACE( CAST( xmlCol as varchar(max) ), '[search]', '[replace]')
That same technique also makes searching XML a snap when you need to just run a quick query to find something, and don't want to deal with xpaths.
SELECT * FROM xmlTable WHERE CAST( xmlCol as varchar(max) ) LIKE '%found it!%'
Edit: Just want to update this a bit, if you get a message along the lines of Conversion of one or more characters from XML to target collation impossible, then you only need to use nvarchar which supports unicode.
CAST( xmlCol as nvarchar(max) )
To find a content in an XML column, look into the exist() method, as described in MSDN here.
SELECT * FROM Table
WHERE XMLColumn.exist('/Root/MyElement') = 1
...to replace, use the modify() method, as described here.
SET XMLColumn.modify('
replace value of (/Root/MyElement/text())[1]
with "new value"
')
..all assuming SqlServer 2005 or 2008. This is based on XPath, which you'll need to know.
update my_table
set xml_column = replace(xml_column, "old value", "new value")