聚合函数: 1.avg 返回组中的平均,空将被忽略。 例如:use northwind // 操作northwind数据库 go select avg (unitprice) //从表中选择求unitprice的平均 from products where categoryid = ‘8’ 2.binaby_checksum 可用于检测表中行的更改返回由表达式
聚合函数:
1.avg 返回组中的平均值,空值将被忽略。
例如:use northwind // 操作northwind数据库
go
select avg (unitprice) //从表中选择求unitprice的平均值
from products
where categoryid = ‘8’
2.binaby_checksum 可用于检测表中行的更改返回值由表达式的运算结果类型决定
例如:use northwind
go
create table tablebc(productid int,bchecksum int) //建立有两个属性的表
insert into tablebc //向表中插入数据
select priductid,binary_checksum(*)
from products
update products //更新
set productname=’oishi tofu’, unitprice=20 where productname = ‘tofu’
update products //更新
set priductname=’oishi konbu’, unitprice = 5 where priductname=’konbu’
update priducts //更新
set prodctname=’oishi genen shouyu’, unitprice =12
where priductname=’genen shouyu’
select productid //挑出变化的行
from tablebc
where exists (
select productid from products
where product.productid = tablebc.productid and
binary_checksu (*) tablebc.bchecksum) //标志出变化的行
3.checksum 返回在表的行上或在表达式上计算的校验值 checksum 用于生成哈希索引
例如:
set arithabort on
se northwind
go
alter table products
add cs_pname as checksum(productname) //在参数列是添加一个校验值
create imdex pname_index on products(cs_pname) //生成索引
go
select top 5 cs_pname from products order by cs_pname desc //选择根据索引列的前5 个cs_pname
4.checksum_agg 返回组中值的校验值。空值冽被忽略。
例如:
use northwind
go
select checksum_agg(cast(unitsinstock as int)) //检测products 表的unitsinstock列的更改
from products
5.count 返回组中项目的数量
例如:
例一:
use pubs
go
select count(distinct city) //对city中的每一行都计算,并返回非空的数量
from authors
go
例二:
use pubs
go
select count(*) // 返回组中项目的数量
from titles
go
例三:
use pubs
go
select count(*),avg(price) // 选择advance大于$1000的总数和平均price
from titles
where advance >$1000
go
6.count_big 返回组中项目的数量。在使用上和count 基本上是一样的,只是在返回值上有一点区别,count_big 返回是bigint的数据类型值,count返回的是int数据类型值。
7.grouping 返回一个聚合函数,它产生一个附加列,当用cube或rollup运算符添加行时,附加 的列输出值为1,当所添加的行不是由cube或rollup产生时,附加列值为0
例如:
use pubs
go
select royalty,sum(advance) ‘total advance’, //选择列royalty,聚合 advance数值
grouping(royalty) ‘grp’ //grouping 函数
from titles
group by royalty with rollup //group by 与 rollup 相联系产生分组
8.max 返回表达式的最大值
例如:
use pubs
go
select max(ytd_sales) //选择ytd_sales列属性,求其最大值。
from titles
go
9.min 返回表达式的最小值
例如:
use pubs
go
select min(ytd_sales) //选择ytd_sales列属性,求其最小值。
from titles
go
10.stdev 返回给定表达式中所有值的统计标准偏差。
例如:
use pubs
select stdev(royalty)
from titles
11.stdevp 返回给定表达式中所有值的填充统计标准偏差。
例如:
use pubs
select stdev(royalty)
from titles
12.sum 返回表达式中所有值的的和,或只返回distinct值。sum只能用数字列。
例如:
use pubs
go
select type,sum(price),sum(advance) //选择type,price的和,advance的和
from titles
where type like ‘%cook’ //匹配结尾字符为cook
group by type
order by type
go
例如2:
use pubs
go
select type,price,advance
from titles
where type like’%cook’
order by type
compute sum(price),sum(advance) by type //根据type的属性计算price和advance
的和
13.var 返回给定表达式中所有值的统计方差。
例如:
use pubs
go
selecdt var(royalty)
from titles
14.varp 返回给定表达式中所有值的填充的统计方差
例如:
use pubs
go
select varp(royalty)
from titles
时间及日期函数
1.dateadd 在向指定日期加上一段时间的基础上返回新datetime值。
例如:
use northwind
go
select dateadd(day,3,hiredate) //显示函数dateadd执行结果
from employees
2.datediff 返回跨两个指定日期和时间边界数。
例如:
use northwind
go
select datediff(day,hiredate,getdate()) as no_of_days
from employees
go
3.datename 返回代表指定日期的指定日期部分的字符串。
例如:
select datename(month,getdate()) as ‘monthname’
4.datepart 返回代表指定日期的指定日期部分的整数。
例如:
select datepart(month,getdate()) as ‘monthnumber’
select datepart(m,0),datepart(d,0),datepart(yy,0)
5.day month year 返回指定日期的天 月 年 的日期部分的整数
例如:
select month(0),day(0),year(0)
6.getdate 按datetime值的标准内部格式返回当前系统时间和日期
例如:
use northwind
go
create table sales //创建sales表
(
sale_id char(11) not null //sale_id列,类型char(11),not null
sale_name varchar(40) not null //sale_name列,类型varchar(40),not null
sale_date datetime defaull getdate() // sale_date列,类型datetime,默认值getdate()
)
insert into sales (sale_id,sale_name)
values(1,’foods’) //向表内插值
select * from sales //表结果检索
7.getutcdate 返回表当前cut时间的datetime值。
例如:
select getutcdatea()
数学函数
1.abs 返回给定数字的绝对值
2.acos 返回以弧度表示的角度值,该角度值的余弦为给定的float表达式,也叫反余弦
3.asin 返回以弧度表示的角度值,也叫反正弦。
例如:
declare @angle float
set @angle = -1.01
select ‘the asin of the angke is : ’ + convert (varchar,asin(@angle))
4.atan 返回以弧度表示的角度值,该角度值的正切为给定的float表达式,也叫反正切。
5.atn2 返回以弧度表示的角度值,该角度值的正切介于两个给定的float表达式之间
例如:
declare @anglel float
declare @angle2 float
set @anglel = 35.175643
set @angle2 =129.44
select ‘the atn2 of the angle is : ’ + convert (varchar,atn2(@anglel,@angle2))
go
6.ceiling 返回或等于所给数字表达式的最小整数。
例如:
select ceiling($123.45),ceiling($-123.45),ceiling($0.0)
7.cos 返回给定表达式中给定角度的三角余弦值
8.cot 返回给定float表达式指定角度的三角余切值
例如:
declare @angle float
set @angle = 124.1332
select ‘the cot fo the angle is :’ + convert(varchar,cot(@angle))
9.degrees 当给出弧度为单位的角度时,返回相应的以度数为单位的角度。
例如:
select ‘the number of degrees in pi/2 radinans is :’ +convert(varchar,degrees((pi()/2)))
10.exp 返回所给的float表达式的指数值。
11.floor 返回小于或等于所给数字表达式的最大整数。
12.log 返回给定float表达式的自然对数。
13.log10 返回给定float表达式的以10为底的对数。
例如:
declare @var float
set @var = 5.175643
select ‘the log of the variable is :’ +convert (varchar,log(@var))
14.pi 返回pi的常量值。
15.power 返回给定表达式乘指定次方的值。
例如:
declare @value int,@counter int
set @value = 2
set @counter = 1
while @counter begin
select power(@value,@counter)
set nocount on
set @counter=@counter +1
end
go
16.radians 对于数字表达式中输入的度数值返回弧度值。
17.rand 返回0到1之间的随机float值。
18.round 返回数字表达式并四舍五入为指定的长度或精度。
例如:
select round(123.9995,3),round(123.9994,3)
19.sign 返回给定表达式的正 零 或负号
例如:
declare @angle gloat
declare @value real
set @value=-1
while @value begin
select sign(@value)
set nocount on
select @value=value+1
end
set nocount off
20.sin 以近似数字表达式返回给定角度的三角正弦值。
21.sqrt 返回给定表达式的平方根。
例如:
declare @myvalue float
set @myvalue = 1.00
while @myvlaue begin
select sqrt(@myvalue)
select @myvalue = @myvalue+1
end
22.square 返回给定表达式的平方值。
23.tan 返回给定表达式的正切。
元数据函数
1.col_length 返回列的定义长度
例如:
use northwind
go
create table t1
( c1 varchar(40),
c2 nvarchar(80) )
go
select col_length(‘t1’,’c1’) as ‘varchar’
select col_length(‘t2’,’c2’) as ‘nvarchar’
go
drop table t1
2.col_name 返回数据库列的名称,该列具有相应的表标识号和列标识号。
例如:
use northwind
go
set nocount off
select col_name(object_id(‘employees’),1) as employees
3.columnproperty 返回有关列或过程的参数的信息。
例如:
use northwind
go
select columnproperty (object_id(‘employees’),’title’,’precision’)
4.databaseproperty 返回给定数据库和属性名的命名数据库属性值。
例如:
use northwind
go
select databaseproperty(‘northwind’,’isautoclose’)
5.databasepropertyex 返回指定数据库的指定数据库选项或属性的当前设置。
例如:
use northwind
go
select databasepropertyex(‘northwind’,isautoclose’)
6.db_id 返回数据库标识id
例如:
select name,db_id(name) as db_id
from sysdatabases
order by dbid
7.db_name 返回数据库名称。
8.file_id 返回当前数据库中给定逻辑文件标识(id)号。
9.file_name 返回指定文件标识(id)号的逻辑文件名。
10.filegroup_id 返回给定文件组名称号
11.filegroup_name 返回给定文件组标识(id)号的文件组名。
12.filegroupproperty 给定文件组和属性名时,返回指定的文件组属性值。
13.fileproperty 给定文件名和属性时返回指定的文件名属性值。
14.fn_listextendedproperty 返回数据库对像的扩展属性值。
例如:
use northwind
go
create table t1 (id int, name char(20)) //创建表t1
exec sp_addextendedproperty ‘caption’,’employee id’,’user’,dbo,’table’,’t1’,
’column’,id //为表t1列id添加扩展属性
exec sp_addextendedproperty ‘caption’,’employee name’,’user’, dbo, ‘table’,
‘t1’,’column’,name
select * from ::fn_listextendedproperty (null,’suer’,’dbo’,table’,t1’,’column’,
default) //列举表t1的扩展属性
15.fulltextserviceproperty 返回有关全文服务级别属性的信息。
原型:fulltextserviceproperty (catalog_name,property)
参数说明:
catalog_name 包含全文目录名称的表达式。
property 包含全文目录属性名称的表达式。
property 参数值列表
populatestatus 0 = 空闲 1 = 正在进行完全填充 2 = 已暂停
3 = 中止 4 = 正在恢复 5 = 关机
6 = 正在进行增量填充 7 = 生成索引
8 = 磁盘已满,已暂停 9 = 更改跟踪
例如:
use northwind
go
select fulltextcatalogproperty(‘cat_desc’,’itemcount”)
16.fulltextserviceproperty 返回有关全文服务级别属性的信息。
原型:fulltextserviceproperty(property)
property 参数说明
属性
描述
resourceusage 一个从 1(后台)到 5(专用)之间的值。
connecttimeout 在超时发生前,microsoft 搜索服务等待所有与 microsoft? sql sever? 数据库服务器的连接完成以便进行全文索引填充所用的时间(以秒为单位)。
isfulltextinstalled 在 sql server 的当前实例中安装全文组件。1 = 已安装全文组件。 0 = 未安装全文组件。 null = 输入无效或发生错误。
datatimeout 在超时发生前,microsoft 搜索服务等待所有由 microsoft sql server 数据库服务器返回数据以便进行全文索引填充所用的时间(以秒为单位)。
例如:
use northwind
go
select fulltextserviceproperty(‘isfulltextinstalled’)
17. index_col 返回索引列名称。
原型:index_col(‘table’,’index_id’,’key_id’)
参数:table 表的名称。
index_id 索引的id
key_id 键的id
例如:
use northwind
go
declare @id int, @type char(2), @msg varchar(10), @indid smallint,
@indname sysname, @status int //声明变量
set nocount on
select @id=id,@type=type //获得employees表的id号以便在系统索引库中
查找其索引
from sysobjects
where name=’employees’ and type=’u’
print ‘index information for the authors table’ //打印输出信息
print ‘----------------------------------------------’
declare i cursor //声明一个游标
for
select indid, name, status //循环搜索employees 表中所有的索引
from sysindexes
where id=@id
open i //打开游标
fetch next from i into @indid, @indname, @status //获取下一系列索引信息
if @@fetch_status = 0 //如果状态为0,打印‘ ’
print ‘ ‘
while @@fetch_status = 0 //循环如果从游标处查找还有行,打印出相关
索引信息
begin
set @msg = null //初始化变量msg为null
set @msg = ‘ index number ‘ + convert(varchar,@indid)+
‘is’ + @indname //填充索引名变量
set @indkey = 1 //初始化变量indkey为1
while @indkey@indkey) // indkey等于key_id,其值可以从1到16
is not null
begin
if @indkey = 1 //打印不同的信息当indkey不等于1和等于1时
set @msg = msg + ‘, ‘+ index_col(@name,@indid,@indkey)
set @indkey = @indkey + 1 //indkey递增
end
print @msg //打印信息
set @msg = null
fetch next from i into @indid,@indname,@status //循环下一条
end
close i
deallocate i
set nocount off
18. indexkey_property 返回有关索引键的信息
原型:
indexkey_property (table_id,index_id,key_id,property)
参数说明:
table_id 表标识号
index_id 索引标识号
indkey_id 索引列的位置
property 属性的名称,将要为该属性返回信息。
propert 的属性参数:
columnid 索引的key_id位置上的列id
isdescending 存储索引列的顺序。1=降序,0=升序
例如:
use northwind
go
select indexkey_property(object_id(‘employees’,1,1,’columnid’)
19. indexproperty 在给定表标识号、索引名称及属性的前提下,返回指定的索引属性值
原型:
indexproperty (table_id,index,property)
参数说明:
table_id 是包含要为其提供索引属性信息的表或索引视图标识号的表达
式。table_id的数据类型为int
index 一个包含索引的名称的表达式,将为该索引返回属性信息。
property 一个表达式,它包含将要返回的数据库属性的名称。
property属性的参数: 属性
描述
indexdepth 索引的深度。 返回索引所具有的级别数。
indexfillfactor 索引指定自己的填充因子。 返回创建索引或最后重建索引时使用的填充因子。
indexid 指定表或索引视图上的索引的索引 id。
isautostatistics 索引是由 sp_dboption 的 auto create statistics 选项生成的。 1 = true 0 = false null = 无效的输入
isclustered 索引是聚集的。 1 = true 0 = false null = 无效的输入
isfulltextkey 索引是表的全文键。 1 = true 0 = false null = 无效的输入
ishypothetical 索引是假设的,不能直接用作数据访问路径。假设的索引保留列级统计。 1 = true 0 = false null = 无效的输入
ispadindex 索引在每个内部节点上指定将要保持空闲的空间。 1 = true 0 = false null = 无效的输入
ispagelockdisallowed 1 = 通过 sp_indexoption 禁用页锁定。 0 = 允许页锁定。 null = 无效的输入
isrowlockdisallowed 1 = 通过 sp_indexoption 禁用行锁定。 0 = 允许行锁定。 null = 无效的输入。
isstatistics 索引是由 create statistics 语句或由 sp_dboption 的 auto create statistics 选项创建的。对于列级统计,统计索引将用作占位符。 1 = true 0 = false null = 无效的输入
isunique 索引是唯一的。 1 = true 0 = false null = 无效的输入
例如:
use northwind
go
select indexproperty(object_id(‘categories’),’pk_categories’,’ispadindex’)
20. object_id 返回数据库对象标识号。
原型:
object_id(‘object’)
例如:
use master
go
select object_id(‘northwind..employees’)
21. object_name 返回数据库对象名。
原型:
object_name(object_id)
例如:
use northwind
go
select table_catalog,table_name
from information_schema.tables
where table_name = object_name(111770580711)
22. objectproperty 返回当前数据库中对象的有关信息。
原型:
objectproperty(id,property)
参数说明:
id 一个表达式,包含当前数据库中某一个对象的id。id的数据类型为int。
property 一个表达式,包含针对由id指定的对象将要返回的信息。
property 属性值参数说明:
属性名称
对象类型
描述和返回的值
cnstisclustkey 约束 带有聚集索引的主键。 1 = true 0 = false
cnstiscolumn 约束 column 约束。 1 = true 0 = false
cnstisdeletecascade 约束 带有 on delete cascade 选项的外键约束。
cnstisdisabled 约束 禁用的约束。 1 = true 0 = false
cnstisnonclustkey 约束 带有非聚集索引的主键。 1 = true 0 = false
cnstisnottrusted 约束 启用约束时未检查现有行,所以可能不是所有行都受约束的控制。 1 = true 0 = false
cnstisnotrepl 约束 使用 not for replication 关键字定义约束。
cnstisupdatecascade 约束 带有 on update cascade 选项的外键约束。
execisaftertrigger 触发器 after 触发器。
execisansinullson 过程、触发器、视图 创建时的 ansi_nulls 设置。 1 = true 0 = false
execisdeletetrigger 触发器 delete 触发器。 1 = true 0 = false
execisfirstdeletetrigger 触发器 对表执行 delete 时触发的第一个触发器。
execisfirstinserttrigger 触发器 对表执行 insert 时触发的第一个触发器。
execisfirstupdatetrigger 触发器 对表执行 update 时触发的第一个触发器。
execisinserttrigger 触发器 insert 触发器。 1 = true 0 = false
execisinsteadoftrigger 触发器 instead of 触发器。
execislastdeletetrigger 触发器 对表执行 delete 时触发的最后一个触发器。
execislastinserttrigger 触发器 对表执行 insert 时触发的最后一个触发器。
execislastupdatetrigger 触发器 对表执行 update 时触发的最后一个触发器。
execisquotedidenton 过程、触发器、视图 创建时的 quoted_identifier 设置。 1 = true 0 = false
execisstartup 过程 启动过程。 1 = true 0 = false
execistriggerdisabled 触发器 禁用的触发器。 1 = true 0 = false
execisupdatetrigger 触发器 update 触发器。 1 = true 0 = false
hasaftertrigger 表,视图 表或视图具有 after 触发器。 1 = true 0 = false
hasinserttrigger 表,视图 表或视图具有 insert 触发器。 1 = true 0 = false
hasinsteadoftrigger 表、视图 表或视图具有 instead of 触发器。 1 = true 0 = false
hasupdatetrigger 表、视图 表或视图具有 update 触发器。 1 = true 0 = false
isansinullson 函数、过程、表、触发器、视图 指定表的 ansi nulls 选项设置为 on,表示所有与空值的比较都取值为 unknown。只要表存在,该设置就应用于表定义中的所有表达式,包括计算列和约束。 1 = on 0 = off
ischeckcnst 任何 check 约束。 1 = true 0 = false
isconstraint 任何 约束。 1 = true 0 = false
isdefault 任何 绑定的默认值。 1 = true 0 = false
isdefaultcnst 任何 default 约束。 1 = true 0 = false
isdeterministic 函数、视图 函数的确定性属性。只适用于标量值及表值函数。 1 = 可确定的 0 = 不可确定的 null = 不是标量值或表值函数,或者是无效的对象 id。
isexecuted 任何 指定执行该对象的方式(视图、过程或触发器)。 1 = true 0 = false
isextendedproc 任何 扩展过程。 1 = true 0 = false
isforeignkey 任何 foreign key 约束。 1 = true 0 = false
isindexed 表、视图 带有索引的表或视图。
isindexable 表、视图 可以创建索引的表或视图。
isinlinefunction 函数 内嵌函数。 1 = 内嵌函数 0 = 非内嵌函数 null = 不是函数,或者是无效的对象 id。
ismsshipped 任何 在安装 microsoft? sql server? 2000 的过程中创建的对象。 1 = true 0 = false
isprimarykey 任何 primary key 约束。 1 = true 0 = false
isprocedure 任何 过程。 1 = true 0 = false
isquotedidenton 函数、过程、表、触发器、视图 指定表的被引用标识符设置为 on,表示在表定义所涉及的所有表达式中,双引号标记分隔标识符。 1 = on 0 = off
isreplproc 任何 复制过程。 1 = true 0 = false
isrule 任何 绑定的规则。 1 = true 0 = false
isscalarfunction 函数 标量值函数。 1 = 标量值 0 = 表值 null = 不是函数,或者是无效的对象 id。
isschemabound 函数,视图 使用 schemabinding 创建的架构绑定函数或视图。 1 = 架构绑定 0 = 非架构绑定 null = 不是函数或视图,或者是无效的对象 id。
issystemtable 表 系统表。 1 = true 0 = false
istable 表 表。 1 = true 0 = false
istablefunction 函数 表值函数。 1 = 表值 0 = 标量值 null = 不是函数,或者是无效的对象 id。
istrigger 任何 触发器。 1 = true 0 = false
isuniquecnst 任何 unique 约束。 1 = true 0 = false
isusertable 表 用户定义的表。 1 = true 0 = false
isview 视图 视图。 1 = true 0 = false
ownerid 任何 对象的所有者。 nonnull = 对象所有者的数据库用户 id。 null = 无效的输入。
tabledeletetrigger 表 表有 delete 触发器。 >1 = 给定类型的第一个触发器的 id。
tabledeletetriggercount 表 表具有指定数目的 delete 触发器。 >1 = 给定类型的第一个触发器的 id。 null = 无效的输入。
tablefulltextbackgroundupdateindexon 表 表已启用全文后台更新索引。 1 = true 0 = false
tablefulltextcatalogid 表 表的全文索引数据所驻留的全文目录的 id。 nonzero = 全文目录 id,它与标识全文索引表中行的唯一索引相关。 0 = 表不是全文索引的。
tablefulltextchangetrackingon 表 表已启用全文更改跟踪。 1 = true 0 = false
tablefulltextkeycolumn 表 与某个单列唯一索引相关联的列 id,这个单列唯一索引参与全文索引定义。 0 = 表不是全文索引的。
tablefulltextpopulatestatus 表 0 = 不填充 1 = 完全填充 2 = 增量填充
tablehasactivefulltextindex 表 表具有一个活动的全文索引。 1 = true 0 = false
tablehascheckcnst 表 表具有 check 约束。 1 = true 0 = false
tablehasclustindex 表 表具有聚集索引。 1 = true 0 = false
tablehasdefaultcnst 表 表具有 default 约束。 1 = true 0 = false
tablehasdeletetrigger 表 表具有 delete 触发器。 1 = true 0 = false
tablehasf, , , oreignkey 表 表具有 foreign key 约束。 1 = true 0 = false
tablehasforeignref 表 表由 foreign key 约束引用。 1 = true 0 = false
tablehasidentity 表 表具有标识列。 1 = true 0 = false
tablehasindex 表 表具有一个任何类型的索引。 1 = true 0 = false
tablehasinserttrigger 表 对象具有 insert 触发器。 1 = true 0 = false null = 无效的输入。
tablehasnonclustindex 表 表具有非聚集索引。 1 = true 0 = false
tablehasprimarykey 表 表具有主键。 1 = true 0 = false
tablehasrowguidcol 表 对于 uniqueidentifier 列,表具有 rowguidcol。 1 = true 0 = false
tablehastextimage 表 表具有 text 列。 1 = true 0 = false
tablehastimestamp 表 表具有 timestamp 列。 1 = true 0 = false
tablehasuniquecnst 表 表具有 unique 约束。 1 = true 0 = false
tablehasupdatetrigger 表 对象具有 update 触发器。 1 = true 0 = false
tableinserttrigger 表 表具有 insert 触发器。 >1 = 给定类型的第一个触发器的 id。
tableinserttriggercount 表 表具有指定数目的 insert 触发器。 >1 = 给定类型的第一个触发器的 id。
tableisfake 表 表不是真实的。根据需要 sql server 对其进行内部具体化。 1 = true 0 = false
tableispinned 表 驻留表以将其保留在数据高速缓存中。 1 = true 0 = false
tabletextinrowlimit 表 text in row 所允许的最大字节数,如果没有设置 text in row 选项则为 0。
tableupdatetrigger 表 表具有 update 触发器。 >1 = 给定类型的第一个触发器的 id。
tableupdatetriggercount 表 表具有指定数目的 update 触发器。 >1 = 给定类型的第一个触发器的 id。
例如:
select objectproperty(object_id(‘employees’),’tabletextinrowlimit’)
23. @@procid 返回当前过程的存储过程标识符(id)。
例如:
create procedure testprocedure as //创建存储过程testprocedure
select @@procid as ‘procid’ //列出存储的id
go
exec testprocedure //调用存储过程
go
24. sql_variant_property 返回有关sql_variant值的基本数据类型的其他信息。
原型:
sql_variant_property (expression,property)
参数说明:
expression 是sql_variant类型的表达式。
property 包含将为其提供信息的sql_variant属性名称。
property的参数说明: 值
描述
返回的 sql_variant 基本类型
basetype sql server 数据类型,如: char int money nchar ntext numeric nvarchar real smalldatetime smallint smallmoney text timestamp tinyint uniqueidentifier varbinary varchar sysname 无效的输入 = null
precision 数字基本数据类型的位数: datetime = 23 smalldatetime = 16 float = 53 real = 24 decimal (p,s) and numeric (p,s) = p money = 19 smallmoney = 10 int = 10 smallint = 5 tinyint = 3 bit = 1 all other types = 0 int 无效的输入 = null
scale 数字基本数据类型小数点右边的位数: decimal (p,s) 和 numeric (p,s) = s money 和 smallmoney = 4 datetime = 3 所有其它类型 = 0 int 无效的输入 = null
totalbytes 要包含值的元数据和数据所需的字节数。该信息在检查 sql_variant 列中数据的最大一侧时很有用。如果该值大于 900,索引创建将失败。 int 无效的输入 = null
collation 代表特定 sql_variant 值的排序规则。 sysname 无效的输入 = null
maxlength 最大数据类型长度(以字节为单位)。例如,nvarchar(50) 的 maxlength 是 100,int 的 maxlength 是 4。 int 无效的输入 = null
例如:
create table tablea (cola sql_variant,colb int) //创建表tablea
insert into tablea values(cast (462711.1 as decimal(18.2)),16811) //插入一条记录
select sql_variant_property(cola,’basetype’), //检索有关值为462711.1的cola
sql_variant_property(cola,’precision’), //sql_variant_property信息
sql_variant_property(cola,’scale’)
from tablea
where colb=1681
25. typeproperty 返回有关数据类型的信息。
原型:
typeproperty(type,property)
property 参数值说明: 属性
描述
返回的值
precision 数据类型的精度。 数字位数或字符个数。 null = 数据类型未找到。
scale 数据类型的小数位数。 数据类型的小数位的个数。 null = 数据类型不是 numeric 或未找到。
allowsnull 数据类型允许空值。 1 = true 0 = false null = 数据类型未找到。
usesansitrim 创建数据类型时 ansi 填充设置为 on。 1 = true 0 = false null = 数据类型未找到,或不是二进制数据类型或字符串数据类型。
例如:
select typeproperty(‘tinyint’,’precision’)
字符串函数
1.ascii 返回字符表达式最左端字符的ascii代码值。
例如:
set nocount on
declare @position int,@string char(15)
set @position = 1
set @string = ‘du monde entier’
while @position begin
select
ascii(substring(@string,@position,1)),
char(ascii(substring(@string,@position,1)))
set @position =@position+1
end
set nocount off
go
2.char 将int ascii代码转换为字符的字符串函数。
3.charindex 返回字符串中指定表达式的起始位置。
原型:
charindex(expression1,expression2,[start_location])
参数说明:
expression1 一个表达式,其中包含要寻找的字符的次序。
expression2 一个表达式,通常是一个用于指定序列的列。
[start_logcation] 在expression2中搜索expression1时的起始字符位置。
例如:
use pubs
go
select charindex(‘wonderful’,notes)
from titles
where title_id=’tc3218’
go
在使用[start_logcation]参数时要注意一点。它所能实现的功能是忽略前面的字符,从你给定的字符开始查找expression1在expression2中的位置。
例如:
declare @t varchar(50)
set @t=’ddfsadawfaafdadfa’
--1
select charindex(’a’,@t,6)
--2
select charindex(’a’,@t,4)
例1和例2的结果是不一样的。
4.difference 比较两个字符串。
例如:
use pubs
go
select soundex(‘green’)
soundex(‘greene’),difference(‘green’,’greene’)
go
5.left 返回从字符串左边开始指定个数的字符。
6.len 返回字符串中字符的数量。
7.lower 将大写字符数据转换为小写字符数据后返回字符表达式。
例如:
use pubs
go
select lower(substring(tit, le,1,20)) as lower,
lower(upper((substring(title,1,20))) as lowerupper
from titles
where price between 11:00 and 20:00
8.ltrim 删除字符串中的起始空格。
9.rtrim 删除字符串中的末尾的空格。
例如:
declare @string_to_trim varchar(60) //声明变量
set @string_to_trim = ‘ five spaces are at the beginning of this string’
//变量赋值
select ‘here is the strng without the leading spaces: ’+ ltrim (@string_to_trim)
//显示函数ltrim执行结果
10.nchar 根据unicode标准所进行的定义,用给定整数代码返回unicode字符。
例如:
declare @position int , @nstring nchar(9) //声明局部变量
set @position = 1 //变量赋值
set @nstring = n’k&benhavn’
print ‘character #’ + ‘ ’ + ‘unicode character’ + ‘ ‘ + ‘unicode value’ //打印输出
while @position begin
select @position , //显示函数nchar执行结果
nchar(unicode(substring(@nstring,@position,1))),
convert (nchar(17), substring(@nstring,@position,1)),
unicode(substring(@nstring,@position,1))
select @position = @position+1
end
go
11.patindex 返回指定表达式中某模式第一次出现的位置;如果在全部有效的文本和字符
数据类型中没有找到该模式,则返回零。
例如:
use pubs
go
select patindex(‘%wonderfull%’,notes)
from totles
where totle_id=’tc3218’
go
12.quotename 返回带有分隔符的unicode字符串
13.replace 用第三个表达式替换第一个字符串表达式中出现的所有第二个给定字符串
表达式。
例如:
select replace (‘abcdefabcfvabcetil’,’abc’,’xxx’)
14.replicate 以指定的次数重复字符表达式。
15.reverse 返回字符天大的反转。
16.right 返回字符串中从右边开始指定个数的integer_expression字符。
例如: