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 data analysis storage engine example explanation

Table of contents 1. Introduce cases 2. View the ...

Vue calls the computer camera to realize the photo function

This article example shares the specific code of ...

A brief discussion on the design and optimization of MySQL tree structure tables

Preface In many management and office systems, tr...

How to compile and install opencv under ubuntu

Easy installation of opencv2: conda install --cha...

Front-end state management (Part 1)

Table of contents 1. What is front-end state mana...

MySQL 5.7.17 installation and configuration method graphic tutorial under win7

I would like to share with you the graphic tutori...

HTML form tag tutorial (5): text field tag

<br />This tag is used to create a multi-lin...

Vue implements two routing permission control methods

Table of contents Method 1: Routing meta informat...

Example of how nginx implements dynamic and static separation

Table of contents Deploy nginx on server1 Deploy ...

How to implement adaptive container with equal aspect ratio using CSS

When developing a mobile page recently, I encount...

About the problem of writing plugins for mounting DOM in vue3

Compared with vue2, vue3 has an additional concep...

How to configure nginx to return text or json

Sometimes when requesting certain interfaces, you...

Docker deployment springboot project example analysis

This article mainly introduces the example analys...

Setting the engine MyISAM/InnoDB when creating a data table in MySQL

When I configured mysql, I set the default storag...