MySQL example to explain single-row functions and character math date process control

MySQL example to explain single-row functions and character math date process control

1. Character Function

insert image description here

1. Case control function

UPPER() :Convert to uppercase

SELECT UPPER('Hello');

insert image description here

LOWER() :Convert to lowercase

SELECT LOWER('Hello');

insert image description here

2. Character control function

LENGTH() : Get the number of bytes of the parameter value

SELECT LENGTH('Chloroplasts do not forget to breathe aaaa');

insert image description here

CONCAT() : concatenate strings

SELECT CONCAT('Hello','世界') AS result;

insert image description here

SUBSTR() : interception (index in MySQL starts from 1, not 0)

#Start intercepting from the 4th one, intercept all the ones after that SELECT SUBSTR('I believe in light',4);

insert image description here

#Start from the first one and intercept 3 SELECT SUBSTR('I believe in light',1,3);

insert image description here

INSTR() : Returns the index of the first occurrence of the substring in the main string. If it does not exist, it returns 0

SELECT INSTR('Chinese national football team will play away against Saudi Arabia on October 13','Saudi Arabia') AS result;

insert image description here

TRIM() : Remove certain characters before and after a string

SELECT TRIM('a' FROM 'aaaaachloroplastaaaaa') AS result;

insert image description here

#Remove spaces SELECT TRIM(' Chloroplast aa ') AS result;

insert image description here

LPAD() : fills the specified length with the specified character on the left, RPAD() fills the right

SELECT LPAD('chloroplast',9,'a') AS result;

insert image description here

REPLACE() :Replace

SELECT REPLACE('a叶aaa chloroplastaaaa','a','B') AS result;

insert image description here

2. Mathematical Functions

ROUND() :round off

#One decimal place is retained by defaultSELECT ROUND(1.62) AS result;

insert image description here

#Keep two decimal placesSELECT ROUND(1.627,2) AS result;

insert image description here

CEIL() : Round up and return the smallest integer greater than or equal to the number

SELECT CEIL(1.002) AS result;

insert image description here

FLOOR() : Round down and return the largest integer less than or equal to the number

SELECT FLOOR(1.002) AS result;

insert image description here

TRUNCATE() :Truncation

SELECT TRUNCATE(1.699,1) AS result;

insert image description here

MOD() : remainder, equivalent to %

SELECT MOD(10,-3) AS result;

insert image description here

3. Date functions

insert image description here

①NOW(): Returns the current system date and time

SELECT NOW();

insert image description here

②CURDATE(): Returns the current system date, excluding time

SELECT CURDATE();

insert image description here

③CURTIME(): Returns the current system time, excluding the date

SELECT CURTIME();

insert image description here

YREAR() : Get the year of the specified date field

SELECT YEAR(NOW());

insert image description here

SELECT YEAR('2021-09-30') AS result;

insert image description here

MONTH() : Get the month of the specified date field, MONTHNAME() can return the English name of the month

SELECT MONTH('2021-09-30') AS result;

insert image description here

SELECT MONTHNAME('2021-09-30') AS result;

insert image description here

Days, hours, minutes, and seconds can all be listed above

STR_TO_DATE() : Convert characters to dates in the specified format

#It is equivalent to parsing: the two parameter formats must match SELECT STR_TO_DATE('9-30 2021','%m-%d %Y') AS result;

insert image description here

DATE_FORMAT() : Convert date to character

#It is equivalent to formatting SELECT DATE_FORMAT(NOW(),'%Y year %m month %d day') AS result;

insert image description here

4. Other functions

#Check the version SELECT VERSION();
#View the database SELECT DATABASE();
#View userSELECT USER();

5. Process Control Function

IF() : judgment, the first parameter is the condition, the second is the return of true, and the third is the return of false

SELECT IF(10>5,'big','small') AS result;

insert image description here

CASE() usage 1: similar to switch in Java

CASE WHEN constant to be judged 1 THEN statement 1; or the value to be displayed 1
...
ELSE statement; or the value to be displayed END

Example

#The example is the value to be displayed, without ';'
SELECT `last_name`,`salary`,`department_id`,
CASE `department_id`
WHEN 100 THEN `salary`*(1+0.8)
WHEN 90 THEN `salary`*(1+0.6)
WHEN 80 THEN `salary`*(1+0.4)
WHEN 70 THEN `salary`*(1+0.2)
ELSE `salary`
END AS finalsalaryFROM employees;

③ Use of CASE() : Similar to multiple if in Java

CASE 
WHEN condition 1 THEN SELECT statement 1; or the value to be displayed 1
...
ELSE statement; or the value to be displayed END

Example

#The example is the value to be displayed, without ';'
SELECT `last_name`,`salary`,
CASE
WHEN `salary`>20000 THEN 'A level'
WHEN `salary`>15000 THEN 'B level'
WHEN `salary`>10000 THEN 'C level'
ELSE 'D level'
END AS level FROM employees;

insert image description here

This concludes this article on MySQL's in-depth explanation of single-row functions and character math date process control. For more relevant MySQL single-row function content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Very practical MySQL function comprehensive summary detailed example analysis tutorial
  • MySQL practical window function SQL analysis class students' test scores and living expenses
  • MySQL Database Basics SQL Window Function Example Analysis Tutorial
  • mysql calculation function details
  • MySQL essential basics: grouping function, aggregate function, grouping query detailed explanation
  • A brief introduction to MySQL functions
  • MySQL spatial data storage and functions
  • Comprehensive summary of mysql functions

<<:  Common tags in XHTML

>>:  Conditional comment style writing method and sample code

Recommend

MySQL 8.0.23 Major Updates (New Features)

Author: Guan Changlong is a DBA in the Delivery S...

A detailed account of the process of climbing a pit of Docker deployment service

First time writing. Allow me to introduce myself....

Linux Dig command usage

Dig Introduction: Dig is a tool that queries DNS ...

Let's talk about the difference between MyISAM and InnoDB

The main differences are as follows: 1. MySQL use...

MySQL multi-master and one-slave data backup method tutorial

Overview Operations on any one database are autom...

Detailed installation and use of virtuoso database under Linux system

I've been researching some things about linke...

Introduction to MySQL <> and <=> operators

<> Operator Function: Indicates not equal t...

Pure js to achieve typewriter effect

This article example shares the specific code of ...

Introduction to new features of ECMAscript

Table of contents 1. Default values ​​for functio...

Vue recursively implements custom tree components

This article shares the specific code of Vue recu...

Implementation code for operating mysql database in golang

Preface Golang provides the database/sql package ...

Detailed explanation of the use of MySQL group links

Grouping and linking in MYSQL are the two most co...

Tutorial on installing jdk1.8 on ubuntu14.04

1. Download jdk download address我下載的是jdk-8u221-li...