sql 函数

 

 

ISNULL函数
isnull(<要检查的表达式>,<如果为null时替换的值>)

CAST函数
cast(<要转换的表达式> as <转换成的数据类型>)
看例子





select
c.LastName,


isnull
(


cast
(

(


select

min
(OrderDate)


from
Sales.SalesOrderHeader o


where
o.ContactID
=
c.ContactID

)
as

varchar
),
'
neverordered
'
)
as
orderdate


from
Person.Contact c

再看一个例子:


use  AdventureWorks
select   ' the order num is '   +   Cast (SalesOrderID  as   varchar )  as  xland
from  Sales.SalesOrderHeader
where  CustomerID  =   5

 

CONVERT函数
convert(数据类型,表达式[,格式])
这个函数和cast类似,我们先看cast的一个例子


use  AdventureWorks
select  OrderDate,  Cast (OrderDate  as   varchar )  as  Converd
from  Sales.SalesOrderHeader
where  CustomerID  =   5

再看convert的例子


use  AdventureWorks
select  OrderDate,  convert ( varchar ( 12 ), OrderDate, 111 )  as  Converd
from  Sales.SalesOrderHeader
where  CustomerID  =   5

这里对日期的格式做了限制
具体这么限制要查convert的微软帮助文件

EXISTS函数
先看例子
返回数据是否存在的布尔变量


use  AdventureWorks
select  e.EmployeeID,FirstName,LastName
from  HumanResources.Employee e
join  Person.Contact c
on  e.ContactID  =  c.ContactID
where   exists
(
     select  EmployeeID  from  HumanResources.JobCandidate jc
     where  jc.EmployeeID  =  e.EmployeeID
)

这个例子也可以完成相同的功能


use  AdventureWorks
select   distinct  e.EmployeeID ,FirstName,LastName
from  HumanResources.Employee e
join  Person.Contact c
     on  e.ContactID  =  c.ContactID
join  HumanResources.JobCandidate jc
     on  jc.EmployeeID  =  e.EmployeeID

第一个例子比第二个例子在性能上要好很多
第三个用exists的例子


use  master
go  
if   not   exists  ( select   ' True '   From  sys.databases  where  name  =   ' xland ' )
begin
    create   database  xland
end  
else
begin
     print   ' xland is exist '
end  
go

 在这里将看到if else和begin end的用法

FLOOR
获取提供的值,把他取整到最近的整数上


declare   @myval   int ;
set   @myval   =   3.6 ;
set   @myval   =   @myval   *   4.8 ;
select   floor ( @myval ) + . 9   as  mycoloumn


COALESCE
coalesce哪个不为空用哪个

COALESCE (i.ProductID,d.ProductID)

 
@@Datefirst
系统认为星期几是一周的第几天,返回这个数字

@@Error
返回错误号,没有错误返回0

@@cursor_rows
返回游标中的行数

@@fetch_status
返回最后一个游标fetch操作状态的指示值
0成功
-1失败,超过了游标的尾
-2失败,当前记录被删除,发生在滚动游标和动态游标上

@@identity
返回当前连接创建的最后一行记录的标志

@@rowcount
返回上一条语句影响的行数

0
0
(

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值