sqlserverの和暦

VBAでは和暦は出てくるけどsqlserverは日本の暦は表示できません
年月日ならば
FORMAT ( @d, ‘D’)で表示されるが実はこれ for china?

ということでH29/12/17のように変換するスカラー関数
CREATE FUNCTION iswareki
(
@paramDate datetime
)
RETURNS nvarchar(10)

AS
BEGIN
— Declare the return variable here
DECLARE @ResultVar nvarchar(10),@VarYear int,@VarDate nvarchar(10),@VarDateInt int

select @VarYear=cast(format(@paramDate,’yyyy’) as int)
select @VarDate=format(@paramDate,’MM/dd’)
select @VarDateInt=cast(format(@paramDate,’yyyyMMdd’) as int)

— Add the T-SQL statements to compute the return value here
select @ResultVar=(case
when @VarDateInt>19890107 then
concat(‘H’,format((@varyear-1988),’00’),’/’,@VarDate)–平成
when @VarDateInt>19261224 then
concat(‘S’,format((@varyear-1925),’00’),’/’,@VarDate) –昭和
when @VarDateInt>19120729 then
concat(‘T’,format((@varyear-1911),’00’),’/’,@VarDate) –大正
when @VarDateInt>18680124 then
concat(‘M’,format((@varyear-1867),’00’),’/’,@VarDate) –明治
else ‘0’
end
)
— Return the result of the function
RETURN @ResultVar

END
GO

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です