SQL/SQL SERVER相关函数集合

Sql函数集合

聚合函数:


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 <5 
   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<2 
    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 <10
    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<=16 and index_col(@name,@indid,
@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属性的参数:

例如: 
        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 属性值参数说明:

例如:   
        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 基本类型

BaseTypeSQL Server 数据类型,如: char int money nchar ntext numeric nvarchar real smalldatetime smallint smallmoney text timestamp tinyint uniqueidentifier varbinary varcharsysname 无效的输入 = 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 = 0int 无效的输入 = NULL
Scale数字基本数据类型小数点右边的位数: decimal (p,s) 和 numeric (p,s) = s money 和 smallmoney = 4 datetime = 3 所有其它类型 = 0int 无效的输入 = 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 <=datalength(@string) 
                      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 <= datalength(@nstring) //循环判断执行 
       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字符。 
例如: 
        Use pubs 
        Go 
        Select right(au_fname,5) 
        From authors 
        Order by au_fname 
        Go 

17.soundex 返回由四个字符组成的代码,以评估两个字符串的相似性。 

18.space 返回由重复的空格组成的字符串。也可以向字符串中插入空格。 

19.str 由数字数据转换来的字符数据。 
原型: 
        Str (float_expression [, length,[decimal]]) 
        参数说明: 
               Float_expression 是带有小数点的近似数字数据类型的表达式。 
               Length 是总的长度。包括小数点、符号、数字或空格。 
               Decimal 是小数点右边的位数。 

20.stuff 删除指定长度的字符并在指定的起始点插入另一组字符。 
原型: 
       Stuff(character_expression,start,length,character_expression) 
例如: 
       Select stuff(‘abcdefx’,1,2’bckjkjoui’) 

21.substring 返回字符,binary,text或image表达式的一部分。 
       原型: 
              Substring(expression,start,length) 

22.unicode 按照unicode标准的定义,返回输入表达式的第一个字符的整数值。 

23.upper 返回将小写字符数据转换成大写的字符表达式。


文本和图像函数

1.patindex 返回指定表达式中某模式每一次出现的起始位置。详细参阅字符串函数中的patindex。 


2.textptr 以varbinary格式返回对应于text、ntext或image列的文本指针值。 
原型: 
        Textptr(column) 
例如1: 
        Use pubs 
        Go 
        Declare @ptval varbinary(16) 
        Select @ptrval = textptr(logo) 
        From pub_info pr,publishers p 
        Where p.pub_id=pr.pub_id and p.pub_name=’new moon books’ 
        Go 
例如2: 
        Create table t1 (c1 int,c2 text) 
        Exec sp_tableoption ‘t1’,’text in row’,’on’ 
        Insert t1 values(‘1’,’this is text.’) 
        Go 
        Begin tran   
        Declare @ptrval varbinary(16) 
        Select @ptrval = textptr(c2) 
        From t1 where c1=1 
        Readtext t1.c2 @ptrval 0 1 
        Commit 


3.textvald 一个text、ntext或image函数,用于检查给定文本指针是否有效。 
原型: Textvald(‘table.column’,text_ptr) 
例如: 
        Use pubs 
        Go 
        Select pub_id,’valid (if 1) text data’= textvald(‘pub_info.log’,textptr(logo)) 
        From pub_info 
Order by pub_id 
Go



配置函数
1.connections 返回上次启动sql server 以来连接或试图连接的次数。


2.datefirst 返回set datefirst参数的当前值,setdatefirst参数指明所规定的每周第一天:
1对应星期一,2对应星期二。。。。。7对应星期日。


3.dbts 为当前数据库返回当前timestamp数据类型的值。是数据库中唯一的值。


4.langid 返回当前所使用语言的本地语言标识符(ID)。


5.language 返回当前使用的语言名。


6.lock_timeout 返回当前会话的当前锁超时设置,单位为毫秒。


7.max_connections 批回microsoft sql server 上允许的同时用户连接的最大数。返回的数不必为当前的配置的数值。


8.max_precision 返回decimal 和 numeric数据类型所用的精度级别,即该服务器中当前设置的精度。


9.nestlevel 返回当前存储过程执行的嵌套层次(初始值为0)。


10.options `返回当前set 选项的信息。


11.remserver 当远程sql server数据库服务器在登录中出现时,返回它的名称。


12.servername 返回运行microsoft sql server 的本地服务器名称。


13.servicename 返回sql server正在运行的实例名。若当前实例为默认实例,则@@servicename返回mssqlserver;否则返回当前实例名。


14.spid 返回当前用户进程的服务器进程标识符(ID)。该结果与当时系统实际运行情况有关。


15.textsize 返回set语句textsize选项的当前值,它指定select语句返回的text 或image数据的最大长度,心字节为单位。


16.service 返回sql server当前安装的日期、版本和处理类型。
例如:
Use northwind
   Select @@service
上同:




用sql server内置函数格式化日期型字符串

Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AM 
Select CONVERT(varchar(100), GETDATE(), 1): 05/16/06 
Select CONVERT(varchar(100), GETDATE(), 2): 06.05.16 
Select CONVERT(varchar(100), GETDATE(), 3): 16/05/06 
Select CONVERT(varchar(100), GETDATE(), 4): 16.05.06 
Select CONVERT(varchar(100), GETDATE(), 5): 16-05-06 
Select CONVERT(varchar(100), GETDATE(), 6): 16 05 06 
Select CONVERT(varchar(100), GETDATE(), 7): 05 16, 06 
Select CONVERT(varchar(100), GETDATE(), 8): 10:57:46 
Select CONVERT(varchar(100), GETDATE(), 9): 05 16 2006 10:57:46:827AM 
Select CONVERT(varchar(100), GETDATE(), 10): 05-16-06 
Select CONVERT(varchar(100), GETDATE(), 11): 06/05/16 
Select CONVERT(varchar(100), GETDATE(), 12): 060516 
Select CONVERT(varchar(100), GETDATE(), 13): 16 05 2006 10:57:46:937 
Select CONVERT(varchar(100), GETDATE(), 14): 10:57:46:967 
Select CONVERT(varchar(100), GETDATE(), 20): 2006-05-16 10:57:47 
Select CONVERT(varchar(100), GETDATE(), 21): 2006-05-16 10:57:47.157 
Select CONVERT(varchar(100), GETDATE(), 22): 05/16/06 10:57:47 AM 
Select CONVERT(varchar(100), GETDATE(), 23): 2006-05-16 
Select CONVERT(varchar(100), GETDATE(), 24): 10:57:47 
Select CONVERT(varchar(100), GETDATE(), 25): 2006-05-16 10:57:47.250 
Select CONVERT(varchar(100), GETDATE(), 100): 05 16 2006 10:57AM 
Select CONVERT(varchar(100), GETDATE(), 101): 05/16/2006 
Select CONVERT(varchar(100), GETDATE(), 102): 2006.05.16 
Select CONVERT(varchar(100), GETDATE(), 103): 16/05/2006 
Select CONVERT(varchar(100), GETDATE(), 104): 16.05.2006 
Select CONVERT(varchar(100), GETDATE(), 105): 16-05-2006 
Select CONVERT(varchar(100), GETDATE(), 106): 16 05 2006 
Select CONVERT(varchar(100), GETDATE(), 107): 05 16, 2006 
Select CONVERT(varchar(100), GETDATE(), 108): 10:57:49 
Select CONVERT(varchar(100), GETDATE(), 109): 05 16 2006 10:57:49:437AM 
Select CONVERT(varchar(100), GETDATE(), 110): 05-16-2006 
Select CONVERT(varchar(100), GETDATE(), 111): 2006/05/16 
Select CONVERT(varchar(100), GETDATE(), 112): 20060516 
Select CONVERT(varchar(100), GETDATE(), 113): 16 05 2006 10:57:49:513 
Select CONVERT(varchar(100), GETDATE(), 114): 10:57:49:547 
Select CONVERT(varchar(100), GETDATE(), 120): 2006-05-16 10:57:49 
Select CONVERT(varchar(100), GETDATE(), 121): 2006-05-16 10:57:49.700 
Select CONVERT(varchar(100), GETDATE(), 126): 2006-05-16T10:57:49.827 
Select CONVERT(varchar(100), GETDATE(), 130): 18 ???? ?????? 1427 10:57:49:907AM

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值