?
Solved

Microsoft SQL - Count varchar where max len = n

Posted on 2014-02-06
10
Medium Priority
?
597 Views
Last Modified: 2014-02-06
Hello -

Hopefully an easy one for you.  I have a varchar(7) field in which the majority of the values are 7 digit numbers ('#######').  

I'm trying to count the number of records that have a "valid" 7 digit number value in them.  

I tried:

SELECT COUNT(myField) FROM myTable HAVING MAX(LEN(myField)) ='7'
That returned the count of all records in my table.  I know there are some blanks and commas and things in there so I know that's not right - Maybe I should use rtrim/ltrim?  

Then after a little research I found "DATALENGTH" and tried:

SELECT COUNT(myField) FROM myTable WHERE DATALENGTH(myField) ='7'
That returned a count smaller than the total record count (more than half).  This seems realistic but I'm not sure if I'm getting what I'm asking for.  

What would be the best way to go about this?

Thanks!!!
0
Comment
Question by:ttist25
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 16

Assisted Solution

by:Surendra Nath
Surendra Nath earned 500 total points
ID: 39839447
ok you can try the below one

select count(1) from myTable where len(myField) = 7

Open in new window

0
 
LVL 25

Assisted Solution

by:Lee Savidge
Lee Savidge earned 500 total points
ID: 39839469
SELECT COUNT(myField) FROM myTable where LEN(myField) =7

Open in new window

0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39839511
>I'm trying to count the number of records that have a "valid" 7 digit number value in them.
Define 'valid'.  Seven characters, all numbers?
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 500 total points
ID: 39839520
SELECT COUNT(myField) FROM myTable WHERE myfield like '[0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
0
 
LVL 66

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 39839534
CREATE TABLE #foo (myField varchar(7) )

INSERT INTO #foo (myField) 
VALUES 
   ('1234567'), ('123,567'), ('  12345'), ('12345  '), ('1234567'), ('(23456)'),
   ('42'), ('one'), (NULL), ('BR549'), ('OU812'), ('foo')

-- Here's your seven numbers
SELECT * FROM #foo WHERE myField LIKE '[0-9][0-9][0-9][0-9][0-9][0-9][0-9]'

-- Here's everything else
SELECT * FROM #foo WHERE myField NOT LIKE '[0-9][0-9][0-9][0-9][0-9][0-9][0-9]'

Open in new window

0
 
LVL 1

Author Closing Comment

by:ttist25
ID: 39839643
Thanks for the responses everyone.  I always forget how powerful "LIKE" can be.  

Anyway - I ended up using a combination of LEN(myField) = 7 and NOT LIKE '[0-9] ...' which may redefine what I consider a "valid" value to be (this is all discovery stage analysis).  

Thanks again everyone.  Always good to know you're there!
0
 
LVL 1

Author Comment

by:ttist25
ID: 39839645
Jim!  VH fan????  :D
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39839669
Yes, and I have a couple of sample data sets that use as customers Sammy Hagar, Eddie Van Halen, Joe Satriani, etc.
0
 
LVL 1

Author Comment

by:ttist25
ID: 39839723
Hah!  That's great.  I'm a player myself and have recently started building pedals for fun.  My standard test for the sound quality of a prototype pedal has become the opening bars from Drop Dead Legs.  

Ok - that's probably enough off-topic garbage but I had to acknowledge the connection.
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39839739
In that case check out this question but do me a favor and don't answer it right away, as I want to see how the other yahoos do.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Suggested Courses

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question