SQL函數大全

SQL函數大全

--聚合函數
use pubs
go
select avg(distinct price)? --算平均數
from titles
where type='business'
go
use pubs
go
select max(ytd_sales)? --最大數
from titles
go

use pubs
go
select min(ytd_sales) --最小數
from titles
go

use pubs
go
select type,sum(price),sum(advance)? --求和
from titles
group by type
order by type
go

use pubs
go
select count(distinct city)? --求個數
from authors
go

use pubs
go
select stdev(royalty) --返回給定表達式中所有值的統計標準偏差
from titles
go

use pubs
go
select stdevp(royalty) --返回表達式中所有制的填充統計標準偏差
from titles
go

use pubs
go
select var(royalty) --返回所有值的統計方差
from titles
go

use pubs
go
select varp(royalty) --返回所有值的填充的統計方差
from titles
go

--數學函數

select sin(23.45),atan(1.234),rand(),PI(),sign(-2.34) --其中rand是獲得一個隨機數
--配置函數
SELECT @@VERSION --獲取當前數據庫版本
SELECT @@LANGUAGE --當前語言
--時間函數
select getdate() as 'wawa_getdate' --當前時間
select getutcdate() as 'wawa_getutcdate' --獲取utc時間
select day(getdate()) as 'wawa_day' --取出天
select month(getdate()) as 'wawa_month' --取出月
select year(getdate()) as 'wawa_year' --取出年
select dateadd(d,3,getdate()) as wawa_dateadd --加三天,注意'd'表示天,'m'表示月,'yy'表示年,下面一樣
select datediff(d,'2004-07-01','2004-07-15') as wawa_datediff --計算兩個時間的差
select datename(d,'2004-07-15') as wawa_datename --取出時間的某一部分
select datepart(d,getdate()) as wawa_datepart? --取出時間的某一部分,和上面的那個差不多
--字符串函數
select ascii(123) as '123',ascii('123') as '"123"',ascii('abc') as '"abc"' --轉換成ascii碼
select char(123),char(321),char(-123) --根據ascii轉換成字符
select lower('ABC'),lower('Abc'),upper('Abc'),upper('abc') --轉換大小寫
select str(123.45,6,1), str(123.45,2,2) --把數值轉換成字符串
select ltrim('??? "左邊沒有空格"')? --去空格
select rtrim('"右邊沒有空格"???? ') --去空格
select ltrim(rtrim('?? "左右都沒有空格"??? ')) --去空格
select left('sql server',3),right('sql server',6) --取左或者取右

use pubs
select au_lname,substring(au_fname,1,1) --取子串
from authors
order by au_lname

select charindex('123','abc123def',2) --返回字符串中指定表達式的起始位置
select patindex('123','abc123def'),patindex('%123%','abc123def') --返回表達式中某模式第一次出現的起始位置
select quotename('abc','{'),quotename('abc') --返回由指定字符擴住的字符串
select reverse('abc'),reverse('上海') --顛倒字符串順序
select replace('abcdefghicde','cde','xxxx') --返回唄替換了指定子串的字符串
select space(5),space(-2)

--系統函數
select host_name() as 'host_name',host_id() as 'host_id',user_name() as 'user_name',user_id() as 'user_id',db_name() as 'db_name'
--變量的定義使用
--聲明局部變量
declare @mycounter int
declare @last_name varchar(30),@fname varchar(20),@state varchar(2) --一下聲明多個變量
--給變量賦值
use northwind
go
declare @firstnamevariable varchar(20),
?@regionvariable varchar(30)
set @firstnamevariable='anne' --可以用set,也可以用select給變量賦值,微軟推薦用set,但select在選擇一個值直接賦值時很有用
set @regionvariable ='wa'

select lastname,firstname,title? --用聲明并賦值過的變量構建一個Select語句并查詢
from employees
where firstname= @firstnamevariable or region=@regionvariable
go
--全局變量
select @@version? --返回數據庫版本
select @@error? --返回最后的一次腳本錯誤
select @@identity? --返回最后的一個自動增長列的id

--while,break,continue的使用
--首先計算所有數的平均價格,如果低于30的話進入循環讓所有的price翻倍,
--里面又有個if來判斷如果最大的單價還大于50的話,退出循環,否則繼續循環,知道最大單價大于50就break出循環,呵呵,
--我分析的應該對吧.
use pubs
go
while (select avg(price) from titles) <$30
begin
?update titles
? set price=price*2
? select max(price) from titles
? if(select max(price) from titles) >$50
? break
? else
? continue
end
print 'too much for the marker to bear'

--事務編程經典例子
--begin transaction是開始事務,commit transaction是提交事務,rollback transaction是回滾事務
--這個例子是先插入一條記錄,如果出現錯誤的話就回滾事務,也就是取消,并直接return(返回),如果沒錯的話就commit 提交這個事務了哦
--上面的那個return返回可以返回一個整數值,如果這個值是0的話就是執行的時候沒出錯,如果出錯了就是一個負數,
--這個return也可以用在存儲過程中,可用用 exec @return_status= pro_name來獲取這個值
use pubs
go
begin tran mytran
?insert into stores(stor_id,stor_name)
? values('333','my books')
?go
?insert into discounts(discounttype,stor_id,discount)
? values('清倉甩賣','9999',50.00)
?if @@error<>0
? begin
?? rollback tran mytran
?? print '插入打折記錄出錯'
?? return
? end
commit tran mytran

--事務處理的保存點示例
--做了事務保存點后可以rollback(回滾)到指定的保存點,不至于所有的操作都不能用
use pubs
go
select * from stores
begin transaction testsavetran
?insert into stores(stor_id,stor_name)
? values('1234','W.Z.D Book')
?save transaction before_insert_data2
?go
?insert into stores(stor_id,stor_name)
? values('5678','foreat Books')
?go
rollback transaction before_insert_data2
select * from stores

--存儲存儲過程
use pubs
if exists(select name from sysobjects where name= 'proc_calculate_taxes' and type='P')
?drop procedure proc_calculate_taxes
go
create procedure proc_calculate_taxes (@p1 smallint=42,@p2 char(1),@p3 varchar(8)='char')
as
select *
from titles
--執行過程
EXECUTE PROC_CALCULATE_TAXES @P2='A'

?

本文轉載自CSDN博客http://blog.csdn.net/xymyeah/archive/2008/06/05/2514592.aspx

轉載于:https://www.cnblogs.com/hhl-kf/archive/2013/01/24/SQL.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/274370.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/274370.shtml
英文地址,請注明出處:http://en.pswp.cn/news/274370.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

時間軸ui設計_我應該在UI設計上花更多時間嗎?

時間軸ui設計Let’s start with an example of communication skills: they are important for any profession, and you expect any professional to have a decent level. However, excellent communication skills won’t make up for the lack of core expertise. Imagine …

一、Oracle介紹

Oracle學習筆記 一、 Oracle介紹 選擇數據庫的標準 項目的規模 負載量多大&#xff0c;用戶量多少 成本 安全性 Oracle 認證 初級&#xff1a;OCA&#xff1a;Oracle Certificated Associate 中級&#xff1a;OCP&#xff1a;Oracle Certificated Professional 高級&#xff…

移動端分步注冊_移動應用程序的可用性測試:分步指南

移動端分步注冊Written by Justin Mifsud由賈斯汀米夫蘇德 ( Justin Mifsud)撰寫 The mobile market is huge and growing at a very fast rate. With an estimated 4.5 billion subscribers worldwide, it is forecasted that the number of mobile phones will surpass the …

ldd隨筆(1)-linux設備模型

一下只是個人學習后的理解&#xff0c;可能有很多不對的地方。 要學習linux的設備驅動模型&#xff0c;首先必須要知道kobject和kset的概念&#xff0c;下面是kobject在2.6.38的源碼中的實現。 struct kobject {const char *name; //名稱&#xff0c;可能在sysfs中創…

插圖 引用 同一行兩個插圖_提出食物主題中的插圖

插圖 引用 同一行兩個插圖I have a page in my portfolio, which is about search functionality. I wanted that page to feel fun and engaging, to convey a positive vibe, so I decided to add illustrations to it.我的投資組合中有一個頁面與搜索功能有關。 我希望該頁面…

Hadoop的SequenceFile讀寫實例

1 SequenceFile可以處理hdfs上大量小文件&#xff0c;它可以作為大量小文件的容器。HDFS和MapReduce是針對大文件優化的&#xff0c;所以通過SequenceFile類型將小文件包裝起來可以獲得更高效的存儲和處理。存儲2 在SequenceFile中的鍵和值并不一定是Writable類型&#xff…

臉部細微表情識別_您可以僅使用面部表情來控制字體嗎?

臉部細微表情識別原型 (The prototype) Facetype is the name of Adam’s interactive project, in which the emotions detected from a person’s facial gestures control a variable font. To each detected emotion corresponds a specific typeface, which keeps transfo…

ssky-keygen + ssh-copy-id 無密碼登陸遠程LINUX主機

