sql截取时间数据方法函数之截取时间的年月
方法一:DATE_FORMAT() 函数
select trans_date,date_format(trans_date,'%Y-%m') month from Transactions;

方法二:left(str, length)函数
#从左边开始截取
select trans_date,left(trans_date, 7) month from Transactions;

方法三:substring(str, pos,[length]) 函数
str
:要截取字符串的原始字符串
pos
:从第几位开始截取
length
:截取长度(如未设置,则为从截取位到末尾)
select trans_date,substring(trans_date, 1,7) month from Transactions;

方法四:substring_index(str, delim, count)
str
:要截取字符串的原始字符串
delim
:分隔符,确定字符串的位置
count
:指定要返回字符串的数量。如果为正数,则从字符串的起始位置开始计数;如果为负数,从字符串的末尾开始计数。
select trans_date,substring_index(trans_date,'-',2) month from Transactions;

本文转载自: https://blog.csdn.net/m0_70882914/article/details/140802393
版权归原作者 @听风吟 所有, 如有侵权,请联系我们删除。
版权归原作者 @听风吟 所有, 如有侵权,请联系我们删除。