2

Is there a way to check that value of an element in the XML field has an empty size using SQLXML? Consider I have the following data in the column Conf of the table Test:

<Conf>
  <UserData>
    <data type="str" value="" />
  </UserData>
</Conf>

I can check that data exists by using the following SQL request:

SELECT Test.Conf.exist('/Conf/UserData/data') FROM Test;

But how can I check that data has an empty value? It could be something like the following, but it doesn't work:

SELECT Test.Conf.value('(/Conf/UserData/data/@value)[1]', 'nvarchar(max)')='' FROM Test;

My final solution is to use the following SQL statement:

SELECT Test.Conf.value('string-length(/Conf[1]/UserData[1]/data[1]/@value)', 'int') FROM Test;

3 Answers 3

1

Using XPath 1.0. string(@someattribute) test should return false if empty. I know nothing about SQLXML, but it will work if you can use a control sequence.

Sign up to request clarification or add additional context in comments.

1 Comment

string-length function does the job.
1

Possibly this will work.

SELECT Test.Conf.exist('data(/Conf/UserData/data[@value=''''])') FROM Test;

This checks to see if a data element with @value = '' exists.

Comments

0

The problem is not with XPath but with TSQL syntax.

XML.Exist returns a BIT datatype, indicating whether it exists. SQL Server has no native BOOLEAN datatype (a real true/false).

So, your query should have been

SELECT CASE WHEN Test.Conf.value('(/Conf/UserData/data/@value)[1]', 'nvarchar(max)')=''
       THEN 1 ELSE 0 END
FROM Test;

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.