简体   繁体   English

sql server选择与之串联吗?

[英]sql server select concatenate with like?

I am trying to select multiple column values and concatenate them into one column. 我正在尝试选择多个列值并将它们连接到一列中。 I then want to run a LIKE on the column that the values are selected as. 然后,我想在将值选择为的列上运行LIKE。 However it does not seem to work. 但是,它似乎不起作用。

Here is my SQL query. 这是我的SQL查询。

SELECT col1 + ' ' + col2 + ' ' + col3 AS colname
FROM tblname
WHERE 'colname' LIKE 'test%'

What is wrong with this query? 此查询有什么问题? Is there a better way to do this? 有一个更好的方法吗?

SELECT 
    col1 + ' ' + col2 + ' ' + col3 AS colname 
FROM tblname 
WHERE (col1 + ' ' + col2 + ' ' + col3) LIKE 'test%'

Or 要么

select *
from
(
    SELECT 
        col1 + ' ' + col2 + ' ' + col3 AS colname 
    FROM tblname 
)a
WHERE colname LIKE 'test%'

This is because column aliases cannot be used in a WHERE clause. 这是因为无法在WHERE子句中使用列别名。 You either need to explicitly right out what the alias defines, or use a subquery to abstract the alias. 您要么需要明确指出别名定义的内容,要么使用子查询来抽象别名。

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

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