Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

sql

How to use the QUOTE_LITERAL() function in SQL

Maria Elijah

Overview

The QUOTE_LITERAL() function returns the given string as a string enclosed in quotation marks, which we can then use as a literal string in an SQL statement.

Syntax

QUOTE_LITERAL(string)

Parameter

  • string: This is the input string and can be of char or varchar type.

Code

The following code shows how to use the QUOTE_LITERAL() function in SQL:

CREATE TABLE Match (
    match_id int,
    team_name varchar(50),
    away_goal int,
    home_goal int ,
    year varchar(15)
);

-- Insert data
INSERT INTO Match
VALUES (101,'Chelsea',4, 1,'2022');
INSERT INTO Match
VALUES (104,'Manchester City',2, 5,'2022');
INSERT INTO Match
VALUES (109,'Arsenal',0, 2,'2022');
INSERT INTO Match
VALUES (112,' Liverpool FC',3, 3,'2022');
INSERT INTO Match
VALUES (102,'Tottenham Hotspur',2, 1,'2022');
INSERT INTO Match
VALUES (106,'Manchester United',3, 2,'2022');

-- Query 
SELECT match_id,QUOTE_LITERAL(team_name)
FROM Match
ORDER BY match_id;

Explanation

In the code above:

  • Lines 1–7: We create a table named Match, which has the columns match_id, team_name, away_goal, home_goal, and year.
  • Lines 10–21: We insert data into the Match table.
  • Lines 24–26: We use the QUOTE_LITERAL() function to return a single-quoted string for the team_name column, and we order the query using the team_name column.

RELATED TAGS

sql
RELATED COURSES

View all Courses

Keep Exploring