zoukankan      html  css  js  c++  java
  • sql 随笔 2015-07-02

    sql 自定义函数

     --检查函数是否存在
     if exists (select * from dbo.sysobjects where id = object_id(N'dbo.pTitleCase') and xtype in (N'FN', N'IF', N'TF'))    
      drop function dbo.pTitleCase  ;
     go
    
    --创建函数
    create function dbo.pTitleCase(@strIn nvarchar(Max))
    	returns nvarchar(Max)
    as 
    	begin;
    		declare
    		@strOut nvarchar(max),
    		@currentPostion int,
    		@nextSpace int,
    		@currentWord nvarchar(max),
    		@strLen int,
    		@lastWord bit;
    	
    	set @nextSpace = 1;
    	set @currentPostion =1;
    	set @strOut ='';
    	set @strLen=Len(@strIn);
    	set @lastWord=0;
    	
    	while @lastWord=0
    		begin;
    			set @nextSpace=CharIndex( ' ',@strIn,@currentPostion+1)
    			if @nextSpace=0 --no more spaces found
    				begin;
    					set @nextSpace=@strLen;					
    					set @lastWord=1;
    				end;
    			
    			set @currentWord = Upper(substring(@strIn,@currentPostion,1));
    			set @currentWord = @currentWord + lower(substring(@strin,@currentPostion+1,@nextSpace-@currentPostion));
    			set @strOut=@strOut+@currentWord;
    			set @currentPostion=@nextSpace+1;
    		end;
    	return @strOut;
    end;
    go	
    
    --执行函数
    SELECT [Demo].[dbo].[pTitleCase] ( 'one TWO tHrEe')
    go
    			
    	
    

      

  • 相关阅读:
    JS 录音
    JS学习笔记 之 作用域链
    JS学习笔记 之 预编译
    JS学习笔记 之 递归
    JS学习笔记 之 函数声明
    JS学习笔记 之 隐式类型转换
    JS学习笔记 之 typeof
    JS学习笔记 之 for循环
    JS学习笔记 之 运算符
    JS学习笔记 之 数据类型
  • 原文地址:https://www.cnblogs.com/xiaobuild/p/4616027.html
Copyright © 2011-2022 走看看