SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 check for spaces
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

jassie
Constraint Violating Yak Guru

308 Posts

Posted - 10/06/2012 :  16:22:23  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 10/06/2012 :  18:39:31  Show Profile  Reply with Quote
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

India
52325 Posts

Posted - 10/07/2012 :  13:08:34  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000