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.mzph.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…