Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 check for spaces

Author  Topic 

jassie
Constraint Violating Yak Guru

332 Posts

Posted - 2012-10-06 : 16:22:23
In a sql server 2008 r2 database column that is defined as varchar, how do you check to see if the field contains all spaces regardless of the length the field is?

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-10-06 : 18:39:31
A few different ways you can do this, see in red below:
DECLARE @x VARCHAR(32) = '         ';
SELECT
CASE WHEN LTRIM(@x) = '' THEN 'All spaces' ELSE 'Not all spaces' END ,
CASE WHEN LEN(@x) = 0 THEN 'All spaces' ELSE 'Not all spaces' END ,
CASE WHEN REPLACE(@x,' ','') = '' THEN 'All spaces' ELSE 'Not all spaces' END ,
CASE WHEN NULLIF(@x,'') IS NULL THEN 'All spaces' ELSE 'Not all spaces' END ;
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-10-07 : 13:08:34
one more way

DECLARE @x VARCHAR(32) = ' ';
SELECT
CASE WHEN @x > '' THEN 'Not all spaces' ELSE 'All spaces' END


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -