1. 程式人生 > 其它 >mysql資料處理函式

mysql資料處理函式

函式 作用
Lower 轉換小寫
Upper 轉換大寫
Substr 取子串(substr(被擷取的字串,起始下標(從1開始),擷取長度))
Length 取長度
Trim 去除首尾空格
Str_to_date 將字串轉換為日期
Date_format 格式化日期
format 設定千分位
Round 四捨五入
Rand 生成隨機數(0~1)
Ifnull 可以將null轉換成一個具體值
Ceiling 向上取整
Floor 向下取整

舉例:

-- 轉換小寫函式
select lower(name) from info;

-- 轉換大寫函式

select upper(name) from info;

-- 取字串
select substr(name,1,1) from info;

select * from info where substr(name,1,1) = upper('m');

-- 獲取長度
select length(name) as length from info;


--去收尾空格

insert into info value(null,'  llo one','2011-12-28',23,45)

select * from info

select* from info where name = trim(lower('Mary  '));

-- 將字串轉換為日期
select * from info where birth = str_to_date('2022-03-12','%Y-%m-%d');

-- 格式化日期
select date_format(birth,'%Y-%m-%d %H:%i:%s') as birth from info;

-- 設定千分位
select format(price,4) from info


-- 生成隨機數
select format(rand(),2)

-- 向上取整
select ceiling(rand())

-- 向下取整

select floor(rand())