RIGHT functions in BigQuery

The RIGHT() function in BigQuery is used to extract a specified number of characters from the right side of a string.

SQL Syntax

RIGHT(string, number_of_characters)

The function takes two arguments:

Example: Create an Employees Table In this example, we create an employees table in BigQuery and insert sample employee data for practice.

Create Table

SQL Query

CREATE OR REPLACE TABLE `ashishcoder.Coding_Dataset.employees` (
    EmployeeID INT64,
    Name STRING,
    Age INT64,
    Salary INT64,
    Department STRING,
    Gender STRING
);

Insert Data into Employees Table

SQL Query

INSERT INTO `ashishcoder.Coding_Dataset.employees` VALUES
(1, 'Ashish', 24, 25000, 'Electrical', 'M'), 
(2, 'Kirshan', 32, 30000, 'Electrical', 'M'),
(3, 'Anjali', 20, 25000, 'Electronics', 'F'),
(4, 'Manjulika', 30, 15000, 'Electrical', 'F'),
(5, 'Katrina', 37, 18000, 'Computer', 'F'),
(6, 'Esha', 37, 20000, 'Computer', 'F'),
(7, 'Ankita', 20, 9000, 'Electronics', 'M'), 
(8, 'Meenakshi', 30, 15000, 'Computer', 'F'),
(9, 'Alia', 37, 16000, 'Electronics', 'F');

View All Records from Employees Table

SQL Query

SELECT *
FROM `ashishcoder.Coding_Dataset.employees`;

Output The above query returns all records from the employees table.

Right function in BigQuery

RIGHT() Function in BigQuery The RIGHT() function is used to extract characters from the right side (end) of a string.

Example: Extract last three characters from the Department column

SQL Query

SELECT
  EmployeeID,
  Name,
  Department,
  RIGHT(Department, 3) AS dept_name
FROM `ashishcoder.Coding_Dataset.employees`;
Right function in BigQuery