使用LIKE条件获取值但如何获取非值

时间:2015-12-24 09:29:43

标签: sql sql-server tsql

我有两张桌子,例如:

CREATE TABLE Names 
(name varchar(1),
Surname Varchar(1))


CREATE TABLE FullNames 
(Fullname varchar(1),
Surname Varchar(1))

现在我正在检查Information_Schema.columns以获取列名

select 
TABLE_NAME,
COLUMN_NAME 
from INFORMATION_SCHEMA.columns 
where 
COLUMN_NAME like '%name%' 

TABLE_NAME  COLUMN_NAME
Names           name
Names           Surname
FullNames       Fullname
FullNames       Surname

但我使用其他类似条件

where COLUMN_NAME like '%name%' or COLUMN_NAME like '%Noname%'

然后我想得到结果

  TABLE_NAME    COLUMN_NAME
    Names           name
    Names           Surname
    FullNames       Fullname
    FullNames       Surname
    NULL             NULL

你能帮忙吗?

3 个答案:

答案 0 :(得分:1)

如果要查找列没有名称的位置:

SELECT
    TABLE_NAME,
    COLUMN_NAME 
FROM
    INFORMATION_SCHEMA.columns 
WHERE
    COLUMN_NAME like '%name%'
OR
    COLUMN_NAME IS NULL;

或者,如果您只想在NULLCOLUMN_NAME类似的位置添加'NoName'行,请使用CASE

SELECT
    CASE WHEN COLUMN NAME LIKE '%NoName%' THEN NULL ELSE TABLE_NAME END,
    CASE WHEN COLUMN NAME LIKE '%NoName%' THEN NULL ELSE COLUMN_NAME END 
FROM
    INFORMATION_SCHEMA.columns 
WHERE
    COLUMN_NAME like '%name%'
OR
    COLUMN_NAME like '%NoName%';

答案 1 :(得分:0)

您是否要查找具有空列名称的表?甚至可以发生吗?类似的东西:

where COLUMN_NAME like '%name%' or COLUMN_NAME is null

答案 2 :(得分:0)

和其他人一样,我不是百分之百确定你的意思,所以这里是快速刺伤.....

SELECT 
  TABLE_NAME, COLUMN_NAME 
FROM INFORMATION_SCHEMA.columns 
WHERE 
  COLUMN_NAME like '%name%'
UNION ALL
SELECT TABLE_NAME,COLUMN_NAME 
FROM INFORMATION_SCHEMA.columns 
WHERE 
  COLUMN_NAME like '%noname%'
UNION ALL 
SELECT NULL, NULL
WHERE 
  NOT EXISTS( SELECT 1 FROM INFORMATION_SCHEMA.columns WHERE COLUMN_NAME like '%noname%' )