SQL SPACE函数

SQL SPACE函数

该SQL的字符串函数在两个字符或一组字符串之间添加指定数量的空格。

SPACE字符串函数的语法

语法1: 该语法使用SQL表的列名结合SPACE函数使用:

SELECT Column_Name1 + SPACE(Number) + Column_Name2 AS Alias_Name FROM Table_Name As Alias_Name ;

在语法中,我们必须指定想要使用SPACE函数的那些列的名称。

语法2: 此语法使用SPACE函数与两个或多个字符串:

SELECT String1 + SPACE(Number) + String2 + SPACE(Number) + …… StringN As Alias_Name;

语法3: 此语法使用SPACE函数和两个或多个单个字符:

SELECT Character1 + SPACE(Number) + Character2 + SPACE(Number) + …… CharacterN As Alias_Name ;

语法4: 我们还可以使用给定的语法来指定空格:

SELECT SPACE(Number) As Alias_Name;

空格字符串函数示例

示例1: 以下SELECT查询显示结果中的15个空格:

SELECT SPACE(15) AS 15_Space;

输出:

15_Spaces
' '

示例2: 以下SELECT查询在JavaTpoint单词后添加了10个空格:

SELECT 'JavaTpoint' + SPACE(10) AS 10_Space; 

输出:

'JavaTpoint '
10

示例3: SELECT查询语句在两个单个字符之间添加了20个空格:

SELECT 'S' + SPACE(20) + 'K';

输出:

20_Spaces
'S K'

**例4: **

下面的SELECT查询在多个给定字符串之间添加了15个空格:

SELECT 'New' + SPACE(15) + 'Delhi' + SPACE(15) + 'is' + SPACE(15) + 'the' + SPACE(15) + 'Capital' + SPACE(15) + 'of' + SPACE(15) + 'India';

输出:

20_Spaces
'New' 'Delhi' 'is' 'the' 'Capital' 'of' 'India'

例子5:本例使用SPACE函数与SQL表格

在这个例子中,我们将创建一个新表格,用于执行带有字符串列的空格函数。

在SQL数据库中创建新表格的语法如下:

CREATE TABLE table_name
(
First_Column_of_SQLtable Data Type (character_size of First Column),  
Second_Column_of_SQLtable Data Type (character_size of the Second column ),  
Third_Column_of_SQLtable Data Type (character_size of the Third column),  
...  

Last_Column_of_SQLtable Data Type (character_size of the Last column)
);  

以下是创建 Student_Grade 表的CREATE语句:

CREATE TABLE Student_Grade
(
Roll_No INT PRIMARY KEY,  
First_Name VARCHAR (100),  
Last_Name VARCHAR (100), 
First_City Varchar(120),
Second_City Varchar(120),
New_City Varchar(120),
Hindi_Marks INT, 
Maths_Marks INT, 
Grade Varchar (80)
);

以下是将学生的成绩和分数记录插入到 Student_Grade 表中的 INSERT 查询:

INSERT INTO Student_Grade (Roll_No, First_Name, Last_Name, First_City, Second_City, New_City, Hindi_Marks, Maths_Marks, Grade) VALUES (10, Aman, Sharma, Lucknow Chandigarh, Ghaziabad, 88, 95, A2);

INSERT INTO Student_Grade 
(Roll_No, First_Name, Last_Name, First_City, Second_City, New_City, Hindi_Marks, Maths_Marks, Grade) 
VALUES ( 02, Vishal, Sharma, Chandigarh, Ghaziabad, Delhi, 95, 82, A1 );

INSERT INTO Student_Grade 
(Roll_No, First_Name, Last_Name, First_City, Second_City, New_City, Hindi_Marks, Maths_Marks, Grade) 
VALUES (07, Raj, Gupta, Delhi, Ghaziabad, Lucknow, 91, 95, A1);

INSERT INTO Student_Grade 
(Roll_No, First_Name, Last_Name, First_City, Second_City, New_City, Hindi_Marks, Maths_Marks, Grade)  
VALUES (04, Yash, Singhania, Ghaziabad, Delhi, Lucknow, 85, 82, A2);

INSERT INTO Student_Grade 
(Roll_No, First_Name, Last_Name, First_City, Second_City, New_City, Hindi_Marks, Maths_Marks, Grade) 
VALUES (11, Vinay, Roy, Delhi, Kanpur, Ghaziabad, 95, 97, A1);

INSERT INTO Student_Grade 
(Roll_No, First_Name, Last_Name, First_City, Second_City, New_City, Hindi_Marks, Maths_Marks, Grade) 
VALUES (16, Manoj, Gupta, Ghaziabad, Meerut, Chandigarh, 95, 90, B1);


INSERT INTO Student_Grade 
(Roll_No, First_Name, Last_Name, First_City, Second_City, New_City, Hindi_Marks, Maths_Marks, Grade) 
VALUES (19, Ram, Gupta, Lucknow, Ghaziabad, Chandigarh, 89, 95, A2);

以下SELECT语句显示了上述 Student_Grade 表中插入的记录:

SELECT * FROM Student_Grade;
Roll_No First_Name Last_Name First_City Second_City New_City Hindi_Marks Maths_Marks 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

以下SELECT查询使用SPACE函数与上面Student_Grade表中的First_Name和Last_Name列:

SELECT First_Name, Last_Name, First_Name + SPACE(10) + Last_Name AS 10_space FROM Student_Grade;

此SQL语句在每个学生的名字和姓氏之间添加了10个空格。

输出结果:

First_Name Last_Name 10_space
Aman Sharma Aman Sharma
Vishal Sharma Vishal Sharma
Raj Gupta Raj Gupta
Yash Singhania Yash Singhania
Vinay Roy Vinay Roy
Manoj Gupta Manoj Gupta
Ram Gupta Ram Gupta

以下SELECT查询使用SPACE函数,并且使用上述Student_Grade表中Roll_No大于2的学生的First_Name和Grade列:

SELECT Roll_No, First_Name + SPACE(5) + Grade FROM Student_Grade WHERE Roll_No > 2;

输出:

Roll_No First_Name + SPACE(5) + Grade
07 Raj A1
04 Yash A2
11 Vinay A1
16 Manoj B1
19 Ram A2

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程