Find Varchar Column with Zeros Only





create TABLE test (a INT,b VARCHAR(30))

INSERT INTO test (a, b)
VALUES      (1,'0000'),
            (2,'000agsn86755jdd000'),
            (3,'000sfdbg0001226' ),
            (4,'0000000' ),
            (5,'0000000sh6820090' ),
            (6,'0000dhsg1190000' ),
            (7,'00000' ),
            (8, NULL),
            (9,'00000xb7826000'),
            (10,'00'),
            (11,'00828jxjk0000' ),
            (12,'0' ),
            (13,'00sd000877jsnj' ) 

 
 --Option 1

 --SQL Server 2012 or 2014
select *  from test
WHERE try_parse(b as int)=0

--Option 2
 select *  from test
where len(replace(B,'0',''))=0 
--and B is not null

drop table test



 

https://social.msdn.microsoft.com/Forums/en-US/0922b320-af7e-4dde-adff-f703deb04aba/query-to-find-all-zeroes-in-table?forum=transactsql

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s