------------------------------------------------------------------ A. Using both CAST and CONVERT -- Use CAST USE AdventureWorks; GO SELECT SUBSTRING(Name, 1, 30) AS ProductName, ListPrice FROM Production.Product WHERE CAST(ListPrice AS int) LIKE '3%'; GO -- Use CONVERT. USE AdventureWorks; GO SELECT SUBSTRING(Name, 1, 30) AS ProductName, ListPrice FROM Production.Product WHERE CONVERT(int, ListPrice) LIKE '3%'; GO ------------------------------------------------------------------ B. Using CAST with arithmetic operators USE AdventureWorks; GO SELECT CAST(ROUND(SalesYTD/CommissionPCT, 0) AS int) AS 'Computed' FROM Sales.SalesPerson WHERE CommissionPCT != 0; GO output: Computed ------ 379753754 346698349 257144242 176493899 281101272 0 301872549 212623750 298948202 250784119 239246890 101664220 124511336 97688107 (14 row(s) affected) ------------------------------------------------------------------ C. Using CAST to concatenate USE AdventureWorks; GO SELECT 'The list price is ' + CAST(ListPrice AS varchar(12)) AS ListPrice FROM Production.Product WHERE ListPrice BETWEEN 350.00 AND 400.00; GO output: ListPrice ------------------ The list price is 357.06 The list price is 364.09 The list price is 364.09 The list price is 364.09 The list price is 364.09 (5 row(s) affected) ------------------------------------------------------------------ D. Using CAST to produce more readable text USE AdventureWorks; GO SELECT DISTINCT CAST(p.Name AS char(10)) AS Name, s.UnitPrice FROM Sales.SalesOrderDetail s JOIN Production.Product p on s.ProductID = p.ProductID WHERE Name LIKE 'Long-Sleeve Logo Jersey, M'; GO output: Name UnitPrice ---------- --------------------- Long-Sleev 31.2437 Long-Sleev 32.4935 Long-Sleev 49.99 (3 row(s) affected) ------------------------------------------------------------------ E. Using CAST with the LIKE clause USE AdventureWorks; GO SELECT p.FirstName, p.LastName, s.SalesYTD, s.SalesPersonID FROM Person.Contact p JOIN Sales.SalesPerson s ON p.ContactID = s.SalesPersonID WHERE CAST(CAST(s.SalesYTD AS int) AS char(20)) LIKE '2%'; GO output: FirstName LastName SalesYTD SalesPersonID ---------------- ------------------- ---------------- ------------- Carol Elliott 2811012.7151 279 Julie Estes 219088.8836 288 Janeth Esteves 2241204.0424 289 (3 row(s) affected) ------------------------------------------------------------------ F. Using CONVERT or CAST with typed XML CONVERT(XML, '') CONVERT(XML, ' ', 1) CAST('CarolElliot' AS XML) ------------------------------------------------------------------ G. Using CAST and CONVERT with datetime data SELECT GETDATE() AS UnconvertedDateTime, CAST(GETDATE() AS nvarchar(30)) AS UsingCast, CONVERT(nvarchar(30), GETDATE(), 126) AS UsingConvertTo_ISO8601 ; GO output: UnconvertedDateTime UsingCast UsingConvertTo_ISO8601 ----------------------- ------------------------------ ------------------------------ 2006-04-18 09:58:04.570 Apr 18 2006 9:58AM 2006-04-18T09:58:04.570 (1 row(s) affected) SELECT '2006-04-04T15:50:59.997' AS UnconvertedText, CAST('2006-04-04T15:50:59.997' AS datetime) AS UsingCast, CONVERT(datetime, '2006-04-04T15:50:59.997', 126) AS UsingConvertFrom_ISO8601 ; GO output: UnconvertedText UsingCast UsingConvertFrom_ISO8601 ----------------------- ----------------------- ------------------------ 2006-04-04T15:50:59.997 2006-04-04 15:50:59.997 2006-04-04 15:50:59.997 (1 row(s) affected) ------------------------------------------------------------------ ------------------------------------------------------------------ ------------------------------------------------------------------