MySQL共通関数-文字列関数、数値関数、日付関数

7369 ワード

文書ディレクトリ
  • 1、文字列関数
  • 1.1、`concat`
  • 1.2、`insert`
  • 1.3、文字列変換大文字と小文字
  • 1.3.1、`lower`
  • 1.3.2、`upper`
  • 1.4、両側の指定個数を取得する文字列
  • 1.4.1、`left`
  • 1.4.2、`right`
  • 1.5、文字列スペースを削除
  • 1.5.1、`ltrim`
  • 1.5.2、`rtrim`
  • 1.5.3、`trim`
  • 1.6、パディング文字列
  • 1.6.1、`lpad`
  • 1.6.2、`rpad`
  • 1.7、`repeat`
  • 1.8、`strcmp`
  • 1.9、`replace`
  • 1.10、`substring`
  • 2、数値関数
  • 2.1、`abs`
  • 2.2、`ceil`
  • 2.3、`float`
  • 2.4、`mod`
  • 2.5、`rand`
  • 2.6、`round`
  • 2.7、`truncate`
  • 3、日時関数
  • 3.1、`curdate`と`current_date`
  • 3.2、`curtime`と`current_time`
  • 3.2、`now`
  • 3.3、`year`
  • 3.4、`weekday`
  • 3.4、`date_format`

  • 1、文字列関数
    関数#カンスウ#
    機能
    concat(s1,s2,…sn)
    接続s 1,s 2,...,snは文字列である
    insert(str,x,y,instr)
    文字列strをx番目の位置から、y文字長のサブ列を文字列instrに置き換えます.
    lower(str)
    文字列strの文字を小文字に変更
    upper(str)
    文字列strの文字を大文字に変更する
    left(str,x)
    文字列strの一番左のx文字を返します
    right(str,x)
    文字列strの右端のx文字を返します
    lpad(str,n,pad)
    strの一番左を文字列padで埋め、長さがn文字になるまで
    rpad(str,n,pad)
    strの右端を文字列padで埋め、長さがn文字になるまで埋めます.
    ltrim(str)
    文字列strの左側のスペースを削除
    rtrim(str)
    文字列strの右側のスペースを削除
    repeat(str,x)
    strがx回繰り返した結果を返す
    replace(str,a,b)
    文字列strに現れるすべての文字列aを文字列bで置き換える
    strcmp(s1,s2)
    比較文字列s 1とs 2
    trim(str)
    文字列の行末と行頭のスペースを削除
    substring(str,x,y)
    文字列str xの位置からy文字の長さを返す文字列
    1.1、 concat concat(s1,s2,......sn):接続s1,s2,.....,snは、可変パラメータと同様の文字列です.
    select 'abc', concat('abc','123'), concat('abc','123','789');
    #   :abc;abc123;abc123789
    

    1.2、 insert insert(str,x,y,instr):文字列strx番目の位置から開始し、y文字長のサブストリングをinstr文字列に置き換えます.
    select insert('abcdefg',2,4,'123');
    #   :a123fg
    

    1.3、文字列変換大文字と小文字
    1.3.1、 lower lower(str):文字列strの文字を小文字に変更
    select lower('abcDEFg');
    #   :abcdefg
    

    1.3.2、 upper upper(str):文字列strの文字を大文字に変更
    select upper('abcDEFg');
    #   :ABCDEFG
    

    1.4.両辺の指定個数を取得する文字列
    1.4.1、 left left(str,x):文字列strの一番左のx文字を返します.
    select left('abcdefg',3);
    #   :abc
    

    1.4.2、 right right(str,x):文字列strの右端にあるx文字を返します.
    select right('abcdefg',3);
    #   :efg
    

    1.5、文字列のスペースを取り除く
    1.5.1、 ltrim ltrim(str):文字列strの左側のスペースを削除
    select ltrim('  abc  ');
    #   :abc  
    

    1.5.2、 rtrim rtrim(str):文字列strの右側のスペースを削除
    select rtrim('  abc  ');
    #   :  abc
    

    1.5.3、 trim trim(str):文字列strの両端のスペースを削除
    select trim('  abc  ');
    #   :abc
    

    1.6、入力文字列
    1.6.1、 lpad lpad(str,n,pad):pad文字列の一番左にstr文字の長さがn文字になるまで埋め込みます.
    select 'abcdefg',lpad('abc',5,'*');
    #   :**abc
    

    1.6.2、 rpad rpad(str,n,pad):pad文字列の右端をstr文字列で塗りつぶし、n文字長になるまで
    select 'abcdefg',rpad('abc',5,'*');
    #   :abc**
    

    1.7、 repeat repeat(str,x):strx回繰り返した結果を返します.
    select repeat('abc',3);
    #   :abcabcabc
    

    1.8、 strcmp strcmp(s1,s2):比較文字列s1およびs2
    select strcmp('abc','abcd');
    #   :-1
    select strcmp('abc','abc');
    #   :0
    select strcmp('abc','ab');
    #   :1
    

    1.9、 replace replace(str,a,b):文字列bを文字列strに置き換えます.
    select replace('abcdabag','ab','12');
    #   :12cd12ag
    

    1.10、 a substring:文字列substring(str,x,y)strの位置からx文字の長さを返す文字列
    select substring('abcdefg',2,5);
    #   :bcdef
    

    2、数値関数
    関数#カンスウ#
    機能
    abs(x)
    xの絶対値を返す
    ceil(x)
    xより大きい最小整数値を返します
    floor(x)
    x未満の最大整数値を返します
    mod(x,y)
    x/yのモードを返す
    rand()
    0~1のランダム値を返します
    round(x,y)
    戻りパラメータxの四捨五入yビット小数の値
    truncate(x,y)
    戻り値xがyビット小数に切り捨てられた結果
    2.1、 y abs:abs(x)の絶対値を返します.
    select abs(-2);
    #   :2
    

    2.2、 x ceil:ceil(x)より大きい最小整数を返します.
    select ceil(4.5)
    #   :5
    

    2.3、 x float:float(x)未満の最大整数を返します.
    select floor(4.5);
    #   :4
    

    2.4、 x mod:mod(x,y)のモジュール(残高)を返します.
    select mod(10,3);
    #   :1
    

    2.5、 x/y rand:rand()の乱数を返します.
    select rand();
    #   :0.7569426025794285
    

    2.6、 0-1 round:戻りパラメータround(x,y)の四捨五入xビット小数の値
    select round(5.4);
    #   :5
    select round(4.15922,2);
    #   :4.16
    

    2.7、 y truncate:truncate(x,y)xビットの小数に切り捨てられた結果を返します.
    select truncate(4.15922,3);
    #   :4.159
    

    3、日付時間関数
    関数#カンスウ#
    機能
    curdate()
    現在の日付を返す
    curtime()
    現在の時刻を返す
    now()
    現在の日付と時刻を返します.
    unix_timestamp(date)
    日付dateを返すunixタイムスタンプ
    from_unixtime
    Unixタイムスタンプの日付値を返します
    week(date)
    戻り日dateは1年の何週目ですか
    year(date)
    日付dateの年を返す
    hour(time)
    timeの時間値を返す
    minute(time)
    timeの分値を返します
    monthname(date)
    日付dateの月名を返す
    date_format(date,fmt)
    文字列fmtでフォーマットされた日付date値を返します
    date_add(date,interval expr type)
    日付または時間の値に時間間隔を加えた時間値を返します.
    datediff(expr,expr2)
    開始時間exprと終了時間expr 2の間の日数を返します.
    3.1、ycurdate
    select curdate()
    #   :2020-04-07
    

    3.2、current_datecurtime
    select curtime()
    #   :13:41:58
    

    3.2、 current_time
    select now()
    #   :2020-04-07 13:42:34
    

    3.3、 now
    select year(now());
    

    3.4、 year
    select weekday(now())
    

    3.4、 weekday
    select date_format(now(),'%y %m %r ')
    #   :20 04 01:47:43 PM