MySql basic query-basic functions

#一. Character function
#Calculate character length

SELECT LENGTH('john')
#Display encoding format
SHOW VARIABLES LIKE '%char%'
#Turn case

SELECT UPPER('john')
SELECT LOWER('JOHN')

SELECT SUBSTR('李莫愁爱上了陆展元',7) AS out_put

#Intercept string
SELECT SUBSTR('李莫愁爱上了陆展元',1,3) AS out_put

# Splicing string
SELECT CONCAT(UPPER(SUBSTR(last_name,1,1)),'_',LOWER(SUBSTR(last_name,2))) FROM employees

#Find index, return the actual index
SELECT INSTR('杨杨阿姨那个','阿姨')

#Remove spaces, remove spaces before and after

SELECT TRIM(' 666  ')
SELECT LENGTH(TRIM(' 666  '))

SELECT TRIM('a' FROM 'aaaaaa张aaaa无极aaaaaa')

#Fill the specified length with the specified characters

SELECT LPAD('赵子龙',10,'*')

SELECT RPAD('赵子龙',10,'*')

#Replace the specified string
SELECT REPLACE('***aaaa****','aaaa','bbbb')


#二. Mathematical Functions
# Rounding

SELECT ROUND(-1.56)
SELECT ROUND(1.46)
SELECT ROUND(1.462,2)

#Round up, return an integer greater than or equal to the parameter #Round
SELECT CEIL(1.02)
down, return an integer less than or equal to the parameter

SELECT FLOOR(1.82)

#Truncation
SELECT TRUNCATE(1.69999,2)

#取余
SELECT MOD(10,3)


#三. Date function

SELECT NOW()

SELECT CURDATE()

SELECT CURTIME()

SELECT YEAR(NOW())

#Convert the specified characters into date format

SELECT STR_TO_DATE('2021-04-01','%Y-%m-%d')
SELECT STR_TO_DATE('2021-4-1','%Y-%c-%d')

#Convert the specified date into a string
SELECT DATE_FORMAT(NOW(),'%y年%m月%d日')


#四. Other functions

SELECT VERSION()
SELECT DATABASE()
SELECT USER()

Guess you like

Origin blog.51cto.com/14049943/2679150