0

SQL Server を使用するように変換された、もともと Oracle 用に作成されたコードがいくつかあります。ただし、現在、フランスで日付名が英語で表示されるという問題があります。これは、CONVERT が形式を変更するために使用されているがSET LANGUAGE、セッションの言語を変更するために使用されていないためです (Oracle 言語では、to_char同等の変換を提供する関数の一部として含まれています)。

SET LANGUAGEコードが関数内にあり、setそこに含めることができない副作用があるため、使用されなかったと思います。

誰もこれを回避する方法を知っていますか; つまり、セッション全体に影響を与えるのではなく、関数内の単一のステートメントの言語を変更するには?

編集

私の問題をよりよく説明するために、たとえば以下のように、SQL で Oracle の機能を再作成しようとすることを想像してください。

--this won't work because SET LANGUAGE affects the state of the session; 
create function dbo.to_char(@date datetime, @format nvarchar(32), @language nvarchar(32))
returns nvarchar(32)
as 
begin
      declare @result nvarchar(32)
      , @formatSql int

      --here we need to map the available Oracle options to their SQL equivs
      --SQL is more restrictive in that it uses predefined date formats rather than masks
      --I can't recall the typical valid oracle format masks - but just append to this those that you use
      set @formatSql = 
      case @format
            when 'dd mon yyyy' then 106
            when 'mon dd yyyy' then 107
            when 'dd/mm/yyyy'  then 103
            when 'mm/dd/yyyy'  then 101
            else 121 --default to yyyy-mm-dd hh:mi:ss.mmm(24h)
      end

      select @language = REPLACE(@language,'NLS_DATE_LANGUAGE','')
      , @language = REPLACE(@language,'=','')
      , @language = REPLACE(@language,' ','')

      set language @language 

      set @result = CONVERT(nvarchar(32), @date, @formatSql)

      set language English --revert session language

      return @result
end
go


select dbo.to_char(getutcdate(), 'dd mon yyyy', 'NLS_DATE_LANGUAG = French') --from dual ;)

前もって感謝します。

4

1 に答える 1