【问题标题】:sql server select concatenate with like?sql server选择与like连接?
【发布时间】:2012-01-31 13:51:34
【问题描述】:

我正在尝试选择多个列值并将它们连接到一列中。然后,我想在选择值的列上运行 LIKE。但是它似乎不起作用。

这是我的 SQL 查询。

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

这个查询有什么问题?有没有更好的方法来做到这一点?

【问题讨论】:

    标签: sql sql-server sql-server-2008 select


    【解决方案1】:
    SELECT 
        col1 + ' ' + col2 + ' ' + col3 AS colname 
    FROM tblname 
    WHERE (col1 + ' ' + col2 + ' ' + col3) LIKE 'test%'
    

    或者

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

    这是因为列别名不能在WHERE 子句中使用。您要么需要明确明确别名定义的内容,要么使用子查询来抽象别名。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-20
      • 2010-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-27
      相关资源
      最近更新 更多