Avatar of Queennie L
Queennie L

asked on 

Find and Count Leading Zeros in Column

Hello Experts,

How to search for values in a column that has leading zeros? Count them?

I know this is just simple to experts but I searched but cannot find any answers.

See attach file.

Thank you.
FindandCountLeadingZeros_inValue.xlsx
Microsoft SQL ServerSQL

Avatar of undefined
Last Comment
Queennie L
Avatar of lcohan
lcohan
Flag of Canada image

Not quite clear where (SQL Server database table column? what data type?) do you need to count them and until you provide more details here's how you can do that in excel:

https://www.extendoffice.com/documents/excel/4963-excel-count-leading-zeros.html
Avatar of Queennie L
Queennie L

ASKER

@lcohan:

This is Microsoft SQL  Server not Excel. I apologized I was not clear in my question.

Thank you for your help.
Try this:
select checknumber, patindex('%[^0]%', checknumber)-1 from your_table;

Open in new window

If they can be all 0:
select checknumber, case when mycol > 0 then mycol else len(checknumber)end zero_count from (
select checknumber, patindex('%[^0]%', checknumber)-1 as mycol from your_table)
newtable

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Pavel Celba
Pavel Celba
Flag of Czechia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of _agx_
_agx_
Flag of United States of America image

@pcelba - Nice, much simpler than mine.
>>@pcelba - Nice, much simpler than mine.

And oddly similar to my new one...  I just found the inline view cleaner
Avatar of Pavel Celba
Pavel Celba
Flag of Czechia image

Yes, very similar...
Avatar of Queennie L
Queennie L

ASKER

@slightwv (䄆 Netminder):

The query is counting the length of the variables.

@pcelba:

WOW! Your query works. I will do more test and I will let you know.

Thank you again.
SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Queennie L
Queennie L

ASKER

Thank you Experts for your sharing your expertise and intelligence.

I really appreciate it.

Thank you.
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo