SQLServer 设置单词首字母大写

网络整理 - 08-20


/*
--SQLServer中将字符串首字母设置大写:
--作者:jinjazz /csdn
--SQLServer2005启用OLEAutomation
sp_configure 'show advanced options', 1;
go
RECONFIGURE;
go
sp_configure 'Ole Automation Procedures', 1;
go
RECONFIGURE;
go
*/
use tempdb
go
if (object_id ('fn_toTitleCase' ) is not null )
drop function dbo . fn_toTitleCase
go
create function fn_toTitleCase (@inStr varchar (8000 ))
returns varchar (8000 )
as
begin
declare @str varchar (8000 )
set @str = 'var data = "' +@inStr + '";
var reg = /\b(\w)|\s(\w)/g ;
data = data.toLowerCase();
data.replace(reg,function(m){return m.toUpperCase()});'
declare @object int
declare @r varchar (800 )
exec sp_OACreate 'MSScriptControl.ScriptControl' ,@object output
exec sp_OASetProperty @object , 'Language' , 'javascript'
exec sp_OAMethod @object , 'eval' , @r out , @str
return @r
end
go
select dbo. fn_toTitleCase ('create function fn_toTitleCase(@inStr varchar(8000)) returns varchar(8000)' )
/*输出
Create Function Fn_totitlecase(@Instr Varchar(8000)) Returns Varchar(8000)
*/