The Wayback Machine - https://web.archive.org/web/20240930233656/https://www.geeksforgeeks.org/sql-string-functions/
Open In App

SQL | String functions

Last Updated : 24 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

String functions are used to perform an operation on input string and return an output string. Following are the string functions defined in SQL:

  1. ASCII(): This function is used to find the ASCII value of a character.
    Syntax: SELECT ascii('t');
    Output: 116
  2. CHAR_LENGTH(): Doesn’t work for SQL Server. Use LEN() for SQL Server. This function is used to find the length of a word.
    Syntax: SELECT char_length('Hello!');
    Output: 6
  3. CHARACTER_LENGTH(): Doesn’t work for SQL Server. Use LEN() for SQL Server. This function is used to find the length of a line.
    Syntax: SELECT CHARACTER_LENGTH('geeks for geeks');
    Output: 15
  4. CONCAT(): This function is used to add two words or strings.
    Syntax: SELECT 'Geeks' || ' ' || 'forGeeks' FROM dual;
    Output: ‘Geeks forGeeks’
  5. CONCAT_WS(): This function is used to add two words or strings with a symbol as concatenating symbol.
    Syntax: SELECT CONCAT_WS('_', 'geeks', 'for', 'geeks');
    Output: geeks_for_geeks
  6. FIND_IN_SET(): This function is used to find a symbol from a set of symbols.
    Syntax: SELECT FIND_IN_SET('b', 'a, b, c, d, e, f');
    Output: 2
  7. FORMAT(): This function is used to display a number in the given format.
    Syntax: Format("0.981", "Percent");
    Output: ‘98.10%’
  8. INSERT(): This function is used to insert the data into a database.
    Syntax: INSERT INTO database (geek_id, geek_name) VALUES (5000, 'abc');
    Output: successfully updated
  9. INSTR(): This function is used to find the occurrence of an alphabet.
    Syntax: INSTR('geeks for geeks', 'e');
    Output: 2 (the first occurrence of ‘e’)
    Syntax: INSTR('geeks for geeks', 'e', 1, 2 );
    Output: 3 (the second occurrence of ‘e’)
  10. LCASE(): This function is used to convert the given string into lower case.
    Syntax: LCASE ("GeeksFor Geeks To Learn");
    Output: geeksforgeeks to learn
  11. LEFT(): This function is used to SELECT a sub string from the left of given size or characters.
    Syntax: SELECT LEFT('geeksforgeeks.org', 5);
    Output: geeks
  12. LENGTH(): This function is used to find the length of a word.
    Syntax: LENGTH('GeeksForGeeks');
    Output: 13
  13. LOCATE(): This function is used to find the nth position of the given word in a string.
    Syntax: SELECT LOCATE('for', 'geeksforgeeks', 1);
    Output: 6
  14. LOWER(): This function is used to convert the upper case string into lower case.
    Syntax: SELECT LOWER('GEEKSFORGEEKS.ORG');
    Output: geeksforgeeks.org
  15. LPAD(): This function is used to make the given string of the given size by adding the given symbol.
    Syntax: LPAD('geeks', 8, '0');
    Output:
    000geeks
  16. LTRIM(): This function is used to cut the given sub string from the original string.
    Syntax: LTRIM('123123geeks', '123');
    Output: geeks
  17. MID(): This function is to find a word from the given position and of the given size.
    Syntax: Mid ("geeksforgeeks", 6, 2);
    Output: for
  18. POSITION(): This function is used to find position of the first occurrence of the given alphabet.
    Syntax: SELECT POSITION('e' IN 'geeksforgeeks');
    Output: 2
  19. REPEAT(): This function is used to write the given string again and again till the number of times mentioned.
    Syntax: SELECT REPEAT('geeks', 2);
    Output: geeksgeeks
  20. REPLACE(): This function is used to cut the given string by removing the given sub string.
    Syntax: REPLACE('123geeks123', '123');
    Output: geeks
  21. REVERSE(): This function is used to reverse a string.
    Syntax: SELECT REVERSE('geeksforgeeks.org');
    Output: ‘gro.skeegrofskeeg’
  22. RIGHT(): This function is used to SELECT a sub string from the right end of the given size.
    Syntax: SELECT RIGHT('geeksforgeeks.org', 4);
    Output: ‘.org’
  23. RPAD(): This function is used to make the given string as long as the given size by adding the given symbol on the right.
    Syntax: RPAD('geeks', 8, '0');
    Output: ‘geeks000’
  24. RTRIM(): This function is used to cut the given sub string from the original string.
    Syntax: RTRIM('geeksxyxzyyy', 'xyz');
    Output: ‘geeks’
  25. SPACE(): This function is used to write the given number of spaces.
    Syntax: SELECT SPACE(7);
    Output: ‘ ‘
  26. STRCMP(): This function is used to compare 2 strings.
    • If string1 and string2 are the same, the STRCMP function will return 0.
    • If string1 is smaller than string2, the STRCMP function will return -1.
    • If string1 is larger than string2, the STRCMP function will return 1.
    Syntax: SELECT STRCMP('google.com', 'geeksforgeeks.com');
    Output: -1
  27. SUBSTR(): This function is used to find a sub string from the a string from the given position.
    Syntax:SUBSTR('geeksforgeeks', 1, 5);
    Output: ‘geeks’
  28. SUBSTRING(): This function is used to find an alphabet from the mentioned size and the given string.
    Syntax: SELECT SUBSTRING('GeeksForGeeks.org', 9, 1);
    Output: ‘G’
  29. SUBSTRING_INDEX(): This function is used to find a sub string before the given symbol.
    Syntax: SELECT SUBSTRING_INDEX('www.geeksforgeeks.org', '.', 1);
    Output: ‘www’
  30. TRIM(): This function is used to cut the given symbol from the string.
    Syntax: TRIM(LEADING '0' FROM '000123');
    Output: 123
  31. UCASE(): This function is used to make the string in upper case.
    Syntax: UCASE ("GeeksForGeeks");
    Output:
    GEEKSFORGEEKS

