Some functions of SQL

INSTR character lookup function

-- instr语法
instr(string1, string2, start_position, nth_appearance)
  • string1: source string, to be searched in this string.
  • string2: The string to look for in string1.
  • start_position: Indicates where to start searching in string1. This parameter is optional, if omitted, it defaults to 1, and if it is 0, the returned result is also 0. String indices start at 1. If this parameter is positive, search from left to right, if this parameter is negative, search from right to left, return the starting position of the string to be found in the source string.
  • nth_appearance: Represents the number of occurrences of string2 to be found. This parameter is optional, if omitted, the default is 1. If it is negative, the system will report an error.
  • Note that the last two parameters are not ranges. The search range is [start_position, length (string1)], the last parameter is the number of times
SELECT 
 INSTR('SQL数据库开发SQL数据库开发','数据') is_4
,INSTR('SQL数据库开发SQL数据库开发','数据',6) is_12
,INSTR('SQL数据库开发SQL数据库开发','数据',1,20) is_0
from dual 

-- MySQL只能是两个参数。也是返回string2在第一个表达式中第一次出现的位置。如果没找到就返回0
SELECT  INSTR('SQL数据库开发SQL数据库开发','数据') is_4

CONCAT_WS concatenation function

CONCAT_WS ( separator, argument1, argument2 [, argumentN]... )

Note: CONCAT_WS ignores NULL values ​​in columns. Wraps a nullable column with the ISNULL function and provides a default value.

-- 输出 SQL-数据库-开发
SELECT CONCAT_WS('-','SQL','数据库',NULL,'开发')
-- 输出 语文,数学,英语,体育
SELECT CONCAT_WS(',','语文','数学','英语','体育')

-- CONCAT 与 CONCAT_WS 函数相似。只不过它是将参数逐个拼接。
-- 输出 null  (有一个为空,则结果就是空)
SELECT CONCAT('-','SQL','数据库',NULL,'开发')
-- 输出 -SQL数据库开发
SELECT CONCAT('-','SQL','数据库','开发')

REVERSE returns the reverse order of the string

Oracle Chinese and English are normal, but Chinese characters will be garbled.

-- 输出 -SQL数据库开发
SELECT reverse('SQL数据库开发')

WITH AS subqueries

Use a subquery in the Where clause for the following query

SELECT 
 name, salary  
FROM
 People 
WHERE
 NAME IN ( SELECT DISTINCT NAME FROM population WHERE country = "Canada" AND city = "Toronto" ) 
 AND salary >= ( SELECT AVG( salary ) FROM salaries WHERE gender = "Female" )

Improve readability with WITH AS


with toronto_ppl as (
   SELECT DISTINCT name
   FROM population
   WHERE country = "Canada" AND city = "Toronto"
)
, avg_female_salary as (
   SELECT AVG(salary) as avgSalary
   FROM salaries
   WHERE gender = "Female"
)
SELECT 
	name, salary
FROM 
 People
WHERE name in (SELECT name FROM toronto_ppl)
  AND salary >= (SELECT avgSalary FROM avg_female_salary)

temporary function

  • It allows you to break down chunks of code into smaller code chunks
  • It works well for writing clean code
  • It prevents duplication and allows you to reuse code similar to using functions in Python.

Like the following example:

SELECT name, 
       CASE WHEN score>= 90 THEN "优"
            WHEN scoreBETWEEN 70 and 89 THEN "良"
            WHEN scoreBETWEEN 60 and 69 THEN "合格"
            WHEN score< 60 THEN "不合格"
            ELSE "n/a"
       END AS grade
FROM stu

Take advantage of temporary functions to capture case clauses.
The query itself is simpler and more readable with ad-hoc functions that can reuse seniority functions!

CREATE TEMPORARY FUNCTION get_grade(score INT64) AS (
	   CASE WHEN score>= 90 THEN "优"
            WHEN scoreBETWEEN 70 and 89 THEN "良"
            WHEN scoreBETWEEN 60 and 69 THEN "合格"
            WHEN score< 60 THEN "不合格"
            ELSE "n/a"
       END);
SELECT name, get_grade(score) as grade FROM stu

Guess you like

Origin blog.csdn.net/qq_55342245/article/details/129726688