简体   繁体   English

错误在SQL中将%转换为数据类型int

[英]error converting % to datatype int in sql

iam using simple query i want year to get extracted and that should be used in table extraction IAM使用简单的查询,我想年提取,应该在表提取中使用

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

I am getting this error 我收到此错误

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

Ideally it should come 2018 and it should extract those records with 2018 and I want records minus 1 yr means 2017 so in all I want records of 2018 and 2017 理想情况下应该是2018年,并且应该提取2018年的记录,而我希望记录减去1年意味着2017年,所以我想要的是2018年和2017年的记录

but iam not able to get can you tell me where iam going wrong in concatenation 但Iam无法获得,您能告诉我Iam在串联时哪里出了问题

want to combine output of these two queries 想要合并这两个查询的输出

    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

Cast your year to varchar because year returns int value yearvarchar因为年份返回int

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

The problem is that year() returns a number, not a string. 问题是year()返回一个数字,而不是字符串。 Because of this, SQL Server interprets the + as addition, rather than string concatenation. 因此,SQL Server将+解释为加号,而不是字符串连接。

SQL Server has a convenient function, datename() , that returns a string: SQL Server有一个方便的函数datename() ,它返回一个字符串:

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

The empty strings that you are concatenating in the like pattern are useless. 您以like模式连接的空字符串是无用的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM