SQL Server T—SQL 函數

來源:互聯網
上載者:User

標籤:code   時間   四捨五入   對象   aabb   索引   求和   time()   floor   

一彙總函式(統計函數)!!!!!

計算個數   count(  )  

                     select count(*) from 表名

求和  sum(  )

                    select sum(列名) from 表名

求平均數  avg ()

                   select avg(列名) from 表名

求最大值  max ()

                   select max(列名) from 表名

求最小值  min()

                   select min(列名) from 表名

 

二.數學函數:操作一個資料,返回一個結果

取上限  ceiling ()

                select code,name,ceiling(price) from car ;         --進行取值運算後 列名 消失 可用 as 起別名

取下限  floor ()

              select floor(price) from car

絕對值   ABS()

四捨五入  round  (A  ,B  )                                           -- A是要求的值或列,B是小數點後保留多少位

                   select round ( oil , 0 )  from car                    --小數位只看指定位元後面一位是否需要進位,不累加      

圓周率  PI( ),                                                -- 括弧裡不需要加東西

                   round  (PI( ),2  )

開根號   SQRT ()

平方,乘以自己   SQUARE ()

 

三.字串函數 !!!

轉換大寫   upper()

          select upper(pic) from car;

轉換小寫     lower()

去左側空格    ltrim ( )                             --  無兩側去空格的函數                           

去右側空格    rtrim( )

截取   substring( A , B  , C  )           -- -- A 操作的目標對象 B 索引(索引從1開始)c 個數 截多少位 

         left (  ,  b )     ---從左邊截取 b 位

        right (  , b )    ---從右邊截取 b 位                                                                      

長度   len()

                select len(‘aaaaaa‘); 返回幾個長度

替換   replace( A ,B , C )                             -- -- A操作的目標對象 B 要被換掉的 C 要換上是

                 select replace(‘aaaaabbaaaaa‘,‘bb‘,‘haha‘);把第一個字串中的bb替換成haha

翻轉  reverse  ()

               select reverse(‘abc‘); 結果是 cba

 

四 轉換函式                                                              -- 在資料庫中 +  : 字串 拼接 , 實值型別 數學運算

1 convert ( A , B )                   -- -- A 要轉換成的類型   B 列/值

      convert ( nvarchar(10), oil )

2  cast( A  as  B )                 -- -- A列/值    B 要轉換成的類型  

      cast ( oil  as  nvarchar (10) )

 

五.時間日期函數

擷取當前系統時間    GetDate( )                   ----sysdatetime() 擷取資料庫服務的時間戳記                                  select getdate()

擷取年月日   year()

                  month()                              -- 沒有時 分 秒                           

                  day()

                                 select year(‘1999-1-1‘)

                                 select month(GetDate( ) )

判斷日期是否正確  isdate ()                                         select isdate(‘2000-2-31‘)           ---- 返回bit類型,false是0,true是1

添加時間   dateadd( A , B , D )                             -- --A 添加什麼類型(year,month,day)   B 加多少   C 給誰加

                                 select dateadd(year,5,‘2000-1-1‘)

返回星期幾   datename(  weekday, 時間     )            --  返回的值是字串     datepart 一樣可以返回周幾,但是返回的是int類型

                                   select datename(weekday,‘2000-1-1‘)

          datename(week,時間)       --第幾周

          datename(day,時間)          --這個月的第幾天

          datename(dayofyear,時間)--這年的第幾天

 

SQL Server T—SQL 函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.