Skip to content

Commit 9b62abd

Browse files
authored
Refresh TRY_* articles (#36039)
1 parent f936379 commit 9b62abd

File tree

3 files changed

+182
-239
lines changed

3 files changed

+182
-239
lines changed

docs/t-sql/functions/try-cast-transact-sql.md

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
22
title: "TRY_CAST (Transact-SQL)"
3-
description: Returns a value cast to the specified data type if the cast succeeds; otherwise, returns null.
3+
description: TRY_CAST returns a value cast to the specified data type if the cast succeeds; otherwise, returns null.
44
author: MikeRayMSFT
55
ms.author: mikeray
66
ms.reviewer: randolphwest
7-
ms.date: 10/15/2025
7+
ms.date: 12/08/2025
88
ms.service: sql
99
ms.subservice: t-sql
1010
ms.topic: reference
@@ -64,43 +64,31 @@ Returns a value cast to the specified data type if the cast succeeds; otherwise,
6464

6565
## Examples
6666

67-
### A. TRY_CAST returns null
67+
### A. TRY_CAST returns NULL
6868

69-
The following example demonstrates that `TRY_CAST` returns null when the cast fails.
69+
- The following example demonstrates that `TRY_CAST` returns null when the cast fails.
7070

71-
```sql
72-
SELECT
73-
CASE WHEN TRY_CAST('test' AS FLOAT) IS NULL
74-
THEN 'Cast failed'
75-
ELSE 'Cast succeeded'
76-
END AS Result;
77-
GO
78-
```
71+
```sql
72+
SELECT
73+
CASE WHEN TRY_CAST('test' AS FLOAT) IS NULL
74+
THEN 'Cast failed'
75+
ELSE 'Cast succeeded'
76+
END AS Result;
77+
GO
78+
```
7979

80-
[!INCLUDE [ssResult](../../includes/ssresult-md.md)]
81-
82-
```output
83-
Result
84-
------------
85-
Cast failed
86-
```
87-
88-
The following example demonstrates that the expression must be in the expected format.
89-
90-
```sql
91-
SET DATEFORMAT dmy;
80+
This query returns a result of `Cast failed`.
9281

93-
SELECT TRY_CAST('12/31/2022' AS DATETIME2) AS Result;
94-
GO
95-
```
82+
- The following example demonstrates that the expression must be in the expected format.
9683

97-
[!INCLUDE [ssResult](../../includes/ssresult-md.md)]
84+
```sql
85+
SET DATEFORMAT dmy;
86+
87+
SELECT TRY_CAST('12/31/2022' AS DATETIME2) AS Result;
88+
GO
89+
```
9890

99-
```output
100-
Result
101-
----------------------
102-
NULL
103-
```
91+
This query returns a result of `NULL`.
10492

10593
### B. TRY_CAST fails with an error
10694

@@ -111,7 +99,7 @@ SELECT TRY_CAST(4 AS XML) AS Result;
11199
GO
112100
```
113101

114-
The result of this statement is an error, because an integer can't be cast into an **xml** data type.
102+
The result of this statement is an error, because an integer can't be cast into the **xml** data type.
115103

116104
```output
117105
Explicit conversion from data type int to xml is not allowed.
@@ -128,16 +116,9 @@ SELECT TRY_CAST('12/31/2022' AS DATETIME2) AS Result;
128116
GO
129117
```
130118

131-
[!INCLUDE [ssResult](../../includes/ssresult-md.md)]
132-
133-
```output
134-
Result
135-
----------------------------------
136-
2022-12-31 00:00:00.0000000
137-
```
119+
This query returns a result of `2022-12-31 00:00:00.0000000`.
138120

139121
## Related content
140122

141123
- [TRY_CONVERT (Transact-SQL)](try-convert-transact-sql.md)
142124
- [CAST and CONVERT (Transact-SQL)](cast-and-convert-transact-sql.md)
143-

docs/t-sql/functions/try-convert-transact-sql.md

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
22
title: "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.
44
author: MikeRayMSFT
55
ms.author: mikeray
66
ms.reviewer: randolphwest
7-
ms.date: 05/23/2024
7+
ms.date: 12/08/2025
88
ms.service: sql
99
ms.subservice: t-sql
1010
ms.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;
115103
GO
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
121109
Explicit conversion from data type int to xml is not allowed.
@@ -131,15 +119,8 @@ SELECT TRY_CONVERT(DATETIME2, '12/31/2022') AS Result;
131119
GO
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

Comments
 (0)