在 SQL 中,我执行以下代码。当打印部分运行时,它会返回“2050 年 12 月 31 日晚上 11:59”。我在代码的后面部分(未显示)中使用了该变量,但它不起作用,并且出现“将字符串转换为 smalldatetime 数据类型时转换失败”错误。
我混淆了我的格式吗?我认为日期时间是 '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)