使用下例中ssky-keygen和ssh-copy-id&#xff0c;僅需通過3個步驟的簡單設置而無需輸入密碼就能登錄遠程Linux主機。 ssh-keygen 創建公鑰和密鑰。 ssh-copy-id 把本地主機的公鑰復制到遠程主機的authorized_keys文件上。ssh-copy-id 也會給遠程主機的用戶主目錄&#xff08;ho…

uva10891Game of sum

題意:經典的取石子游戲是這樣的:有一堆石子&#xff0c;A、B兩個人輪流取&#xff0c;每次取一顆&#xff0c;只能從邊上取&#xff0c;每個石子有相應的價值&#xff0c;A、B兩人都想使得自己的價值最多&#xff0c;兩個人足夠聰明&#xff0c;問最后價值分別是多少 本題則是可…

用戶體驗設計師能為seo做_用戶體驗設計師可以從產品設計歷史中學到什么

用戶體驗設計師能為seo做Many things have changed from tool design in the prehistoric era to today’s digital product design. However, we can see surprisingly many similarities. Especially when it comes down to one particular aspect: usability.從史前時代的工…

函數指針

顧名思義&#xff0c;指針函數即返回指針的函數。其一般定義形式如下&#xff1a; 類型名 *函數名(函數參數表列); 其中&#xff0c;后綴運算符括號“()”表示這是一個函數&#xff0c;其前綴運算符星號“*”表示此函數為指針型函數&#xff0c;其函數值為指針&#xff0c;即它…

orton效果_如何使圖片發光:Orton效果

orton效果Have you ever seen an impossibly dream-like landscape photo? One with a slow burning, glowing sunset. That’s really the best way to describe it, the image looks as if it’s glowing. You might be thinking, “wow, I wish I was that good and could …

UVA10785 The Mad Numerologist

雖然是sorting的壓軸&#xff0c;但是比起前面真心水題。這個專題結合前面string的很多&#xff0c;排序相對簡單了&#xff0c;qsort基本解決。 題目&#xff1a; The Mad Numerologist Numerology is a science that is used by many people to find out a mans personality,…

蘋果人機交互指南_蘋果人機界面設計指南的10個見解

蘋果人機交互指南重點 (Top highlight)I’ve been developing an IOS app for the past few months and have been constantly referring to Apple’s Human Interface Design Guidelines. I would consider it a must-read for any aspiring or current UI/UX designer.在過去…

也來學學插件式開發

上一家公司有用到插件式開發來做一個工具箱&#xff0c;類似于QQ電腦管家&#xff0c;有很多工具列表&#xff0c;點一下工具下載后就可以開始使用了。可惜在那家公司待的時候有點短&#xff0c;沒有好好研究一下。現在有空&#xff0c;自己在網上找了些資料&#xff0c;也來試…

同態加法_我對同態的想法

同態加法Early February, I uploaded this shot onto Dribbble. Nothing fancy –– just two screens experimenting with “2月初&#xff0c;我將這張照片上傳到Dribbble。 沒什么幻想–只有兩個屏幕在嘗試“ Neumorphism,” or soft UI. Little did I know that this post…

php內核探索

引自&#xff1a;http://www.nowamagic.net/librarys/veda/detail/1285 SAPI:Server Application Programming Interface 服務器端應用編程端口。研究過PHP架構的同學應該知道這個東東的重要性&#xff0c;它提供了一個接口&#xff0c;使得PHP可以和其他應用進行交互數據。 本…

hp-ux鎖定用戶密碼_UX設計101:用戶研究-入門需要了解的一切

hp-ux鎖定用戶密碼這是什么&#xff1f; (What is this?) This session is part of a learning curriculum that I designed to incrementally skill up and empower a team of Designers and Researchers whose skillset and ways of working needed to evolve to keep up wi…

等比數列前N項和的公式推導

設等比數列的前n項和為S(n), 等比數列的第一項為a1&#xff0c;比值為q。 &#xff08;1&#xff09;S(n) a1 a1 * q a1 * q ^ 2 .... a1 * q ^ (n - 1);&#xff08;2&#xff09;S(n1) a1 a1 * q a1 * q ^ 2 .... a1 * q ^ (n - 1) a1 * q ^ n;由&#xff08;2&am…

extjs6 引入ux_關于UX以及如何擺脫UX的6種常見誤解

extjs6 引入uxDo you ever browse social media, internet, or talk to colleagues and hear them say something UX related you disagree with so much that you just want to lecture them on the spot?您是否曾經瀏覽過社交媒體&#xff0c;互聯網或與同事交談&#xff0c…