Try_Convert for SQL Server 2008 R2

When using XML in SQL Server you can try to cast to a data type and receive null values where the cast fails.

declare @T table
(
  Col varchar(50)
)

insert into @T values
('1'),
('1.1'),
('1,1'),
('1a')

select cast('' as xml).value('sql:column("Col") cast as xs:decimal ?', 
                             'decimal(28,10)') as Col
from @T

Result:

Col
-------------
1.0000000000
1.1000000000
NULL
NULL

Leave a Comment