Here is an example of my way of getting CDATA:
DECLARE @GlobalDateFormat varchar(32)
SET @GlobalDateFormat = 'MM/dd/yyyy hh:mm tt'
DECLARE @xml XML
SET @xml =
(
SELECT
SegmentId = ISNULL(SegmentId,0)
, DocumentId = ISNULL(DocumentId,0)
, Title = CAST(Core.dbo.fCharFormat('xmlCDATA',Title,DEFAULT,'') AS xml)
, DocumentShortName = CAST(Core.dbo.fCharFormat('xmlCDATA',DocumentShortName,DEFAULT,'') AS xml)
, [FileName] = CAST(Core.dbo.fCharFormat('xmlCDATA',[FileName],DEFAULT,'') AS xml)
, [Path] = CAST(Core.dbo.fCharFormat('xmlCDATA',[Path],DEFAULT,'') AS xml)
, CreateDate = ISNULL(Core.dbo.fDateFormat(@GlobalDateFormat,CreateDate),Core.dbo.fDateFormat(@GlobalDateFormat,GETDATE()))
, ModificationDate = ISNULL(Core.dbo.fDateFormat(@GlobalDateFormat,ModificationDate),Core.dbo.fDateFormat(@GlobalDateFormat,GETDATE()))
, TemplateId = ISNULL(CAST(TemplateId AS varchar(16)),'')
, IsRoot = ISNULL(IsRoot,0)
, IsActive = ISNULL(IsActive,0)
, SortOrdinal = ISNULL(CAST(SortOrdinal AS varchar(16)),'')
, ClientId = ISNULL(ClientId,'')
, Tag = CAST(Core.dbo.fCharFormat('xmlCDATA',Tag,DEFAULT,'') AS xml)
FROM
Document
WHERE
DocumentId = 9073
FOR XML AUTO, ELEMENTS
)
SELECT @xml
Here is the relevant detail from my weasel-like, CDATA-handling function:
IF @cmdName = 'xmlCDATA'
BEGIN
IF @chars IS NULL
BEGIN
SET @charsOut = @charsDefault
END
ELSE
BEGIN
SET @chars = REPLACE(@chars,'<![CDATA[','')
SET @chars = REPLACE(@chars,']]>','')
SET @charsOut = '<![CDATA[' + @chars + ']]>'
END
END