例1:
ALTER FUNCTION [dbo].[ConvertToPinYin](@source [nvarchar](4000))
RETURNS [nvarchar](4000) WITH EXECUTE AS CALLERASEXTERNAL NAME [Job88.FullTextSearch.DataSourceFormatter].[Job88.FullTextSearch.DataSourceFormatter.Func_FormatFieldValue].[ConvertToPinYin]
例2:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
/*
SELECT dbo.Func_GetPinYinExt('中/国/人')
*/
ALTER FUNCTION [dbo].[Func_GetPinYinExt]
(
@source nvarchar(4000)
)
RETURNS nvarchar(4000)
AS
BEGIN
-- DECLARE @source nvarchar(4000)
-- SET @source = '(中国/人民)'
DECLARE @pinyin TABLE(
ch char(1),
hz1 nchar(1) COLLATE Chinese_PRC_CS_AS_KS_WS,
hz2 nchar(1) COLLATE Chinese_PRC_CS_AS_KS_WS)
INSERT INTO @pinyin (ch, hz1, hz2)
SELECT 'A', N'吖', N'鏊'
UNION ALL SELECT 'B', N'八', N'簿'
UNION ALL SELECT 'C', N'嚓', N'错'
UNION ALL SELECT 'D', N'哒', N'跺'
UNION ALL SELECT 'E', N'屙', N'贰'
UNION ALL SELECT 'F', N'发', N'馥'
UNION ALL SELECT 'G', N'旮', N'过'
UNION ALL SELECT 'H', N'铪', N'蠖'
UNION ALL SELECT 'J', N'丌', N'竣'
UNION ALL SELECT 'K', N'咔', N'廓'
UNION ALL SELECT 'L', N'垃', N'雒'
UNION ALL SELECT 'M', N'妈', N'穆'
UNION ALL SELECT 'N', N'拿', N'糯'
UNION ALL SELECT 'O', N'噢', N'沤'
UNION ALL SELECT 'P', N'趴', N'曝'
UNION ALL SELECT 'Q', N'七', N'群'
UNION ALL SELECT 'R', N'蚺', N'箬'
UNION ALL SELECT 'S', N'仨', N'锁'
UNION ALL SELECT 'T', N'他', N'箨'
UNION ALL SELECT 'W', N'哇', N'鋈'
UNION ALL SELECT 'X', N'夕', N'蕈'
UNION ALL SELECT 'Y', N'丫', N'蕴'
UNION ALL SELECT 'Z', N'匝', N'做'
DECLARE @i int, @j int, @isSplitChar bit, @isFirstChar bit
SET @i = PATINDEX('%[吖-做]%' COLLATE Chinese_PRC_CS_AS_KS_WS, @source)
WHILE @i > 0
BEGIN
SELECT
@source = REPLACE(@source, SUBSTRING(@source, @i, 1), ch)
,@i = PATINDEX('%[吖-做]%' COLLATE Chinese_PRC_CS_AS_KS_WS, @source)
FROM @pinyin
WHERE SUBSTRING(@source, @i, 1) BETWEEN hz1 AND hz2
END
--SELECT (@source)
DECLARE @returnString nvarchar(4000)
SET @j = PATINDEX('%[A-Z]%', @source)
SET @returnString = SUBSTRING(@source, @j, 1)
SET @i = CHARINDEX('/', @source)
--SELECT @i
WHILE @i > 0
BEGIN
SET @source = SUBSTRING(@source, @i + 1, LEN(@source) - @i)
SET @j = PATINDEX('%[A-Z]%', @source)
SET @returnString = @returnString + '/' + SUBSTRING(@source, @j, 1)
SET @i = CHARINDEX('/', @source)
END
--SELECT @returnString
RETURN (@returnString)
END