SQL FORMAT函数
这个结构化查询语言的字符串函数可以改变给定文本的格式。在SQL中,它可以将字符串格式改变为任何格式。
FORMAT字符串函数的语法
语法1: 这个语法使用了带有SQL表的列名的FORMAT函数:
SELECT FORMAT(Column_Name, format) AS Alias_Name FROM Table_Name;
在语法中,我们必须指定那些想要格式化值的列的名称。
语法2: 这个语法使用带有字符串的FORMAT函数:
SELECT FORMAT(String_Value, format);
FORMAT字符串函数示例
示例1: 以下SELECT查询将字符串转换为百分比格式:
SELECT FORMAT( '98', 'Percent' ) AS Percent_98;
输出:
Percent_98 |
---|
98% |
示例2: 以下SELECT查询将字符串转换为货币金额:
SELECT FORMAT( '98', 'C' ) AS Currency_98;
输出:
Percent_98 |
---|
98.00 |
示例3: 以下SELECT查询将字符串转换为自定义字符串格式:
SELECT FORMAT( '9812', ' $#,###.00 ') AS Custom_#;
输出:
Custom_# |
---|
$9,812.00 |
示例 4: 以下SELECT查询将日期转换为指定的格式:
SELECT FORMAT(@date, ' M / d / yyyy ') AS 'Format_M/d/yyyy';
输出:
Format_M/d/yyyy |
---|
4 / 11 / 2022 |
示例5: 下面的SELECT查询将时间转换为指定格式:
SELECT FORMAT(@Time, ' hh : mm : ss tt') AS 'Format_hh:mm:ss';
输出:
Format_hh:mm:ss’ |
---|
2: 50 : 38 PM |
示例6:此示例使用结构化查询语言中的FORMAT函数与表一起使用。
在此示例中,我们将创建一个新的SQL表,我们希望在该表上执行Format函数。
在SQL数据库中创建新表的语法如下:
CREATE TABLE table_name
(
First_Column_of_table Data Type (character_size of First Column),
Second_Column_of_table Data Type (character_size of the Second column ),
Third_Column_of_table Data Type (character_size of the Third column),
...
Last_Column_of_table Data Type (character_size of the Last column)
);
下面的CREATE语句创建了 Worker_Grade 表:
CREATE TABLE Worker_Grade
(
Worder_ID Varchar(50) PRIMARY KEY,
First_Name VARCHAR (100),
Last_Name VARCHAR (100),
First_City Varchar(120),
Second_City Varchar(120),
New_City Varchar(120),
Attendance_Remarks Varchar(50),
Worker_Salary Varchar(50),
Grade Varchar (80)
);
下面的INSERT查询向 Worker_Grade 表中插入具有等级、工资和备注的工人记录:
INSERT INTO Worker_Grade (Worder_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Worker_Salary, Grade) VALUES (10, Aman, Sharma, Lucknow Chandigarh, Ghaziabad, 88, 9598, A2);
INSERT INTO Worker_Grade
(Worder_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Worker_Salary, Grade)
VALUES ( 02, Vishal, Sharma, Chandigarh, Ghaziabad, Delhi, 95, 8200, A1 );
INSERT INTO Worker_Grade
(Worder_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Worker_Salary, Grade)
VALUES (07, Raj, Gupta, Delhi, Ghaziabad, Lucknow, 91, 9512, A1);
INSERT INTO Worker_Grade
(Worder_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Worker_Salary, Grade)
VALUES (04, Yash, Singhania, Ghaziabad, Delhi, Lucknow, 85, 8225, A2);
INSERT INTO Worker_Grade
(Worder_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Worker_Salary, Grade)
VALUES (11, Vinay, Roy, Delhi, Kanpur, Ghaziabad, 95, 9712, A1);
INSERT INTO Worker_Grade
(Worder_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Worker_Salary, Grade)
VALUES (16, Manoj, Gupta, Ghaziabad, Meerut, Chandigarh, 95, 9025, B1);
INSERT INTO Worker_Grade
(Worder_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Worker_Salary, Grade)
VALUES (19, Ram, Gupta, Lucknow, Ghaziabad, Chandigarh, 89, 9512, A2);
以下SELECT语句显示了上述 Worker_Grade 表的插入记录:
SELECT * FROM Worker_Grade;
Worder_ID | First_Name | Last_Name | First_City | Second_City | New_City | Attendance_Remarks | Work_Remarks | Grade |
---|---|---|---|---|---|---|---|---|
10 | Aman | Sharma | Lucknow | Chandigarh | Ghaziabad | 88 | 95 | A2 |
02 | Vishal | Sharma | Chandigarh | Ghaziabad | Ghaziabad | 95 | 82 | A1 |
07 | Raj | Gupta | Delhi | Ghaziabad | Lucknow | 91 | 95 | A1 |
04 | Yash | Singhania | Ghaziabad | Delhi | Lucknow | 85 | 82 | A2 |
11 | Vinay | Roy | Delhi | Kanpur | Ghaziabad | 95 | 97 | A1 |
16 | Manoj | Gupta | Ghaziabad | Meerut | Chandigarh | 95 | 90 | B1 |
19 | Ram | Gupta | Lucknow | Ghaziabad | Chandigarh | 89 | 95 | A2 |
查询1: 以下SELECT查询使用上述Worker_Grade表的Worker_ID列的FORMAT函数:
SELECT Worker_ID, Format(Worker_ID, C) AS Format_c FROM Worker_Grade;
这个SQL语句改变了每个工人的id的格式。
输出:
Worker_ID | Format_C |
---|---|
10 | 10.00 |
02 | 2.00 |
07 | 7.00 |
04 | 4.00 |
11 | 11.00 |
16 | 16.00 |
19 | 19.00 |
查询2: 下面的SELECT查询使用了上述Worker_Grade表中的Attendance_Remarks列的FORMAT函数:
SELECT Attendance_Remarks, Format(Attendance_Remarks, Percent) AS Format_percent FROM Worker_Grade;
此SQL语句更改每个工人的出勤备注的格式。
输出:
Attendance_Remarks | Format_Percent |
---|---|
88 | 88% |
95 | 95% |
91 | 91% |
85 | 85% |
95 | 95% |
95 | 95% |
89 | 89% |
查询3: 以下SELECT查询使用了上述Worker_Grade表的Worker_Salary列的FORMAT函数:
SELECT Worker_Salary, Format(Worker_Salary, '$#,###.00' ) AS Format_Default FROM Worker_Grade;
这个 SQL 语句改变了每个工作人员的薪水格式。
输出:
Worker_Salary | Format_Default |
---|---|
9598 | 9,598.00 |
8200 | 8,200.00 |
9512 | 9,512.00 |
8225 | 8,225.00 |
9712 | 9,712.00 |
9025 | 9,025.00 |
9512 | 9,512.00 |