Technical Article

Convert Months,Days,ShortMonths to different Languages

,

This script allows you to convert date languages without exsplicitly setting the language in sql server it can be used in views,storedprocedures and select statements

/**

examples

 posible input values Months,Days,ShortMonths

 Returns all values for the language selected

 select * from dbo.FN_ConvertLanguageValues('Dutch','Months')

 select * from dbo.FN_ConvertLanguageValues('Dutch','Days')

 select * from dbo.FN_ConvertLanguageValues('Dutch','ShortMonths')

'Direct query example returns current month for current date

select item as dutchmonth from dbo.FN_ConvertLanguageValues('Dutch','months') where row = DATEPART(m, getdate())

 

 'Select the dayname for the current day

 select item as dutchday from dbo.FN_ConvertLanguageValues('Dutch','days') where row = DATEPART(weekday, getdate())

 'Table Query Examples

  select myfield,(select item as dutchmonth from dbo.FN_ConvertLanguageValues('Dutch','months') where row = DATEPART(m, mydatefield)) as dutchmonth from mytable

  select myfield,(select item as dutchday from dbo.FN_ConvertLanguageValues('Dutch','days') where row = DATEPART(weekday, mydatefield)) as dutchday from mytable

 Helper Functions

--Example select the day name for the given date

--select dbo.dayNames('Dutch',GetDATE()) as dayname

--Example Select the MonthName for the Given Date

--select dbo.monthNames('Dutch',GetDATE()) as monthname

 -- additional example

Select dbo.daynames('dutch',GetDate()) + ' ' + convert(varchar,Datepart(day,getdate())) + ' ' + dbo.monthnames('dutch',getdate()) + ' ' + convert(varchar(4),year(getdate()))

Create FUNCTION [dbo].[FN_ConvertLanguageValues]
(
 @language varchar(100),
 @field varchar(20)
) 
RETURNS @List TABLE (row [int] identity(1,1) not null, item VARCHAR(2000))
/**
examples
 posible input values Months,Days,ShortMonths
 Returns all values for the language selected
 select * from dbo.FN_ConvertLanguageValues('Dutch','Months')
 select * from dbo.FN_ConvertLanguageValues('Dutch','Days')
 select * from dbo.FN_ConvertLanguageValues('Dutch','ShortMonths')

'Direct query example returns current month for current date
select item as dutchmonth from dbo.FN_ConvertLanguageValues('Dutch','months') where row = DATEPART(m, getdate()) 
 
 'Select the dayname for the current day
 select item as dutchday from dbo.FN_ConvertLanguageValues('Dutch','days') where row = DATEPART(weekday, getdate()) 

 'Table Query Examples
  select myfield,(select item as dutchmonth from dbo.FN_ConvertLanguageValues('Dutch','months') where row = DATEPART(m, mydatefield)) as dutchmonth from mytable
  select myfield,(select item as dutchday from dbo.FN_ConvertLanguageValues('Dutch','days') where row = DATEPART(weekday, mydatefield)) as dutchday from mytable

****/BEGIN

DECLARE @sItem VARCHAR(2000),
@sInputList VARCHAR(2000),
@sDelimiter CHAR(1) = ','

SET @sItem = ''

if @field = 'Months'
Begin
Set @sInputList = (Select Months From sys.sysLanguages where alias = @language)
end
if @field = 'Days'
Begin
Set @sInputList = (Select Days From sys.sysLanguages where alias = @language)
End
if @field = 'ShortMonths'
Begin
Set @sInputList = (Select shortmonths From sys.sysLanguages where alias = @language)
End

WHILE CHARINDEX(@sDelimiter,@sInputList,0) <> 0
BEGIN
    SET @sItem= RTRIM(LTRIM(SUBSTRING(@sInputList,1, CHARINDEX(@sDelimiter, @sInputList, 0) - 1)))
    SET @sInputList= RTRIM(LTRIM(SUBSTRING(@sInputList, CHARINDEX(@sDelimiter, @sInputList, 0)+LEN(@sDelimiter),LEN(@sInputList))))
    INSERT INTO @List SELECT Replace(@sItem,left(@sItem,1),UPPER(left(@sItem,1)))
END

INSERT INTO @List SELECT @sInputList

RETURN


END 

-**** Helper Functions
   Create Function dbo.[MonthNames]
 (
 @Language Varchar(50),
 @Date datetime
 )
 Returns Varchar(50)
 --Example
 --select dbo.monthNames('Dutch',GetDATE()) as monthname

 Begin
 Declare @rValue varchar(50)
 Set @rValue = (select item as dutchmonth from dbo.FN_ConvertLanguageValues(@Language,'months') where row = DATEPART(m, @date)) 
 Return @rValue
 End
 

 Create Function dbo.[DayNames]
 (
 @Language Varchar(50),
 @Date datetime
 )
 Returns Varchar(50)
 --Example
 --select dbo.dayNames('Dutch',GetDATE()) as dayname

 Begin
 Declare @rValue varchar(50)
 Declare @datefirst int
 Set @dateFirst = (select datefirst from sys.syslanguages where alias = @language)

 if @Datefirst = 1 

  Begin
 Set @rValue =   (
   Select item from 
   (
     select item, case when row = 1 then 7
                   when row = 2 then 1
   when row = 3 then 2
   when row = 4 then 3
   when row = 5 then 4
   when row = 6 then 5
   when row = 7 then 1 end as row 
   from dbo.FN_ConvertLanguageValues(@Language,'Days')
   ) as d1 where row =  DATEPART(weekDay, @date))


  End
  Else
  Begin
 Set @rValue = (select item as dutchmonth from dbo.FN_ConvertLanguageValues(@Language,'Days') where row = DATEPART(weekDay, @date)) 
  End


 Return @rValue
 End

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating