简体   繁体   English

在报告生成器(SSRS)中获取本地时间月份名称

[英]Get Local Time Month Name in report builder (SSRS)

I wrote this SSRS expression to get the month name, it gives the month name in English, but I want to get the Local Time or specific culture month name (eg. Serbian). 我写了这个SSRS表达式来获取月份名称,它用英语给出了月份名称,但是我想获取本地时间或特定文化月份的名称(例如塞尔维亚语)。

How to solve in SSRS ? 如何解决SSRS

MonthName(Month(Today()))

My initial though was that you can do it by using SET LANGUAGE before getting the month name. 我最初的想法是,您可以在获取月份名称之前使用SET LANGUAGE进行SET LANGUAGE

However, turns out that Sql Server does not support Serbian language (it's not in sys.syslanguages at least in Sql Server 2012), so your best option is to create a table or cte with the localized month names and numbers and use it instead of the MonthName function. 但是,事实证明Sql Server不支持塞尔维亚语言(至少在Sql Server 2012中不是sys.syslanguages ),因此,最好的选择是使用本地化的月份名称和数字创建表或cte并使用它代替MonthName函数。
Edit 编辑
Actually, a table will serve you better as it will only be needed to created once: 实际上,表将为您提供更好的服务,因为只需要创建一次即可:

CREATE TABLE SerbianMonths (
    SerbianMonths_Id int,
    SerbianMonths_Name nvarchar(10)
)

INSERT INTO SerbianMonths VALUES 
(1, N'јануар'),
(2, N'фебруар'),
(3, N'март'),
(4, N'април'),
(5, N'мај'),
(6, N'јун'),
(7, N'јул'),
(8, N'август'),
(9, N'септембар'),
(10, N'октобар'),
(11, N'новембар'),
(12, N'децембар')

(Sorry if I've got the spelling wrong, I've copied from this website ) (对不起,如果我拼写错误,我是从本网站复制的)

You can then create your own user-defined function to get the month name by it's number: 然后,您可以创建自己的用户定义函数,以按数字获取月份名称:

CREATE FUNCTION GetSerbianMonthName
(
    @MonthNumber int
) 
RETURNS nvarchar(10)
AS
BEGIN
    DECLARE @MonthName nvarchar(10)
    SELECT @MonthName = SerbianMonths_Name
    FROM SerbianMonths
    WHERE SerbianMonths_Id = @MonthNumber
    RETURN @MonthName
END

and use it like this: SELECT dbo.GetSerbianMonthName(2) 并像这样使用它: SELECT dbo.GetSerbianMonthName(2)

If you want to use more unsupported languages, you can add a language table and a language_id column to your months table: 如果要使用更多不受支持的语言,则可以在月份表中添加语言表和language_id列:

CREATE TABLE Languages 
(
    Language_Id int,
    Language_Name nvarchar(100)
)

CREATE TABLE MonthNames 
(
    MonthNames_MonthNumber int, -- values from 1 to 12
    MonthNames_LanguageId int, -- FK to languages table
    MonthNames_Name nvarchar(100) -- the local month name
)

set a composite primary key on both MonthNumber and LanguageId, and add the language id to your select from this table: 在MonthNumber和LanguageId上设置一个复合主键,并将语言ID添加到此表中的选择中:

CREATE FUNCTION GetMonthName
(
    @Language_Id int,
    @MonthNumber int
) 
RETURNS nvarchar(10)
AS
BEGIN
    DECLARE @MonthName nvarchar(100)
    SELECT @MonthName = MonthNames_Name 
    FROM MonthNames 
    WHERE MonthNames_MonthNumber = @MonthNumber
    AND MonthNames_LanguageId  = @Language_Id
    RETURN @MonthName
END

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM