代碼如下:
/*
--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)
*/