SQL RIGHT 函式

摘要:在本教程中,您將學習如何使用 SQL RIGHT 函式從字串的末尾返回指定數量的字元。

SQL RIGHT 函式簡介 #

在 SQL 中,RIGHT 函式接收一個字串,並從字串的末尾(右側)返回指定數量的字元。

以下是 RIGHT 函式的語法

RIGHT(string, number_of_characters)Code language: SQL (Structured Query Language) (sql)

RIGHT 函式接受兩個引數

  • string:您希望從中返回字元的輸入字串。
  • number_of_characters:您希望從輸入字串中返回的字元數。

RIGHT 函式返回一個字串,該字串包含從輸入字串右側開始的指定數量的字元。

如果輸入的 stringnumber_of_charactersNULL,則返回 NULL

基本的 SQL RIGHT 函式示例 #

以下查詢使用 RIGHT 函式返回檔名的副檔名

SELECT
  RIGHT('resume.pdf', 3) extension;Code language: SQL (Structured Query Language) (sql)

試一試

輸出

 extension
-----------
 pdfCode language: SQL (Structured Query Language) (sql)

提取副檔名 #

首先,建立一個名為 performance_evaluations 的新表,用於儲存員工的績效評估

CREATE TABLE performance_evaluations (
  employee_id INT PRIMARY KEY,
  rating INT NOT NULL,
  evaluation_form VARCHAR(255) NOT NULL
);Code language: SQL (Structured Query Language) (sql)

試一試

其次,向 performance_evaluations 表中插入行

INSERT INTO
  performance_evaluations (employee_id, rating, evaluation_form)
VALUES
  (101, 4, 'neena.pdf'),
  (102, 3, 'lex.pdf'),
  (103, 5, 'alexander.doc'),
  (104, 3, 'bruce.xls'),
  (105, 3, 'david.xls');Code language: SQL (Structured Query Language) (sql)

試一試

第三,從 performance_evaluations 表中檢索資料

SELECT
  *
FROM
  performance_evaluations;Code language: SQL (Structured Query Language) (sql)

試一試

輸出

 employee_id | rating | evaluation_form
-------------+--------+-----------------
         101 |      4 | neena.pdf
         102 |      3 | lex.pdf
         103 |      5 | alexander.doc
         104 |      3 | bruce.xls
         105 |      3 | david.xlsCode language: SQL (Structured Query Language) (sql)

對錶資料使用 RIGHT 函式 #

以下查詢使用 RIGHT 函式檢索員工姓名、評級、評估表以及評估表的副檔名

SELECT
  first_name,
  rating,
  evaluation_form,
  RIGHT(evaluation_form, 3) form_extension
FROM
  performance_evaluations p
  INNER JOIN employees e ON e.employee_id = p.employee_id
ORDER BY
  first_name;Code language: SQL (Structured Query Language) (sql)

試一試

輸出

 first_name | rating | evaluation_form | form_extension
------------+--------+-----------------+----------------
 Alexander  |      5 | alexander.doc   | doc
 Bruce      |      3 | bruce.xls       | xls
 David      |      3 | david.xls       | xls
 Lex        |      3 | lex.pdf         | pdf
 Neena      |      4 | neena.pdf       | pdfCode language: SQL (Structured Query Language) (sql)

在 WHERE 子句中使用 RIGHT 函式 #

以下語句在 WHERE 子句中使用 RIGHT 函式,以查詢副檔名為 pdfxls 的評估表

SELECT
  first_name,
  rating,
  evaluation_form,
  RIGHT(evaluation_form, 3) form_extension
FROM
  performance_evaluations p
  INNER JOIN employees e ON e.employee_id = p.employee_id
WHERE
  RIGHT(evaluation_form, 3) IN ('pdf', 'xls')
ORDER BY
  first_name;Code language: SQL (Structured Query Language) (sql)

試一試

輸出

 first_name | rating | evaluation_form | form_extension
------------+--------+-----------------+----------------
 Bruce      |      3 | bruce.xls       | xls
 David      |      3 | david.xls       | xls
 Lex        |      3 | lex.pdf         | pdf
 Neena      |      4 | neena.pdf       | pdfCode language: SQL (Structured Query Language) (sql)

將 RIGHT 函式與聚合函式一起使用 #

以下查詢將 RIGHT 函式與 COUNT 聚合函式一起使用,以獲取每種表單副檔名的計數

SELECT
  RIGHT(evaluation_form, 3) form_extension,
  COUNT(*) extension_count
FROM
  performance_evaluations
GROUP BY
  RIGHT(evaluation_form, 3)
ORDER BY
  form_extension;Code language: SQL (Structured Query Language) (sql)

試一試

輸出

 form_extension | extension_count
----------------+-----------------
 doc            |               1
 pdf            |               2
 xls            |               2Code language: SQL (Structured Query Language) (sql)

摘要 #

  • 使用 RIGHT 函式從字串的末尾提取指定數量的字元。

資料庫 #

本教程是否有幫助?
© .