SQL Query to Check If a Name Begins and Ends With a Vowel
Last Updated :
08 Oct, 2021
In this article, we will see an SQL query to check if a name begins and ends with a vowel and we will implement it with the help of an example for better understanding, first of all, we will create a database Name of the Database will GeeksforGeeks. and inside the database, we will create a table name As “Student”.
Here we use two different methods for this.
Syntax:
FOR LEFT():
LEFT ( expression, no_of_chars needed to the left)
FOR RIGHT():
RIGHT ( expression, no_of_chars needed to the right)
In this article let us see how to Check If a Name Begins and Ends With a Vowel and display them using MSSQL as a server.
Step 1: Creating a database
Creating a database GeeksforGeeks by using the following SQL query as follows.
Query:
CREATE DATABASE GeeksforGeeks;

Step 2: Using the database
Using the database GeeksforGeeks using the following SQL query as follows.
Query:
USE GeeksforGeeks;

Step 3: Creating a table
Creating a table student with 3 columns using the following SQL query as follows.
Query:
CREATE TABLE student(
stu_id VARCHAR(8),
stu_name VARCHAR(30),
stu_branch VARCHAR(30)
)

Step 4: Verifying the database
To view the description of the table using the following SQL query as follows.
Query:
EXEC sp_columns student

Step 5: Inserting data into the table
Inserting rows into student tables using the following SQL query as follows.
Query:
INSERT INTO student VALUES
('191401', 'ABHI','E.C.E'),
('191402', 'OLIVIA','E.C.E'),
('191403', 'SAMARTH','E.C.E'),
('191404', 'ANNABELLE','E.C.E'),
('191405', 'ARIA','E.C.E'),
('191406', 'RAMESH','E.C.E')

Step 6: Verifying the inserted data
Viewing the table student after inserting rows by using the following SQL query as follows.
Query:
SELECT * FROM student

Step 7: Query to Check If a Name Begins and Ends With a Vowel using string functions and IN operator
Method 1:
To check if a name begins ends with a vowel we use the string functions to pick the first and last characters and check if they were matching with vowels using in where the condition of the query. We use the LEFT() and RIGHT() functions of the string in SQL to check the first and last characters.
Query:
SELECT stu_name
FROM student
WHERE LEFT(stu_name , 1) IN ('a','e','i','o','u')
AND RIGHT(stu_name,1) IN ('a','e','i','o','u')
Output:

Method 2:
Using Regular Expressions and LIKE operator to check if first and last characters are vowels. Query to Check If a Name Begins and Ends With a Vowel using REGEX
Query:
SELECT stu_name
FROM student
WHERE stu_name LIKE '[aeiouAEIOU]%[aeiouAEIOU]'
Here % is used for multiple occurrences of any character and [] is used for anyone occurrence of a given set of characters in the brackets.
Output:
Similar Reads
Computer Science Subjects