在sql中将%转换为数据类型int时出错

时间:2018-03-30 10:56:45

标签: sql sql-server tsql

iam使用简单查询我想要提取年份,并且应该在表格提取中使用

select * 
from  v_AuthListInfo LI 
where title like '%SUG%' 
  and Title like '%P1%' 
  and Title like '%' + '' + year(getdate()) +  '' + '%'

我收到此错误

Msg 245, Level 16, State 1, Line 26
Conversion failed when converting the varchar value '%' to data type int.

理想情况下应该是2018年,它应该在2018年提取这些记录 我希望减去1年的记录意味着2017年 所以我想要2018年和2017年的记录

但是我无法得到你能告诉我在连接中出错的地方

想要结合这两个查询的输出

    select
count(*) [Total Clients], li.title,li.CI_UniqueID,coll.name,
SUM (CASE WHEN ucs.status=3 or ucs.status=1  then 1 ELSE 0 END ) as 'Installed / Not Applicable',
sum( case When ucs.status=2 Then 1 ELSE 0 END ) as 'Required',
sum( case When ucs.status=0 Then 1 ELSE 0 END ) as 'Unknown',
round((CAST(SUM (CASE WHEN ucs.status=3 or ucs.status=1 THEN 1 ELSE 0 END) as float)/count(*) )*100,2) as 'Compliant%',
    round((CAST(count(case when ucs.status not in('3','1') THEN '*' end) as float)/count(*))*100,2) as 'NotCompliant%'
    From v_Update_ComplianceStatusAll UCS
inner join v_r_system sys on ucs.resourceid=sys.resourceid
inner join v_FullCollectionMembership fcm on ucs.resourceid=fcm.resourceid
inner join v_collection coll on coll.collectionid=fcm.collectionid
inner join v_AuthListInfo LI on ucs.ci_id=li.ci_id
where coll.CollectionID='SMS00001' and
--title like '%SUG%' 
 Title like '%P1%' 
and Title like '%SUG_' + '' + CAST(year(getdate()) as varchar) +  '' + '%'
--and Title like '%SUG_' + '' + CAST(year(getdate())-1 as varchar) +  '' + '%'
group by li.title,li.CI_UniqueID,coll.name
order by li.title ASC


select
count(*) [Total Clients], li.title,li.CI_UniqueID,coll.name,
SUM (CASE WHEN ucs.status=3 or ucs.status=1  then 1 ELSE 0 END ) as 'Installed / Not Applicable',
sum( case When ucs.status=2 Then 1 ELSE 0 END ) as 'Required',
sum( case When ucs.status=0 Then 1 ELSE 0 END ) as 'Unknown',
round((CAST(SUM (CASE WHEN ucs.status=3 or ucs.status=1 THEN 1 ELSE 0 END) as float)/count(*) )*100,2) as 'Compliant%',
    round((CAST(count(case when ucs.status not in('3','1') THEN '*' end) as float)/count(*))*100,2) as 'NotCompliant%'
    From v_Update_ComplianceStatusAll UCS
inner join v_r_system sys on ucs.resourceid=sys.resourceid
inner join v_FullCollectionMembership fcm on ucs.resourceid=fcm.resourceid
inner join v_collection coll on coll.collectionid=fcm.collectionid
inner join v_AuthListInfo LI on ucs.ci_id=li.ci_id
where coll.CollectionID='SMS00001' and
--title like '%SUG%' 
 Title like '%P1%' 
-- Title like '%SUG_' + '' + CAST(year(getdate()) as varchar) +  '' + '%'
and Title like '%SUG_' + '' + CAST(year(getdate())-1 as varchar) +  '' + '%'
group by li.title,li.CI_UniqueID,coll.name
order by li.title ASC

2 个答案:

答案 0 :(得分:4)

year投放到varchar,因为年份返回int

select * from  v_AuthListInfo LI 
where title like '%SUG%' 
and Title like '%P1%' 
and Title like '%' + '' + CAST(year(getdate()) as varchar(4)) +  '' + '%'

答案 1 :(得分:2)

问题是year()返回一个数字,而不是一个字符串。因此,SQL Server将+解释为添加,而不是字符串连接。

SQL Server有一个方便的函数datename(),它返回一个字符串:

select *
from  v_AuthListInfo LI 
where title like '%SUG%' and
      title like '%P1%' and
      title like '%' + datename(year, getdate()) + '%';

您在like模式中连接的空字符串是无用的。