月末・月初
yymmdd形式で与えられた日付から、月末および月初を取得するSQLです。
-- 対象文字列 declare @ymd nvarchar(6); set @ymd = '090500'; -- 月初 declare @bom datetime; set @bom = convert(datetime, substring(@ymd, 1, 2) + substring(@ymd, 3, 2) + '01') -- 月末 declare @eom datetime; set @eom = dateadd("month", 1, @bom) - 1; select @eom;
ところで、月末を英語でEOM(end of the month)というのは解ったのですが、月初はなんていうんだろう、と思いつつ、とりあえず対称性を重んじてBOM(begin of the month)でいいやとしましたが、fore-endっていうんですね。ふーむ。