
- SQL - Home
- SQL - Roadmap
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL - Comments
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Databases
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Cheatsheet
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
SQL - STRING_ESCAPE() Function
The SQL STRING_ESCAPE() function is used to convert the escape's special characters into the string.
It accepts two parameters text and type. It returns a new string(text) with an escaped characters. Currently, the STRING_ESCAPE() function only supports escaping JOSNâs special characters.
An Escape character is a character that invokes an alternative interpretation of the following characters in a character sequence. For example, the backslash(\) is the escape character. You can use the escape character in SQL to search for the special character and to escape the special character, you can use the backslash(\) before it.
Following is the syntax of the SQL STRING_ESCAPE() function −
STRING_ESCAPE( text , type )
Parameters
text − It Is a nvarchar expression expression representing the object that should be escaped.
type − It is an escaping rule that will be applied.
Return value
It returns a text with escaped characters.
Example
In the following example,we are using the SQL STRING_ESCAPE() function to convert the escape special character '\HELLO\WORLD' into the string.
SELECT STRING_ESCAPE('\HELLO\WORLD', 'JSON') as New_string;
Output
On executing the above program, it will produce the following output −
+----------------+ | New_string | +----------------+ | \\HELLO\\WORLD | +----------------+
Example
Following is another example of the SQL STRING_ESCAPE() function, here, we are trying to convert or escapes the special escape character from the text 'https://www.tutorialspoint.com/users'into the text. This function will returns text with escape characters.
SELECT STRING_ESCAPE('https://www.tutorialspoint.com/users', 'JSON') as New_string;
Output
+-----------------------------------------+ | New_string | +-----------------------------------------+ | https:\/\/fanyv88.com:443\/https\/www.tutorialspoint.com\/users | +-----------------------------------------+
Example
If we pass the type argument value as empty to the STRING_ESCAPE() function, this function throws an invalid value specified for argument 2 error.
SELECT STRING_ESCAPE('\\TutorialsPount\easylearning', '') as New_string;
Output
Following is the output of the above statement −
An invalid value was specified for argument 2.
Example
If we pass the type argument as NULL to this function, the STRING_ESCAPE() function throws an invalid datatype error.
SELECT STRING_ESCAPE('\\TutorialsPount\easylearning', NULL) as New_string;
Output
After executing the above program, it produces the following output −
Argument data type NULL is invalid for argument 2 of string_escape function.
Example
You can pass the table column as an argument to the STRING_ESCAPE() function to convert the column escape character into a string with an escape character. Assume, we have created a table with the name Customers using the CREATE statement as follows −
CREATE TABLE CUSTOMERS( ID INT NOT NULL, FIRST_NAME VARCHAR (20), LAST_NAME VARCHAR(20),s AGE INT NOT NULL, ADDRESS CHAR (25) , SALARY DECIMAL (18, 2));
Letâs insert some records into the Customers table with escape characters using the INSERT statement as follows −
INSERT INTO CUSTOMERS VALUES (1, '/Rohan\','Verma', 33, 'Hyderbad', 2100.00 ); INSERT INTO CUSTOMERS VALUES (2, '\\Kamlesh','Kumar', 30, 'Lucknow', 2500.00 ); INSERT INTO CUSTOMERS VALUES (3, 'Seeta\\','Sharma', 23, 'Delhi', 3150.00 );
Following is the SQL query to convert the escape character of the column FIRST_NAME into the string with an escape character in the Customer table −
SELECT ID, FIRST_NAME, STRING_ESCAPE(FIRST_NAME, 'JSON') AS NEW_LIST FROM CUSTOMERS;
Output
Following is the output of the above query −
+--------------------+-----------------+ | ID | FIRST_NAME | NEW_LIST | +--------------------+-----------------+ | 1 | /Rohan\ | \/Rohan\\ | | 2 | \\Kamlesh | \\\\Kamlesh | | 3 | Seeta\\ | Seeta\\\\ | +--------------------+-----------------+