SQL query - filter out field containing only spaces

北城以北 提交于 2019-12-12 12:13:56

问题


I need to write a sql query that filters out rows that have a changing number of spaces in a field. For example, I have this query

   SELECT MEMO_SYSTEM_TXT
   FROM [EE].[dbo].[EE_Billing_Memo]
   where MEMO_SYSTEM_TXT is not null and MEMO_SYSTEM_TXT <> '' and MEMO_SYSTEM_TXT <>  ' '

I found out that the field MEMO_SYSTEM_TXT might contain different number of spaces, so my restrictions are not sufficient. Anyone have a robust where cluase that will filter out all spaces at once ?


回答1:


SELECT 
      MEMO_SYSTEM_TXT
FROM [EE].[dbo].[EE_Billing_Memo]
WHERE 
        MEMO_SYSTEM_TXT IS NOT NULL 
    AND LTRIM(MEMO_SYSTEM_TXT) <> ''



回答2:


several spaces will always equal empty string

SELECT 1
WHERE 
  'a' = 'a ' and
  'a' = 'a  ' and 
  '' = '   ' and
  cast('' as char(1)) = cast('   ' as char(5))

Returns 1 since they are all equal

So all you have to do is this:

SELECT MEMO_SYSTEM_TXT
FROM [EE].[dbo].[EE_Billing_Memo]
WHERE MEMO_SYSTEM_TXT is not null and MEMO_SYSTEM_TXT <> ''



回答3:


The easiest way is to replace spaces by empty strings, and check the string length, i.e. express your condition like this:

AND LEN(REPLACE(MEMO_SYSTEM_TXT , ' ', '')) = 0

This will find all the empty strings and strings composed of any number of spaces:

'', ' ', '  ', '   '...

so this can replace your original expression:

 and MEMO_SYSTEM_TXT <> '' and MEMO_SYSTEM_TXT <> ' '

and all the rest of MEMO_SYSTEM_TXT <> ' ' that you'd have to include.




回答4:


This will filter out MEMO_SYSTEM_TXT which are not null or empty string and do not contain any space.

SELECT MEMO_SYSTEM_TXT
FROM [EE].[dbo].[EE_Billing_Memo]
WHERE 
    MEMO_SYSTEM_TXT IS NOT NULL
    AND MEMO_SYSTEM_TXT <> ''
    AND MEMO_SYSTEM_TXT NOT LIKE '% %'



回答5:


You can use RegEx-like condition, however the supported functionality is very limited in native T-SQL.

SELECT
  MEMO_SYSTEM_TXT
FROM
  [EE].[dbo].[EE_Billing_Memo]
WHERE 
    MEMO_SYSTEM_TXT LIKE N'%[^ ]%'

Example values

SELECT
    *
FROM
    (VALUES (''), ('  '), (' '), ('x y'), (' x'), ('x ')) AS A(txt)
WHERE
    txt LIKE N'%[^ ]%'

If this is not a on-time query, update the data stored in the column, update all records to remove all space-only values (update them to NULL or empty string).

You can add a CHECK constraint for the column to prevent newly created 'empty' records.

Updating all 'empyt' values to NULL

UPDATE
  [EE].[dbo].[EE_Billing_Memo]
SET
  MEMO_SYSTEM_TXT = NULL
WHERE
  MEMO_SYSTEM_TXT LIKE N'%[^ ]%'

The CHECK constraint to add:

CONSTRAINT CK_PreventEmpty_MEMO_SYSTEM_TXT
  CHECK MEMO_SYSTEM_TXT LIKE N'%[^ ]%'

An alternative solution is to add an INSTEAD OF trigger (INSERT and UPDATE) to prevent 'empty' values in MEMO_SYSTEM_TXT, OR you can create an indexed view which is not contains the 'empty' texts.




回答6:


Try the code below:

SELECT MEMO_SYSTEM_TXT
FROM [EE].[dbo].[EE_Billing_Memo]
WHERE MEMO_SYSTEM_TXT IS NOT NULL AND ASCII(REPLACE(MEMO_SYSTEM_TXT, ' ', '')) != 10

after replacing the spaces with '',only one '' remains and the ascii code of '' is 10




回答7:


You can use regex expressions in SQL. Find the regex expression that matches what you want to detect and apply it directly in the WHERE clause.



来源:https://stackoverflow.com/questions/25863722/sql-query-filter-out-field-containing-only-spaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!