Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ms sql xml data type convert to text

in MS Sql there are data types that are not supported by delphi 7, the xml datatype is one example.

I wish to convert the XML datatype to Text datatype, so that i could handle it in delphi.

Is there a way to convert from xml to text?

like image 252
none Avatar asked Jan 05 '11 15:01

none


People also ask

Can you convert data types in SQL?

Data types can be converted either implicitly or explicitly. Implicit conversions are not visible to the user. SQL Server automatically converts the data from one data type to another. For example, when a smallint is compared to an int, the smallint is implicitly converted to int before the comparison proceeds.

Is XML a data type in SQL?

The xml data type is a built-in data type in SQL Server and is somewhat similar to other built-in types such as int and varchar. As with other built-in types, you can use the xml data type as a column type when you create a table as a variable type, a parameter type, a function-return type, or in CAST and CONVERT.

How do I change text datatype in SQL?

Use UPDATETEXT to change only a part of a text, ntext, or image column in place. Use WRITETEXT to update and replace a whole text, ntext, or image field. This feature will be removed in a future version of Microsoft SQL Server.


2 Answers

A simple cast will suffice:

select cast(XMLCol as nvarchar(max)) as XMLCol  

Or for non-unicode:

select cast(XMLCol as varchar(max)) as XMLCol  

You can't convert explicitly to a 'text' data type.

I've added the as XMLCol to ensure that the converted data has the the same name as the column. You needn't have this, of course.

EDIT:

A few links. You are encouraged to use nvarchar(max) instead of text regardless. Microsoft have said they will be deprecating these types in future releases. nvarchar(max) ought to offer you 2GB:

http://www.petefreitag.com/item/734.cfm

http://www.teratrax.com/articles/varchar_max.html

http://msdn.microsoft.com/en-us/library/ms187752(v=SQL.90).aspx

like image 54
James Wiseman Avatar answered Sep 17 '22 18:09

James Wiseman


SELECT CAST(YourXMLColumn as nvarchar(max))     FROM YourTable 
like image 26
Joe Stefanelli Avatar answered Sep 21 '22 18:09

Joe Stefanelli