简体   繁体   English

在SQL中只获取字母数字行?

[英]get only alphanumeric rows in sql?

I've column with data 我有数据列

CLARK
KING
MILLER
SMITH, $800 .00
JONES, $2975.00
SCOTT, $3000.00
ADAMS, $1100.00
FORD, $3000.00
ALLEN30  
WARD30  
MARTIN30  
BLAKE30  
TURNER30  
JAMES30  

I'want only alphanumeric rows means 我只希望字母数字行表示

SMITH, $800 .00
JONES, $2975.00
SCOTT, $3000.00
ADAMS, $1100.00
FORD, $3000.00

except this rows all rows should be included? 除了这一行,所有行都应包括在内?

How can i get this? 我怎么能得到这个?

You could use PATINDEX : 您可以使用PATINDEX

DECLARE @test table(col varchar(30));
INSERT INTO @test
   SELECT * FROM (SELECT 'CLARK' UNION ALL SELECT 'KING' UNION ALL SELECT 'MILLER' UNION ALL SELECT 'SMITH, $800 .00' UNION ALL SELECT 'JONES, $2975.00' UNION ALL SELECT 'SCOTT, $3000.00' UNION ALL SELECT 'ADAMS, $1100.00' UNION ALL SELECT 'FORD, $3000.00' UNION ALL SELECT 'ALLEN30' UNION ALL SELECT 'WARD30' UNION ALL SELECT 'MARTIN30' UNION ALL SELECT 'BLAKE30' UNION ALL SELECT 'TURNER30' UNION ALL SELECT 'JAMES30')AS T(Spalte)

Patindex returns the starting position of the first occurrence of a pattern in a specified expression. Patindex返回指定表达式中模式首次出现的起始位置。 Since i've used a pattern that matches any non-alphanumeric ( ^ Wildcard - Character ), this returns only records which contain only alphanumeric characters (+ white-spaces): 由于我使用了与任何非字母数字( ^通配符-字符 )匹配的模式,因此仅返回仅包含字母数字字符(+空格)的记录:

SELECT col 
FROM   @test
WHERE  PATINDEX('%[^a-zA-Z0-9 ]%',col) = 0

Result: 结果:

CLARK
KING
MILLER
ALLEN30
WARD30
MARTIN30
BLAKE30
TURNER30
JAMES30
select * from your_table
where len((replace(your_col, '$', '')) != len(your_col)

Well, part of your problem is that the table design is poor - columns should be atomic and contain only one type of item. 好吧,您的问题的一部分是表设计不佳-列应该是原子的,并且仅包含一种类型的项目。 If the numeric were in a separate column (the correct design) you could use several techniques to return rows which had the numeric populated (eg WHERE NOT NULL, as a simple example). 如果数字位于单独的列中(正确的设计),则可以使用多种技术返回填充了数字的行(例如,作为简单示例的WHERE NOT NULL)。

In your case, assuming you don't want to change the table design, you could use a LIKE clause to find rows where the column contains '$'. 在您的情况下,假设您不想更改表设计,则可以使用LIKE子句查找列包含“ $”的行。 This is hugely inefficient though, and I suggest it without endorsing it. 但是,这是非常低效的,我建议不要认可它。

You can use a case statement similar to: 您可以使用类似于以下内容的case语句:

SELECT colname = CASE CaseName
    WHEN '%$%' THEN colname
    END
FROM tablename

I think table structure could be improved for speed, but given what we have... 我认为可以提高表结构的速度,但是鉴于我们所拥有的...

Select * from [table]
where [column] like '%$%'

or did you want the rows without the '$'? 还是您想要没有'$'的行?

Select * from [table]
where [column] not like '%$%'

this is of course assuming that you only want the rows with the dollar sign. 当然,这是假设您只想要带有美元符号的行。

If you were to have something like this: 如果您有这样的事情:

[Name] [Dollars]
SMITH   800.00

Then you could query like so: 然后,您可以像这样查询:

Select [Name], [Dollars]
From [table]
Where [Dollars] is not null
select col
from tab
where PATINDEX('%[^a-z0-9]%',col)=0

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

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