简体   繁体   中英

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).

How to solve in SSRS ?

MonthName(Month(Today()))

My initial though was that you can do it by using SET LANGUAGE before getting the month name.

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.
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)

If you want to use more unsupported languages, you can add a language table and a language_id column to your months table:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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