如何在ms sql中使用concat函数where条件与like

时间:2013-09-30 10:53:59

标签: sql-server concatenation sql-like trim concat

我在Oracle中使用以下查询得到了正确的输出。

SELECT COUNT(*) AS CNT FROM MSS WHERE TRIM(UPPER(FIRSTNAME)|| UPPER(SECONDNAME)) LIKE UPPER('qw1')||UPPER('121212')

但是我需要查询MS SQL Server,任何帮助都会受到赞赏..

2 个答案:

答案 0 :(得分:0)

尝试

SELECT COUNT(*) AS CNT FROM MSS WHERE LTRIM(RTRIM(UPPER(FIRSTNAME) + UPPER(SECONDNAME))) LIKE UPPER('qw1') + UPPER('121212')

但是我不确定你是否不需要比较任何有类似情况的东西:

SELECT COUNT(*) AS CNT FROM MSS WHERE LTRIM(RTRIM(UPPER(FIRSTNAME) + UPPER(SECONDNAME))) LIKE '%' + UPPER('qw1') + UPPER('121212') + '%'

否则,您可以在第一个查询中使用=

SELECT COUNT(*) AS CNT FROM MSS WHERE LTRIM(RTRIM(UPPER(FIRSTNAME) + UPPER(SECONDNAME))) = UPPER('qw1') + UPPER('121212')

答案 1 :(得分:0)

1.在创建表格MSS时,确保名称(整理)和 确保将varchar或nvarchar用于名称。

示例:

CREATE TABLE MSS
(
firstname nvarchar(50) collate Latin1_General_CI_AS,
lastname nvarchar(50) collate Latin1_General_CI_AS
)

2.填充表格时,MSS确保名称前没有空格。

示例:

declare @val1 nvarchar(50) = ' qw1 '
declare @val2 nvarchar(50) = ' 121212 '
insert MSS(firstname, lastname)
values(ltrim(@val1), ltrim(@val2))

3.制作更新查询以修复当前数据,使名称不以空格开头。

示例:

UPDATE MSS
SET FIRSTNAME = LTRIM(FIRSTNAME),
SECONDNAME = LTRIM(SECONDNAME)
WHERE FIRSTNAME <> LTRIM(FIRSTNAME)
or SECONDNAME <> LTRIM(SECONDNAME)

4.写你的查询。

示例:

SELECT COUNT(*) AS CNT 
FROM MSS
WHERE FIRSTNAME like 'qw1'
and SECONDNAME like '121212'

请在比较前停止查询列,这会降低性能