Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL query - filter out field containing only spaces

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 ?

like image 311
user4045430 Avatar asked Jan 10 '23 19:01

user4045430


2 Answers

SELECT 
      MEMO_SYSTEM_TXT
FROM [EE].[dbo].[EE_Billing_Memo]
WHERE 
        MEMO_SYSTEM_TXT IS NOT NULL 
    AND LTRIM(MEMO_SYSTEM_TXT) <> ''
like image 160
Max Avatar answered Jan 19 '23 03:01

Max


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 <> ''
like image 21
t-clausen.dk Avatar answered Jan 19 '23 01:01

t-clausen.dk