Craig Zirnheld
Craig Zirnheld

Reputation: 139

How do I do a SQL query that finds numbers in a field that is only 6 characters long and starts with the number 5?

See the following code:

select no_
from dbo.customer
where no_ like '5%%%%%'

Upvotes: 1

Views: 2525

Answers (3)

Kostya
Kostya

Reputation: 1605

i would also add isnumeric

select no_ from dbo.customer where no_ like '5_____' and ISNUMERIC(no_) = 1

Upvotes: 0

krokodilko
krokodilko

Reputation: 36087

Use:

select no_ from dbo.customer where no_ like '5_____'

LIKE operator recognizes two wildcard characters :

  • % - The percent sign represents zero, one, or multiple characters
  • _ - The underscore represents a single character

Upvotes: 3

Gordon Linoff
Gordon Linoff

Reputation: 1269503

Well, you would do:

 select no_ from dbo.customer where no_ like '5%'

% is a wildcard that matches any number of characters, including none.

If you want to guarantee exactly 6 characters as well:

 select no_ from dbo.customer where no_ like '5_____'

_ is a wildcard that matches exactly one character.

Upvotes: 1

Related Questions