ฟังก์ชันCAST() ของ เซิร์ฟเวอร์ SQL
ตัวอย่าง
แปลงค่าเป็นประเภทข้อมูล int:
SELECT CAST(25.65 AS int);
ความหมายและการใช้งาน
ฟังก์ชัน CAST() แปลงค่า (ประเภทใดก็ได้) เป็นประเภทข้อมูลที่ระบุ
เคล็ดลับ:ดูฟังก์ชัน CONVERT() ด้วย
ไวยากรณ์
CAST(expression
AS datatype(length))
ค่าพารามิเตอร์
Value | Description |
---|---|
expression | Required. The value to convert |
datatype | Required. The datatype to convert expression to. Can be one of the following: bigint, int, smallint, tinyint, bit, decimal, numeric, money, smallmoney, float, real, datetime, smalldatetime, char, varchar, text, nchar, nvarchar, ntext, binary, varbinary, or image |
(length) | Optional. The length of the resulting data type (for char, varchar, nchar, nvarchar, binary and varbinary) |
รายละเอียดทางเทคนิค
ทำงานใน: | SQL Server (เริ่มตั้งแต่ปี 2008), ฐานข้อมูล Azure SQL, Azure SQL Data Warehouse, Parallel Data Warehouse |
---|
ตัวอย่างเพิ่มเติม
ตัวอย่าง
แปลงค่าเป็นประเภทข้อมูล varchar:
SELECT CAST(25.65 AS varchar);
ตัวอย่าง
แปลงค่าเป็นประเภทข้อมูลวันที่และเวลา:
SELECT CAST('2017-08-25' AS datetime);