Previous Article
Next Article

Similar Reads

Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
Structured Query Language (SQL): Structured Query Language (SQL) has a specific design motive for defining, accessing and changement of data. It is considered as non-procedural, In that case the important elements and its results are first specified without taking care of the how they are computed. It is implemented over the database which is drive
2 min read
Configure SQL Jobs in SQL Server using T-SQL
In this article, we will learn how to configure SQL jobs in SQL Server using T-SQL. Also, we will discuss the parameters of SQL jobs in SQL Server using T-SQL in detail. Let's discuss it one by one. Introduction :SQL Server Agent is a component used for database task automation. For Example, If we need to perform index maintenance on Production ser
7 min read
Functions in MySQL | Numeric, String and Date Time Functions in MySQL
In MySQL, functions play a crucial role in performing various operations on data, such as calculations, string manipulations, and date handling. These built-in functions simplify complex queries and data transformations, making it easier to manage and analyze data within a database. In this article, we will look at these different categories of MyS
4 min read
SQL | Numeric Functions
Numeric Functions are used to perform operations on numbers and return numbers. Following are the numeric functions defined in SQL: ABS(): It returns the absolute value of a number. Syntax: SELECT ABS(-243.5); Output: 243.5 SQL> SELECT ABS(-10); +--------------------------------------+ | ABS(10) +--------------------------------------+ | 10 +---
3 min read
SQL | Advanced Functions
SQL (Structured Query Language) offers a wide range of advanced functions that allow you to perform complex calculations, transformations, and aggregations on your data. Aggregate Functions In database management an aggregate function is a function where the values of multiple rows are grouped together as input on certain criteria to form a single
2 min read
Date manipulating functions in SQL
To manipulate and fetch date values from the table, oracle has inbuilt date functions. Here, we are going to cover Date manipulating functions in SQL. ADD_MONTHS : It will add the total number of months specified in n to the date parameter and Returns date. Syntax : ADD_MONTHS(d, n) Example - Select ADD_MONTHS(SYSDATE, 4) "Add months" from dual; Ou
2 min read
How to Use HAVING With Aggregate Functions in SQL?
SQL provides many built-in functions to perform tasks. There are 4 types of functions Date functions, Char functions, Numeric functions, and Aggregate functions. Here we will be looking at aggregate functions and how to use them with the HAVING keyword. Aggregate Functions are database built-in functions that act on multiple rows of the table and p
4 min read
How to Use SELECT With Aggregate Functions in SQL?
In SQL, mainly five aggregate functions are provided. These help us to process the table data in the desired way. All these are illustrated below. For this article, we will be using the Microsoft SQL Server as our database. Step 1: Create a Database. For this use the below command to create a database named GeeksForGeeks. Query: CREATE DATABASE Gee
2 min read
SQL - Statistical Functions
SQL statistical functions are essential tools for extracting meaningful insights from databases. These functions, such as AVG(), SUM(), COUNT(), MIN(), MAX(), STDDEV(), and VAR(), enable users to perform statistical calculations on numeric data. Whether determining averages, sums, counts, or measures of variability, these functions empower efficien
4 min read
SQL | NULL functions
In the database, null values serve as placeholders for data that is either missing or not available. a null value is a flexible data type that can be placed in the column of any data type, including string, int, blob, and CLOB datatypes. It is not a component of any specific data type. Null values are helpful when cleaning the data prior to explora
5 min read
SQL | Character Functions with Examples
Character functions accept character inputs and can return either characters or number values as output. SQL provides a number of different character datatypes which includes - CHAR, VARCHAR, VARCHAR2, LONG, RAW, and LONG RAW. The various datatypes are categorized into three different datatypes : VARCHAR2 - A variable-length character datatype whos
6 min read
How to Find the Length of a Series Using Window Functions in SQL
In data analysis, understanding the sequential patterns within datasets is crucial for informed decision-making. SQL's window functions offer a powerful toolset for such tasks, allowing efficient calculations across related rows. This article delves into utilizing window functions to determine the length of series within datasets. Beginning with an
5 min read
Date and Time Functions in PL/SQL
Date and Time Functions in PL/SQL are useful for date and time operations like extracting date components, performing date arithmetic, and converting between date formats. Date and Time Functions in PL/SQLThere are many useful Date and Time functions in PL/SQL. A table of such functions along with their description is given below: S.noFunctionDescr
4 min read
SQL MIN() and MAX() Functions
The SQL MIN() and MAX() functions are the aggregate functions in SQL, that operate on group of data and return one output. The SQL Min() function returns the minimum value of the selected columns, and the SQL MAX() function is used to return the maximum value of the selected columns. In this article, we will understand the concept of MIN() and MAX(
3 min read
Deterministic and Nondeterministic Functions in SQL Server
Deterministic and nondeterministic functions are two fundamental concepts in SQL Server that determine how a function behaves when called with a specific set of input values. Deterministic functions always return the same result every time they are called with a fixed set of input values and given the same state of the database. On the other hand,
2 min read
PL/SQL Functions
PL/SQL functions are reusable blocks of code that can be used to perform specific tasks. They are similar to procedures but must always return a value. A function in PL/SQL contains:Function Header: The function header includes the function name and an optional parameter list. It is the first part of the function and specifies the name and paramete
4 min read
SQL Server Mathematical Functions (SQRT, PI, SQUARE, ROUND, CEILING & FLOOR)
SQL Server mathematical functions are used to perform mathematical calculations. These functions are designed to simplify complex calculations and make it easier to analyze and manipulate data. Here, we will cover the syntax and examples of various mathematical functions like SQRT(), PI(), SQUARE(), and many more Mathematical Functions in SQL Serve
1 min read
SQL Aggregate functions
SQL Aggregate functions are functions where the values of multiple rows are grouped as input on certain criteria to form a single value result of more significant meaning. It is used to summarize data, by combining multiple values to form a single result. SQL Aggregate functions are mostly used with the GROUP BY clause of the SELECT statement. Vari
2 min read
SQL General Functions
SQL general functions are built-in tools provided by SQL databases to perform a variety of operations on data. These functions are crucial for manipulating, analyzing, and transforming data efficiently. In this article, We will learn about the what are the SQL General Functions along with the examples and so on. SQL General FunctionsSQL general fun
5 min read
SQL | Date Functions (Set-1)
SQL Date Functions are essential for managing and manipulating date and time values in SQL databases. They provide tools to perform operations such as calculating date differences, retrieving current dates and times and formatting dates. In this article, We will learn about the various SQL Date Functions with the help of examples and so on. SQL Dat
5 min read
SQL | Date Functions (Set-2)
SQL Date Functions are powerful tools that allow users to manipulate, extract, and format date and time values within SQL databases. In this article, We will learn about What are SQL Date Functions along with various examples and so on. What are SQL Date Functions?SQL Date Functions are built-in functions that help manipulate, extract, and format d
4 min read
Window functions in SQL
Window functions apply to aggregate and ranking functions over a particular window (set of rows). OVER clause is used with window functions to define that window. OVER clause does two things : Partitions rows to form a set of rows. (PARTITION BY clause is used) Orders rows within those partitions into a particular order. (ORDER BY clause is used) N
4 min read
Categories of SQL Functions
SQL functions are powerful tools that help streamline queries, perform operations, and manipulate data efficiently. They are essential for handling various tasks within SQL queries and database management. Functions in SQL can be broadly categorized into predefined (built-in) functions and user-defined functions. In this article, We will learn abou
4 min read
SQL Date and Time Functions
In SQL, dates are complicated for newbies, since while working with a database, the format of the data in the table must be matched with the input data to insert. In various scenarios instead of date, datetime (time is also involved with date) is used. For storing a date or a date and time value in a database,MySQL offers the following data types:
3 min read
SQL SERVER – Input and Output Parameter For Dynamic SQL
An Input Parameter can influence the subset of rows it returns from a select statement within it. A calling script can get the value of an output parameter. An aggregate function or any computational expression within the stored process can be used to determine the value of the output parameter. A parameter whose value is given into a stored proced
3 min read
Difference between T-SQL and PL-SQL
1. Transact SQL (T-SQL) : T-SQL is an abbreviation for Transact Structure Query Language. It is a product by Microsoft and is an extension of SQL Language which is used to interact with relational databases. It is considered to perform best with Microsoft SQL servers. T-SQL statements are used to perform the transactions to the databases. T-SQL has
3 min read
SQL Server | Convert tables in T-SQL into XML
In this, we will focus on how tables will be converted in T-SQL into XML in SQL server. And you will be able to understand how you can convert it with the help of command. Let's discuss it one by one. Overview :XML (Extensible Markup Language) is a markup language similar to HTML which was designed to share information between different platforms.
2 min read
SQL - SELECT from Multiple Tables with MS SQL Server
In SQL we can retrieve data from multiple tables also by using SELECT with multiple tables which actually results in CROSS JOIN of all the tables. The resulting table occurring from CROSS JOIN of two contains all the row combinations of the 2nd table which is a Cartesian product of tables. If we consider table1 contains m rows and table2 contains n
3 min read
How to Execute SQL Server Stored Procedure in SQL Developer?
A stored procedure is a set of (T-SQL ) statements needed in times when we are having the repetitive usage of the same query. When there is a need to use a large query multiple times we can create a stored procedure once and execute the same wherever needed instead of writing the whole query again. In this article let us see how to execute SQL Serv
2 min read
SQL Query to Check if Date is Greater Than Today in SQL
In this article, we will see the SQL query to check if DATE is greater than today's date by comparing date with today's date using the GETDATE() function. This function in SQL Server is used to return the present date and time of the database system in a ‘YYYY-MM-DD hh:mm: ss. mmm’ pattern. Features: This function is used to find the present date a
2 min read
Article Tags :