SQL では、次のコードを実行します。印刷セクションが実行されると、「2050 年 12 月 31 日 11:59PM」が返されます。コードの後半部分 (表示されていません) で変数を使用していますが、機能せず、「文字列を smalldatetime データ型に変換するときに変換に失敗しました」というエラーが表示されます。
フォーマットを混同していませんか? datetime は 'yyyy-mm-dd hh:mm:ss' として表示されると思っていましたが、明らかに間違っているに違いありません。datetim 形式を見るたびに、これはうまくいくはずだと言っているので、何が間違っているのかわかりません。
declare @date int
--@date is yyyymm format
declare @sql_v_fx as varchar(max), @sql_dim_for_exc as varchar(max)
declare @maxEndDate as smalldatetime
set @date = 201202
set @maxEndDate = Convert(smalldatetime, '2050-12-31 23:59', 101)
print @maxenddate
--remove prior date in [zstbl_fx_rollback]
truncate table zstbl_fx_rollback
--insert the user entered month
--specify in the VS report whether it's Balance Sheet or Income Statement date
insert into [zstbl_fx_rollback] (yyyymm) select @date
--alter the v_fx view to into account the date in the [zstbl_fx_rollback] table
select @sql_v_fx =
'
Alter view [dbo].[v_fx] as
select
[foreign exchange key],[currency key], [to usd], [from usd], [effective date],
[expiration date] =
case when [effective date] =
convert(datetime,cast((select isnull(max(yyyymm),205012) from zstbl_fx_rollback) * 100 + 1 as char(8))) then ' + @maxEndDate + ' else [expiration date] end
,[is current]
,[effective date key] = year([effective date]) * 10000 + month([effective date])*100
from crs..[dim foreign exchange]
where [effective date] <= convert(datetime,cast((select isnull(max(yyyymm),205012) from zstbl_fx_rollback) * 100 + 1 as char(8)))
'
exec(@sql_v_fx)