SQL School Lesson 7: Using Functions to Process Data

      Recent projects need to use some knowledge about stored procedures. To be honest, knowledge about databases is necessary at any time. A program is nothing more than a combination of functions and data, and how to operate data must be done without SQL, so from today From now on, I will simply record the basic knowledge about SQL, and put it under the SQL school for now.

      Like other languages, SQL can use functions to manipulate data. However, for different DBMSs, although they support almost all function types, the presentation and use of different types of functions in different DBMSs are very different, and the functions that are used more frequently are often the consistency supported by each DBMS. the worse. vice versa.


1. Example of function difference


function grammar
extract string Accesses use MID(), DB2, Oracle, PostgreSQL, SQLite use SUBSTR(), MySQL and SQLServer use SUBSTRING()
data type conversion DB2 and PostgreSQL use CAST(), MySQL and SQL Server use CONVERT(), Accese and Oracle use multiple functions
Extract current date Accese uses NOW(), DB2 and PostgreSQL use CURRENT_DATE, MySQL uses CURDATE(), Oracle uses SYSDATE(), SQLite uses DATE(), SQL Server uses GETDATE()

As can be seen from the above simple examples, each DBMS basically supports different function types, but the implementation methods are still very different. For details, please refer to the operation document of the corresponding DBMS. Many people do not recommend using functions in SQL for manipulating data due to the portability of the code ; the decision is yours .


2. Simple usage example

(1) Text processing functions

UPPER()

select  name   UPPER(name) AS   UPname

from table name;

View the data in the column corresponding to the name in the table, and convert all the queried data into uppercase letters and display them in the UPname column; it is very simple to use

List of commonly used text data processing functions:

function illustrate
LEFT() Returns the left character of the string
RIGHT() Returns the right character of the string
LOWER(), Accses use LCASE() Convert all string characters to lowercase
UPPER(), Accese uses UCASE() Convert all string characters to uppercase
LENGTH() return string length
TRIM() Remove spaces from both sides of a string
LTRIM () Remove spaces from the left of the string
RTRIM() Remove spaces from the right of a string

(2) Date and time processing functions

       Each DBMS has its own unique format for storing dates and times, allowing for fast and efficient filtering and saving space. However, applications must not directly use this form of data, so at this time, corresponding functions are needed to process them; such functions are frequently used, and in other words, different DBMSs support such functions in a very consistent manner. Difference. Therefore, this part is still practiced in combination with the specific DBMS operation instructions, and the differences between each DBMS are too great.


(3) Numerical processing functions

This part is used relatively rarely, however, the consistency of support for this type of function is the best among various DBMSs, hahaha; the following is a list of the most commonly used numerical processing functions:

function illustrate
ABS() Returns the absolute value of a number
COS() Returns the cosine of a number
WITHOUT() Returns the sine of a number
TAN() Returns the tangent of a number
PI() returns a pi
EXP() Returns the exponential value of a number
SQRT() Returns the square root of a number


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324683503&siteId=291194637