sql函数使用实例 select fd1,fd1,dbo.isNullOrEmpty(fdClass,'无类别') from [tableName]
2012-11-11 18:01
866 查看
为sqlserver写了一个isNullOrEmpty函数
sql server中有个函数isNull,接收两个参数,isNull(a,b)的返回值是这样的:如果a为null,这返回b,否则返回a
比如isnull(null,'qq')返回qq,isnull('','qq')返回''
有时候我们想如果a为空字符串的时候也返回b,即func('','qq')返回qq,那么sqlserver中就没有这种函数了,今天刚好遇见了这个需求,自己写了一个函数
isNullOrEmpty(a,b)
如果a为null或者空串,则返回b,否则返回a
代码很简单:
CREATE FUNCTION [isNullOrEmpty] (@srcStr varchar(50),@dstStr varchar(50))
RETURNS varchar(50) AS
BEGIN
set @srcStr=isnull(@srcStr,@dstStr)
if @srcStr=''
set @srcStr=@dstStr
return @srcStr
END
那么什么时候要用到这个函数呢?举个简单的例子,在某个表中有个字段fdClass存储的是该记录的属组,可以不填,我们想输出这些记录,并且把fdClass为空(或空字符串)的值显示为"无类别",呢么这个函数就发挥威力了:
select fd1,fd1,dbo.isNullOrEmpty(fdClass,'无类别') from [tableName]
注意:执行函数时需要指定用户,如dbo.funcName
相关文章推荐
- UCenter info: MySQL Query Error SQL:SELECT value FROM [Table]vars WHERE name=\'noteexists2′ Error:SELECT command denied to use
- 有多少行? SELECT rows FROM sysindexes WHERE id = OBJECT_ID('table_name') AND indid < 2
- Table 'table_name' is marked as crashed and last (automatic?) repair failed解决办法
- 解决 Error: Table './db_name/table_name' is marked as crashed and last (automatic?) repair
- discuz7.2问题SQL:SELECT * FROM [Table]notelist WHERE closed='0' AND app1'-5' LIMIT 1
- Can't perform Create, Update or Delete operations on 'Table(@TableName)' because it is read-only.
- 复制表,仅当使用了列的列表,并且 IDENTITY_INSERT 为 ON 时,才能在表 'TableName' 中为标识列指定显式值。
- ADODB使用excel的"Cannot modify the design of table 'xxxx'. It is in a read-only database"的问题
- select * from dbo.sysobjects where type not in('S','D','K')
- python实例2—NameError: name 'file' is not defined
- select replace(t.area_name,' ','') a from dic_area t where t.area_code = '131127'
- 内存变量 Export / Import ...To Memory ID 'ID_Name' / From Memory ID 'ID_Name' 导出/读取的使用
- mysql -- error code [1442]; Can't update table 'table_name' in stored function/trigger because it is already used by statement w
- You can't specify target table 'table_name' for update in FROM clause
- 句话是什么意思?if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[PerPer
- NLTK使用中NameError: name 'FreqDist' is not defined问题解决
- Table 'table_name' is marked as crashed and should be repaired
- 复制表,仅当使用了列的列表,并且 IDENTITY_INSERT 为 ON 时,才能在表 'TableName' 中为标识列指定显式值。 .
- 【转】ibatis 中使用select top #pagesize# * from tablename
- strSQL = "Select * From " & strTableName & " Where False "