sql查询不返回任何数据

6qftjkof  于 2021-07-27  发布在  Java
关注(0)|答案(1)|浏览(320)

我正在尝试测试存储过程中使用的查询。
该存储过程是从另一个用于工作的数据库复制的。
存储过程将接受一个参数 yyyymm ,例如 202006 它有一个使用 substrings 从月份和年份的参数。
这就是计算月份和年份数据的方法:

declare @themonth nvarchar(15) = '202006'

declare @month nvarchar(5)
set @month = substring(@themonth,5,2)

declare @year nvarchar(5)
set @year= substring(@themonth,1,4)

选择数据时 202006 ,我正在获取该参数的所有记录。
所以,这里有一些逻辑。我不需要在这里写整个查询。我只提供一些伪代码:

Select top 10000 mmi.theMonth, *
from (here we have inner joins)

where mmi.theMonth = @theMonth

执行时,我得到:

theMonth   rptdate
202006     2020-06-30 00:00:00.000

rptdate是date数据类型themonth是nvarchar(6)
现在,当向该查询添加另一个条件时,将不返回任何内容。
这里有一个条件:

and rptdate like @month + '%' and rptdate like '%' + @year

我不知道为什么。 2020-06-30 00:00:00.000 包含 @month = '06' and @year = '2020' 这种逻辑有什么问题?

k4aesqcs

k4aesqcs1#

你有通配符 % 在代码中的顺序错误,但您也可以这样做

where year(rptdate) * 100) + month(rptdate) = @theMonth

相关问题