首页 > 代码库 > CAST CONVERT转换日期和时间数据类型

CAST CONVERT转换日期和时间数据类型

  1 USE AdventureWorks2008R2;  
  2 GO  
  3 --CAST
  4 
  5 SELECT CAST "CAST",
  6 CAST(1990-11-1 AS VARCHAR(10)) 转为字符串,
  7 CAST(1990-11-1 AS DATETIME) 转为日期
  8 
  9 --CONVERT
 10 SELECT CONVERT "CONVERT",
 11 CONVERT(VARCHAR(10),2999) 转为字符串,
 12 CONVERT(INT,2999) 转为整数
 13 
 14 --同时使用 CAST 和 CONVERT
 15 
 16 -- Use CAST  
 17 SELECT SUBSTRING(Name, 1, 30) AS ProductName, ListPrice  
 18 FROM Production.Product  
 19 WHERE CAST(ListPrice AS int) LIKE 3%;  
 20 GO  
 21   
 22 -- Use CONVERT.  
 23 SELECT SUBSTRING(Name, 1, 30) AS ProductName, ListPrice  
 24 FROM Production.Product  
 25 WHERE CONVERT(int, ListPrice) LIKE 3%;  
 26 GO  
 27 
 28 --B. 将 CAST 与算术运算符结合使用
 29 
 30 SELECT CAST(ROUND(SalesYTD/CommissionPCT, 0) AS int) AS Computed  
 31 FROM Sales.SalesPerson   
 32 WHERE CommissionPCT != 0;  
 33 GO  
 34 /*
 35 C. 使用 CAST 进行连接
 36 以下示例使用 CAST 连接非字符型非二进制表达式。
 37 */
 38 SELECT The list price is  + CAST(ListPrice AS varchar(12)) AS ListPrice  
 39 FROM Production.Product  
 40 WHERE ListPrice BETWEEN 350.00 AND 400.00;  
 41 GO 
 42 
 43 /*
 44 D. 使用 CAST 生成可读性更高的文本
 45 以下示例使用选择列表中的 CAST 将 Name 列转换为 char(10) 列。
 46 */
 47 
 48 SELECT DISTINCT CAST(p.Name AS char(10)) AS Name, s.UnitPrice  
 49 FROM Sales.SalesOrderDetail AS s   
 50 JOIN Production.Product AS p   
 51     ON s.ProductID = p.ProductID  
 52 WHERE Name LIKE Long-Sleeve Logo Jersey, M;  
 53 GO 
 54 
 55 SELECT DISTINCT Name, s.UnitPrice  
 56 FROM Sales.SalesOrderDetail AS s   
 57 JOIN Production.Product AS p   
 58     ON s.ProductID = p.ProductID  
 59 WHERE Name LIKE Long-Sleeve Logo Jersey, M;  
 60 GO 
 61 
 62 /*
 63 E. 将 CAST 与 LIKE 子句一起作用
 64 以下示例将 money 列 SalesYTD 转换为 int 列,然后再转换为 char(20) 列,以便在 LIKE 子句中使用该列。
 65 */
 66 SELECT p.FirstName, p.LastName, s.SalesYTD, s.BusinessEntityID  
 67 FROM Person.Person AS p   
 68 JOIN Sales.SalesPerson AS s   
 69     ON p.BusinessEntityID = s.BusinessEntityID  
 70 WHERE CAST(CAST(s.SalesYTD AS int) AS char(20)) LIKE 2%;  
 71 GO  
 72 
 73 /*
 74 G. 对 datetime 数据使用 CAST 和 CONVERT
 75 以下示例显示当前日期和时间,使用 CAST 将当前日期和时间更改为字符数据类型,
 76 然后使用 CONVERT 以 ISO 8901 格式显示日期和时间。
 77 */
 78 SELECT   
 79    GETDATE() AS UnconvertedDateTime,  
 80    CAST(GETDATE() AS nvarchar(30)) AS UsingCast,  
 81    CONVERT(nvarchar(30), GETDATE(), 126) AS UsingConvertTo_ISO8601  ;  
 82 GO  
 83 
 84 /*
 85 以下示例与上述示例几乎完全相反。 该示例将日期和时间显示为字符数据,使用 CAST 将字符数据更改为 datetime 数据类型,然后使用 CONVERT 将字符数据更改为 datetime 数据类型。
 86 */
 87 
 88 SELECT   
 89    2006-04-25T15:50:59.997 AS UnconvertedText,  
 90    CAST(2006-04-25T15:50:59.997 AS datetime) AS UsingCast,  
 91    CONVERT(datetime, 2006-04-25T15:50:59.997, 126) AS UsingConvertFrom_ISO8601 ;  
 92 GO 
 93 
 94 /*
 95 H. 使用 CONVERT 处理二进制和字符数据
 96 以下示例使用不同样式显示转换二进制和字符数据的结果。
 97 */
 98 
 99 --Convert the binary value 0x4E616d65 to a character value.  
100 SELECT CONVERT(char(8), 0x4E616d65, 0) AS [Style 0, binary to character];  
101 
102 --The following example shows that Style 2 does not truncate the  
103 --result because the characters 0x are not included in  the result.  
104 SELECT CONVERT(char(8), 0x4E616d65, 2) AS [Style 2, binary to character];  
105 
106 --Convert the character value ‘Name‘ to a binary value.  
107 SELECT CONVERT(binary(8), JackChen, 0) AS [Style 0, character to binary];
108 
109 SELECT CONVERT(binary(4), 0x4E616D65, 1) AS [Style 1, character to binary];  
110 
111 /*
112 I. 转换日期和时间数据类型
113 以下示例演示了日期、时间及 datetime 数据类型的转换。
114 */
115 
116 DECLARE @d1 date, @t1 time, @dt1 datetime;  
117 SET @d1 = GETDATE();   
118 SET @t1 = GETDATE();  
119 SET @dt1 = GETDATE();  
120 select @d1 date格式,@t1 time格式,@dt1 datetim格式
121 -- When converting date to datetime the minutes portion becomes zero.  
122 SELECT @d1 AS [date], CAST (@d1 AS datetime) AS [date as datetime];  
123 -- When converting time to datetime the date portion becomes zero   
124 -- which converts to January 1, 1900.  
125 SELECT @t1 AS [time], CAST (@t1 AS datetime) AS [time as datetime];  
126 -- When converting datetime to date or time non-applicable portion is dropped.  
127 SELECT @dt1 AS [datetime], CAST (@dt1 AS date) AS [datetime as date], CAST (@dt1 AS time) AS [datetime as time]; 

 

CAST CONVERT转换日期和时间数据类型