Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

sql
postgresql

How to use the QUOTE_IDENT() function in SQL

Maria Elijah

SQL QUOTE_IDENT() function

The QUOTE_IDENT() function returns the provided string as a string enclosed in double quotation marks, allowing it to be used as an identifier in a SQL statement.

Syntax

QUOTE_IDENT(string)

Parameter

  • string: This represents the input string. It can be char or varchar.

Code

The following code demonstrates how to use the QUOTE_IDENT() function in SQL.

CREATE TABLE Employee (
    id int,
    first_name varchar(50),
    last_name varchar (50),
    salary int,
    gender varchar(10),
    state varchar(15)
);

-- Insert data
INSERT INTO Employee
VALUES (01,'Sharon', 'Peller',40000,'F','Kogi');
INSERT INTO Employee
VALUES (02,'Paul', 'Dons',150000,'M','Lagos');
INSERT INTO Employee
VALUES (03,'Ameera', 'Abedayo',200000,'F','Imo');
INSERT INTO Employee
VALUES (04,'Maria', 'Elijah',320000,'F','Lagos');
INSERT INTO Employee
VALUES (05,'David', 'Hassan',250000,'M','Abuja');
INSERT INTO Employee
VALUES (06,'Niniola', 'Disu',80000,'F','Lagos');
INSERT INTO Employee
VALUES (08,'Joe', 'Smith',75000, 'M','Lagos');

-- Query 
SELECT first_name,QUOTE_IDENT(gender)
FROM Employee
ORDER BY first_name;
QUOTE_IDENT() function in SQL

Explanation

In the code above:

  • Lines 1 to 7: We create the table called Employee which has the columns id, name, salary, gender, and state.
  • Lines 11 to 24: We insert data into the Employee table.
  • Line 27 to 28: Using the QUOTE_IDENT() function, we return a double-quoted string for the gender column and we order the query using the first_name column.

RELATED TAGS

sql
postgresql
RELATED COURSES

View all Courses

Keep Exploring