ฟังก์ชันการแปลงเซิร์ฟเวอร์ SQL ()
ตัวอย่าง
แปลงนิพจน์เป็น int:
SELECT CONVERT(int, 25.65);
ความหมายและการใช้งาน
ฟังก์ชัน CONVERT() จะแปลงค่า (ประเภทใดก็ได้) เป็นประเภทข้อมูลที่ระบุ
เคล็ดลับ:ดู ฟังก์ชันCAST() ด้วย
ไวยากรณ์
CONVERT(data_type(length), expression, style)
ค่าพารามิเตอร์
Value | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
data_type | 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) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
expression | Required. The value to convert to another data type | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
style | Optional. The format used to convert between data types, such as a date
or string format. Can be one of the following values:
Converting datetime to character:
|
รายละเอียดทางเทคนิค
ทำงานใน: | SQL Server (เริ่มตั้งแต่ปี 2008), ฐานข้อมูล Azure SQL, Azure SQL Data Warehouse, Parallel Data Warehouse |
---|
ตัวอย่างเพิ่มเติม
ตัวอย่าง
แปลงนิพจน์จากประเภทข้อมูลหนึ่งเป็นอีกประเภทหนึ่ง (varchar):
SELECT CONVERT(varchar, 25.65);
ตัวอย่าง
แปลงนิพจน์จากประเภทข้อมูลหนึ่งเป็นอีกประเภทหนึ่ง (วันที่และเวลา):
SELECT CONVERT(datetime, '2017-08-25');
ตัวอย่าง
แปลงนิพจน์จากประเภทข้อมูลหนึ่งเป็นอีกประเภทหนึ่ง (varchar):
SELECT CONVERT(varchar,
'2017-08-25', 101);