MySQL資料庫—日期與時間函數

來源:互聯網
上載者:User

標籤:local   正弦   串連數   產生   轉換   dia   span   pos   char   

一、 日期和時間函數

函數的概念:按指定格式輸入參數,返回正確結果的運算單元

1. 返回當前日期:curdate()

                   current_date()

                   current_date()+0可以將當前日期轉換為數值型

    例: select curdate(), current_date(), curdate()+0

            2015-09-22       2015-09-22       20150922

2. 返回目前時間:curtime()

                       current_time()

                       current_time()+0

    例:select curtime(),current_time(),curtime()+0

            20:47:53          20:47:53        204753

3. 返回當前日期和時間:current_timestamp()

                                localtime()

                                now()

                                sysdate()

    例: select now() as ‘此時此刻‘,sysdate() as ‘此時此刻2‘ 

4. 擷取月份: month(date)

                 monthname(date)

    例:select month(‘2015-07-22‘) ----> 7

          select monthname(‘2015-07-22‘) ---->July

5. 擷取星期:dayname(date)

                 dayofweek(date)

                 weekday(date)

                 week(date)

                 weekofyear(date)

   例:select dayname(‘2015-09-23‘) --->Wednesday

        select dayofweek(‘2015-09-23‘) --->3(以星期天作為第一天)

        select weekday(‘2015-09-23‘) --->1(以星期一為第0天)

        select week(‘2015-01-01‘) --->0(在一年中屬於哪個星期)

        select weekofyear(‘2015-01-01‘) --->1(在一年中屬於哪個星期)

6. 擷取天數:dayofyear(date) ---->計算參數所指定的日期在這一年中是屬於第幾天

                  dayofmonth(date)---->計算參數所指定的日期在這一月中是屬於第幾天

       例:select week(‘2015-01-01‘) -->1

            select week(‘2015-01-01‘) --->1

7.擷取年份,季度,小時,分鐘,秒鐘

        year(date)           select year(‘2015-01-01‘) -->2015

        quarter(date)       select quarter(‘2015-09-22‘) --->3

        hour(time)

        minute(time)

        second(time)

二 、數學函數

1. 絕對值函數 ->ABS(X)

例:select ABS(-8) -->8

2. 符號函數sign(x):判斷一個數字是正數還是負數還是零?(正數傳回值為1,負數傳回值為-1,零傳回值是0) 

例:select sign(-8) -->-1

3. 擷取隨機數的函數:rand()    例:select rand() --> (每次運行將產生一個新的隨機數)

                             rand(x)   例:select rand(3) --> 產生的隨機數為固定的數

4. 擷取整數的函數:ceil(x)   ceilung(x)  例:select ceil(3.5) -->4 (取不小於3.5的最小整數)

                          floor(x)            例:select floor(3.5) -->3(取小於3.5的最大整數)

5.四捨五入函數:round(x)      例:select round(3.5) ---> 4

                      round(x,y)    例:select floor(3.14,1) --->3.1(保留小數位元1)

                                         例:select floor(3.14,-1) --->0 (保留到十位元)

                      truncate(x,y)  例:select truncate(3.15,1) --->3.1(只保留一位小數,不進行四捨五入直接截去)

6. 求餘數函數:mod(x,y)       例:select mod(31,8) --->7

7. 冪運算函數:pow(x,y)/power(x,y) 求x的y次冪        例:select pow(2,3) --->8

                     exp(x) 求自然對數的冪

                     sqrt(x) 求某一個數的平方根     例:select sqrt(2) ----1.41421356237

                                                                   select sqrt(9) ---- 3

8.角度弧度互換函數:radians(x) 將角度互換成弧度    例:select radians(180) --->3.1415926...

                            degrees(x) 將弧度變換成角度   例:select degrees(3.14) --->179.9.8...

9. 圓周率函數:PI()

10. 三角函數:sin(x) 正弦

                   asin(x) 反正弦

                   cos(x) 餘弦

                   acos(x) 反餘弦

                   tan(x) 正切

                   atan(x) 反正切

                   cot(x) 餘切

 三 、字串函數

1.計算字元數和字串長度的函數

char_length(s):計算字串中有幾個字元

例: select char_length(‘abc‘) --->3

例: select char_length(‘你好嗎‘) --->3

length(s):計算字串在記憶體中占幾個位元組

例: select length(‘abc‘) --->3

例: select length(‘你好嗎‘) --->9

2.合并字串函數:   concat(s1,s2...) 把參數中的字串串連成一個新的字串              例:select concat(‘你好嗎‘,‘abc‘,‘word‘) --->你好嗎abcword

                          concat_ws(x,s1,s2...)  例:select concat_ws(‘#‘,‘你好嗎‘,‘abc‘,‘word‘)---->你好嗎#abc#word

3.替換字串函數:INSERT(S1,X,LEN,S2)  在S1這個字串當中以X位置開頭數len這樣長度的字串給它替換成S2

                                  例:select insert(‘ABCDEF‘,‘2‘,‘3‘,‘##‘) --->A##EF

                        REPLACE(S,S1,S2)  把原始的字串中的某個子字串用另一個字串代替

                                  例:select insert(‘ABCDEFAB‘,‘ab‘,‘%‘) --->%CDEF%

4.截取字串的函數:left(s,n)   例:select left(‘ABCDEFAB‘,‘3‘) --->ABC

                            right(s,n)  例:select right(‘ABCDEFAB‘,‘3‘) --->FAB

5.重複生產字串函數:repeat(s,n)  例: select repeat(‘ABC‘,‘3‘) --->ABCABCABC

6.大小寫轉換的函數:lower(x,y)/lcase(x)  例:select lcase(‘ABC‘)--->abc

                            upper(x)/ucase(x)   例:select ucase(‘abc‘)--->ABC

7.填充字串的函數:lpad(s1,len,s2) 從左邊開始填充  例:select lpad(‘ABCd‘,10,‘#‘)---->######ABCd

                            rpad(s1,len,s2) 從右邊開始填充  例:select rpad(‘ABCd‘,10,‘#‘)----->ABCd######

8.刪除空格函數:LTRIM(S)/RTRIM(S) 刪除左邊的空格/刪除右邊的空格 例:select RTRIM(‘   ABCd   ‘) - ->   ABCd   

                      TRIM(S)  刪除兩邊的空格   例:select TRIM(‘   ABCd   ‘)----->ABCd

9.刪除指定字串:Trim(s1 from s)   例:select trim(‘A‘,from ‘ABCADE‘) --->BCADE (刪除靠邊的A)

10.擷取子字串:SUBSTRING(S,N,LEN)  例:select substring(‘ABCADE‘,3,2)----->CA

                         MID(S,N,LEN)      例:select substring(‘ABCADE‘,3,2)----->CA

11.返回指定位置字串函數:ELT(N,S1,S2....)  例:select elt(2,‘ABC‘,‘DEF‘,‘MYSQL‘)  ---->DEF

12.返回指定字串位置:FIELD(S,S1,S2...)  例:select field(‘HI‘,‘HI‘,‘HO‘,‘HE‘,‘HU‘) ---->1

四 、系統函數

1. 擷取MySQL版本號碼的函數:version()     例: select version()

2. 查看當前的使用者串連數:connection_id()   例:select connection_id

3. 查看當前所用資料庫函數:datebase() 

                                     schema9()

4. 擷取使用者名稱的函數:user()                例:select user(),current_user(),system_user(),session_user() [email protected]

                             current_user()

                             system_user()

                             session_user()

MySQL資料庫—日期與時間函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.