Check if SQL Server 2005 XML field is empty

msfanboy picture msfanboy · Jun 25, 2010 · Viewed 10.2k times · Source

I just did this:

Delete FROM MyTable WHERE ScopeValue = "" 
Delete FROM G_Scope WHERE ScopeValue is ''
Delete FROM G_Scope WHERE ScopeValue = empty
Delete FROM G_Scope WHERE ScopeValue is empty

I want to delete all rows with xml field (not nullable) where ScopeValue column has empty entries means zero chars.

Anyone knows?

Answer

marc_s picture marc_s · Jun 25, 2010

Try this:

 DELETE FROM dbo.G_Scope WHERE ScopeValue IS NULL

The SQL Server column would be NULL is if contains no value.

The other possibility would be that the XML is not NULL, but contains an empty string as its value. For that, use this command:

-- The DATALENGTH of an empty XML column is 5
SELECT * FROM dbo.G_Scope WHERE DATALENGTH(ScopeValue) = 5

Does that show you the rows you're interested in?