SQL FORMAT 函数

一、SQL FORMAT 函数 语法

SQL语言的 FORMAT 函数 改变了给定文本的格式。在 SQL 中,它可以将字符串格式转换为任何格式。

语法1:此语法使用带有 SQL 表的列名的 FORMAT 函数:

SELECT FORMAT(Column_Name, format) AS Alias_Name FROM Table_Name;  

在语法中,我们必须指定那些我们想要格式化其值的列的名称。

语法2:此语法使用带字符串的 FORMAT 函数:

SELECT FORMAT(String_Value, format);  

二、SQL 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表中插入 Workers with Grades、Salaries 和 Remarks 的记录:

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 查询使用 FORMAT 函数与上述 Worker_Grade 表的 Worker_ID 列:

SELECT Worker_ID, Format(Worker_ID, C) AS Format_c FROM Worker_Grade;

这个 SQL 语句改变了每个 worker 的 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 查询将 FORMAT 函数与上述 Worker_Grade 表的 Attendance_Remarks 列一起使用:

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 查询将 FORMAT 函数与上述 Worker_Grade 表的 Worker_Salary 列一起使用:

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

 

热门文章

优秀文章