How to Get First Character of a String in SQL?
Last Updated :
05 Dec, 2024
SQL (Structured Query Language) is essential for managing and querying relational databases. Whether you're handling customer data, employee records, or product details, SQL provides powerful tools for manipulating string data. One common task when working with strings is to extract the first character from a string. This can be useful for categorizing data, sorting records, or performing additional data analysis.
In this article, we will explain how to extract the first character of a string in SQL using two popular methods: the SUBSTRING() and SUBSTR() functions. We'll provide examples, explain their usage, and ensure we have everything needed to implement these techniques in our SQL queries.
Using SQL to Extract the First Character of a String
When working with string data in SQL, extracting specific portions of a string, such as the first character, is a common requirement. To perform this task, we can use two primary SQL functions: SUBSTRING() and SUBSTR(). Both of these functions can be used to extract a substring from a string, and we'll focus on using them to get the first character.
1. SUBSTRING()
SUBSTRING() is a function in SQL that can be used to get substrings from strings based on start positions and length. We can use this function to get a substring from a string column or a literal value, providing us with the flexibility to work with textual data.
Syntax
SUBSTRING(string, start, length)
Key Terms
- string: This parameter represents the string from which we need to extract the part of a string. This can be the column_name of an underlying table that contains the string values.
- start: This is the starting index from which the string should start extracting.
- length: This is an optional parameter that is used to represent the terminating index of the substring process. If this parameter is not mentioned, the length parameter will be assigned as the actual length of the string by default.
Example 1: Extracting the First Character Using SUBSTRING()
Let's us consider a table STUDENT_DETAILS which contains the details of the students along with their First_name, Last_name, and Course. We'll use the SUBSTRING() function to extract the first character from the First_name
column.
Table Structure and Sample Data
CREATE TABLE STUDENT_DETAILS (
First_name VARCHAR(10),
Last_name VARCHAR(10),
Course VARCHAR(10)
);
INSERT INTO STUDENT_DETAILS (First_name, Last_name, Course) VALUES
('Poojitha', 'Pullambhatla', 'IT'),
('Siri', 'Varma', 'CSE'),
('Sindhu', 'Botla', 'CSE'),
('Ravi', 'Suggala', 'IT'),
('Dhoni', 'Singh', 'MECH');
Output
STUDENT_DETAILSThe query will return the first character of each student's first name from the STUDENT_DETAILS table.
Query:
SELECT SUBSTRING(First_Name, 1, 1) AS First_name_first_character
FROM STUDENT_DETAILS;
Output
SQL Query to extract the first character in the First_name of all the studentsExplanation:
Here, the SUBSTRING() function accepts the strings in the First_name column and starting from index 1, it extracts a part of string of length 1. That is the first character of the strings in the First_name column.
2. SUBSTR() Function
The SUBSTR() function works similarly to SUBSTRING(), but it's often used in certain database systems like Oracle and MySQL. It's another useful method for extracting substrings from a string.
Syntax
SUBSTR(string, start, length)
Example 2: Extracting First Character from Last Names using SUBSTRING() in SQL
To extract first characters from the Last_name of all the students, we can use the following query with the SUBSTRING() function. This technique helps us quickly retrieve the first letter of a person's last name, which can be useful for grouping or sorting data based on initials.
Query:
SELECT SUBSTRING(Last_Name, 1, 1) AS Last_name_first_character
FROM STUDENT_DETAILS;
Output
SQL Query to extract the first character in the Last_name of all the studentExplanation:
Here, the SUBSTRING() function accepts the strings in the Last_name column and starting from index 1, it extracts a part of string of length 1. That is the first character of the strings in the Last_name column.
Conclusion
SUBSTRING() is a function in SQL that can be used to manipulate string data. It can be used to get substrings according to different positions and lengths. For example, it can be used to retrieve the first character in a string. This function can be used in MySQL database, SQL Server database, or any other database that complies with SQL. It provides a standardized and flexible solution.
Similar Reads
SQL Interview Questions Are you preparing for a SQL interview? SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM in the 1970's, SQL allows us to create, read, update, and delete data with simple yet effective commands.
15+ min read
SQL Tutorial SQL is a Structured query language used to access and manipulate data in databases. SQL stands for Structured Query Language. We can create, update, delete, and retrieve data in databases like MySQL, Oracle, PostgreSQL, etc. Overall, SQL is a query language that communicates with databases.In this S
11 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
SQL Joins (Inner, Left, Right and Full Join) SQL joins are fundamental tools for combining data from multiple tables in relational databases. Joins allow efficient data retrieval, which is essential for generating meaningful observations and solving complex business queries. Understanding SQL join types, such as INNER JOIN, LEFT JOIN, RIGHT JO
6 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
ACID Properties in DBMS In the world of DBMS, transactions are fundamental operations that allow us to modify and retrieve data. However, to ensure the integrity of a database, it is important that these transactions are executed in a way that maintains consistency, correctness, and reliability. This is where the ACID prop
8 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read