此函数将根据sys.syslanguages 表转换字符串中的月份。
即SELECT dbo.fn_tranMonth(2,0,'1 déc. 2014 10:26:14 UTC+00:00')
结果:
2014 年 12 月 1 日 10:26:14 UTC+00:00
CREATE FUNCTION [dbo].[Split] (@sep char(1), @s varchar(8000))
RETURNS table
AS
RETURN (
WITH Pieces(pn, start, stop) AS (
SELECT 1, 1, CHARINDEX(@sep, @s)
UNION ALL
SELECT pn + 1, stop + 1, CHARINDEX(@sep, @s, stop + 1)
FROM Pieces
WHERE stop > 0
)
SELECT
SUBSTRING(@s, start, CASE WHEN stop > 0 THEN stop-start ELSE 512 END) AS s
FROM Pieces
)
GO
CREATE FUNCTION dbo.fn_tranMonth
(
@fromLan INT
,@toLan INT
,@string VARCHAR(MAX)
)
RETURNS
VARCHAR(50)
AS
BEGIN
DECLARE @TTTT AS TABLE(PK INT IDENTITY(1,1)
,fromMonth VARCHAR(50)
,toMonth VARCHAR(50)
)
DECLARE
@fromMonths VARCHAR(200)
,@toMonths VARCHAR(200)
,@fromMonth VARCHAR(20)
,@toMonth VARCHAR(20)
,@rowNum INT=12;
SELECT @fromMonths=shortmonths
FROM SYS.syslanguages
WHERE langid=@fromLan;
SELECT @toMonths=shortmonths
FROM sys.syslanguages
WHERE langid=@toLan;
INSERT @TTTT(fromMonth)
SELECT S
FROM dbo.Split(',',@fromMonths);
DECLARE @TTTT2 AS TABLE(PK INT IDENTITY(1,1)
,toMonth VARCHAR(50)
)
INSERT @TTTT2(toMonth)
SELECT S
FROM dbo.Split(',',@toMonths);
UPDATE @TTTT
SET toMonth=B.toMonth
FROM
@TTTT A
JOIN @TTTT2 B ON A.PK=B.PK;
DECLARE
@loopPos INT=0
,@returnMonth VARCHAR(50);
WHILE @loopPos<@rowNum
BEGIN
SET @loopPos+=1;
SELECT
@fromMonth=fromMonth
,@toMonth=toMonth
FROM @TTTT
WHERE PK=@loopPos;
SET @string=REPLACE(@string,@fromMonth,@toMonth);
END;
RETURN @string;
END