SQL Server 2008 XML Attribute search is case sensitive - sql

I store user preferences in an XML column which looks like this:
<tags>
<user>
<tag name="AB"/>
</user>
</tags/>
When I use the query below,
select *
from company
where CAST(tags.query('tags/user/tag[fn:contains(#name,"Ab")]') as varchar(2000) ) <> ''
it does not return any results, the attribute value is in different case then one in the xml column.
Any ideas on making the search by attribute name case insensitive?
Thanks

With SQL Server 2008 you can make use of the lower-case and upper-case functions like so:
select * from company where CAST(tags.query('tags/user/tag[fn:contains(lower-case(#name),"ab")]') as varchar(2000) ) <>''
see:
New XQuery functions introduced in SQL Server 2008: upper-case() and lower-case()
lower-case Function (XQuery) (MSDN)

Related

Fetch Tag\Element value of XML from CLOB in DB2 database

i have an xml of the following structue...
Structure of XML:
<Persons>
<PersonID>12345</Person>
<PersonName>Larissa</Person>
<PersonAge>28</Person>
<Persons>
<Persons>
<PersonID>12345</Person>
<PersonName>Larissa</Person>
<PersonAge>28</Person>
<Persons>
The xml is in the CLOB datatype column of IBM DB2 Database.I want to fire an select query to extract the value of PersonID field and get the value 12345 in return.
Is there any functions in DB2 for xml by using which i can extract the value of the PersonID???
(Assuming DB2 Linux/Unix/Window)
You can use xquery to get the results you want:
xquery
db2-fn:xmlcolumn('YOUR_TABLE.YOUR_COLUMN')/Persons/PersonID/text()
Since this query invokes xquery directly, you have to instruct DB2 to use the xquery parser (instead of the SQL parser) by using the xquery keyword.

Using regular expression within a stored procedure

What is the regular expression pattern that enables every input besides characters? So far this is what i have -
CREATE PROCEDURE Paging_Movies
#alphaChar char(1)
AS
if #alphaChar = '#'
select * from Movies where movies like '[0-9]%'
else
select * from Movies where movies like #alphaChar + '%'
If you want true regular expression pattern matching you will need to roll your own CLR UDF. This link goes over how to do that:
http://msdn.microsoft.com/en-us/magazine/cc163473.aspx
Keep in mind that you can only do this in SQL Server 2005 or higher.
If you just want non-alpha you can do this:
'([^a-z])'
Here is the documentation for SQL Server like:
http://msdn.microsoft.com/en-us/library/ms179859.aspx
SQL Server 2008 R2 has some regular expression functions built-in.
Here's a link explaining how to extract them and use them in your own database.

FOR XML AUTO and sql server 2005

SELECT * FROM emp FOR XML AUTO , ROOT ('Employees') , ELEMENTS
this query return value in xml format but total xml is showing in one column and column name is showing dynamically. if i want that i will specify the column name like data etc. so xml value will show as single row & column but header or output column name will be "DATA"
is it possible if yes the please show me the way.thanks
You need to check out the new FOR XML PATH feature in SQL Server 2005 and up - read all about it in SQL Server Books Online.
Basically, with FOR XML PATH, you can define the shape of your XML very easily, e.g.
SELECT
EmployeeID AS '#EmployeeID',
FirstName,
LastName
FROM dbo.Employees
FOR XML PATH('Employee'), ROOT('AllEmployees')
will generate XML something like:
<AllEmployees>
<Employee EmployeeID="12345">
<FirstName>John</FirstName>
<LastName>Doe</LastName>
</Employee>
</AllEmployees>
FOR XML PATH is very flexible and allows you to do a lot of things quite easily and intuitively.

xml generation using sql 2000

I have a stored procedure that returns a single recordset. is it possible to convert it into xml like below
<employee>
<firstname>x</name>
<lastname>x</name>
<dob>x</dob>
</employee>
using some simple options in sql 2000?
SELECT *
FROM MyTable
WHERE 1 = 1
FOR XML AUTO
Instead of the AUTO, you can use RAW, EXPLICIT or PATH (see here http://msdn.microsoft.com/en-us/library/ms178107(v=SQL.90).aspx), but AUTO generally does what you want.
I can only find notes on MSDN for SQL 2008 and 2005 but I'm sure I've used this on 2000 before...
Thanks Skrealin
the query below works
select * from Mytable employee for xml auto , elements

Querying by a value in XML in SQL Server 2005

Let's say I have a column in a table where the datatype is XML. I have a specific value I want query for in an xml tag that is unique(no repeating) in the XML. How do I do this?
Something like:
select * from MyTable
where XMLColumn.TagImLookingAt.Value = #QueryValue
Use:
WHERE xmlcolumn.value('(/path/to/tag)[1]', 'int') = #QueryValue
Change the data type to whatever is appropriate.
For more info, see the documentation - specifically the methods available when dealing with the XML data type...