保留小数 round()
将数字四舍五入到小数点后两位:
SELECT ROUND(135.375, 2);
提取字符串子串
从列中的文本中提取一个子字符串(从位置 2 开始,提取 5 个字符):
SELECT SUBSTRING('CustomerName', 2, 5) AS ExtractString
结果: ustom
从字符串中提取子串(从末尾开始,位置-5,提取5个字符):
SELECT SUBSTRING("SQL Tutorial", -5, 5) AS ExtractString;
结果: orial
字符串拼接 concat
concat(字符串1,字符串2,字符串3,...)
将三列添加到一个"Address"列中:
SELECT CONCAT(Address, " ", PostalCode, " ", City) AS Address FROM Customers;
将字符串转换为字母大写 upper(str)
将字符串转换为小写形式:LOWER ()
日期转换
获取日期的年月日
year select yaer('2020-01-01')
2020
month select month('2020-01-01')
1
day select day('2020-01-01')
1
按指定格式获取日期
例select date_format('2020-01-01','%Y %m %d')
%Y:四位年份(例如:2024)
%y:两位年份(例如:24)
%m:两位月份(01 - 12)
%c:月份(1 - 12)
%M:月份名称(January - December)
%b:缩写的月份名称(Jan - Dec)
%d:两位日期(01 - 31)
%e:日期(1 - 31)
%H:24小时制的小时(00 - 23)
%h:12小时制的小时(01 - 12)
%i:两位分钟(00 - 59)
%s:两位秒数(00 - 59)
%p:AM 或 PM
%W:完整的星期几名称(Sunday - Saturday)
%a:缩写的星期几名称(Sun - Sat)