[英]How to return same data in different column?
I have few columns as below: 我的栏目如下:
column1 column2 column3 column4
ID123 Apple Red Apple
ID456 Apple Blue Apple
ID987 Pear Blue Apple
ID899 Pear Blue Apple
I wanted to pull rows where column2 = column4
as below: 我想拉出
column2 = column4
行,如下所示:
column1 column2 column3 column4
ID123 Apple Red Apple
ID456 Apple Blue Apple
I tried: 我试过了:
select column1,column2,column3,column4
where column2=column4
But no result are returned. 但是没有结果返回。
Where is my mistake? 我的错误在哪里?
As Recommend to you if the collation is Case Sensitive then you should make the columns all the same by using either UPPER or LOWER i would also do a replace for any spaces. 根据您的建议,如果排序规则区分大小写,则应使用UPPER或LOWER来使列相同,我也将替换任何空格。
CREATE TABLE #temp1
(
column1 NVARCHAR(255),
column2 NVARCHAR(255),
column3 NVARCHAR(255),
column4 NVARCHAR(255)
)
INSERT INTO #temp1 VALUES('ID123','Apple','Red',' Apple')
INSERT INTO #temp1 VALUES('ID456','Apple','Blue','Apple')
INSERT INTO #temp1 VALUES('ID987','Pear','Blue','Apple')
INSERT INTO #temp1 VALUES('ID899','Pear','Blue','Apple')
SELECT * FROM #temp1
WHERE REPLACE(UPPER(column2), ' ', '' ) = REPLACE(UPPER(column4), ' ', '' )
drop table #temp1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.