CAST 和 CONVERT
將某種數據類型的表達式顯式轉換為另一種數據類型。CAST 和 CONVERT 提供相似的功能。
語法
使用 CAST:
CAST ( expression AS data_type )
使用 CONVERT:
CONVERT (data_type[(length)], expression [, style])
1。2222222222轉換為16位的84746B8E,取後面6位746B8E,然後再轉換為
7629710
select 22222222-convert(bigint,2222222222/convert(int,0x1000000)*
convert(int,0x1000000)
2。SELECT SUBSTRING(title, 1, 30) AS Title, ytd_sales
FROM titles
WHERE CAST(ytd_sales AS char(20)) LIKE ''''3%''''
3.顯示時加百分號
select cast(field1/fIEld2 as varchar(20))+''''%''''
4.SELECT CAST(ROUND(ytd_sales/price, 0) AS int) AS ''''CopIEs''''
FROM titles
5.select convert(type,case .......when....end)
6>密碼有大小寫的比較
select *
from tablename
where cast(ma as varbinary)=cast(:para as varbinary)
7.日期轉換
select convert(char(10),getdate(),101) //102,121
101 04/18/2003
102 2003.04.18
121 2003-04-18