11---
22title : " TRY_CONVERT (Transact-SQL)"
3- description : Returns a value cast to the specified data type if the cast succeeds; otherwise, returns NULL.
3+ description : TRY_CONVERT returns a value cast to the specified data type if the cast succeeds; otherwise, returns NULL.
44author : MikeRayMSFT
55ms.author : mikeray
66ms.reviewer : randolphwest
7- ms.date : 05/23/2024
7+ ms.date : 12/08/2025
88ms.service : sql
99ms.subservice : t-sql
1010ms.topic : reference
@@ -71,40 +71,28 @@ This function is capable of being remoted to servers that have [!INCLUDE [ssSQL1
7171
7272### A. TRY_CONVERT returns NULL
7373
74- The following example demonstrates that ` TRY_CONVERT ` returns ` NULL ` when the cast fails.
74+ - The following example demonstrates that ` TRY_CONVERT ` returns ` NULL ` when the cast fails.
7575
76- ``` sql
77- SELECT
78- CASE WHEN TRY_CONVERT(FLOAT, ' test' ) IS NULL
79- THEN ' Cast failed'
80- ELSE ' Cast succeeded'
81- END AS Result;
82- GO
83- ```
76+ ``` sql
77+ SELECT
78+ CASE WHEN TRY_CONVERT(FLOAT, ' test' ) IS NULL
79+ THEN ' Cast failed'
80+ ELSE ' Cast succeeded'
81+ END AS Result;
82+ GO
83+ ```
8484
85- [ !INCLUDE [ ssResult ] ( ../../includes/ssresult-md.md )]
85+ This query returns a result of ` Cast failed ` .
8686
87- ``` output
88- Result
89- ------------
90- Cast failed
91- ```
87+ - The following example demonstrates that the expression must be in the expected format.
9288
93- The following example demonstrates that the expression must be in the expected format.
89+ ``` sql
90+ SET DATEFORMAT dmy;
91+ SELECT TRY_CONVERT(DATETIME2, ' 12/31/2022' ) AS Result;
92+ GO
93+ ```
9494
95- ``` sql
96- SET DATEFORMAT dmy;
97- SELECT TRY_CONVERT(DATETIME2, ' 12/31/2022' ) AS Result;
98- GO
99- ```
100-
101- [ !INCLUDE [ ssResult] ( ../../includes/ssresult-md.md )]
102-
103- ``` output
104- Result
105- ----------------------
106- NULL
107- ```
95+ This query returns a result of ` NULL ` .
10896
10997### B. TRY_CONVERT fails with an error
11098
@@ -115,7 +103,7 @@ SELECT TRY_CONVERT(XML, 4) AS Result;
115103GO
116104```
117105
118- The result of this statement is an error, because an integer can't be cast into an ** xml** data type.
106+ The result of this statement is an error, because an integer can't be cast into the ** xml** data type.
119107
120108``` output
121109Explicit conversion from data type int to xml is not allowed.
@@ -131,15 +119,8 @@ SELECT TRY_CONVERT(DATETIME2, '12/31/2022') AS Result;
131119GO
132120```
133121
134- [ !INCLUDE [ ssResult] ( ../../includes/ssresult-md.md )]
135-
136- ``` output
137- Result
138- ----------------------------------
139- 2022-12-31 00:00:00.0000000
140- ```
122+ This query returns a result of ` 2022-12-31 00:00:00.0000000 ` .
141123
142124## Related content
143125
144126- [ CAST and CONVERT (Transact-SQL)] ( cast-and-convert-transact-sql.md )
145-
0 commit comments