black_belt
black_belt

Reputation: 6799

Mysql Query : between a number range when numbers have hyphens

I have a table like following, and I am trying to get all the values where reg_number is in between 01-01-02-000002 AND 01-01-02-000004

id  name               reg_number
1   Charlie Sheen      01-01-02-000001
2.  John Cryer         01-01-02-000002
3.  Roger Federar      02-02-02-000001
4.  Jason Stathum      01-01-02-000003
5.  Robert De Niro     01-01-02-000004 

I have tried the following code to achieve this but its not working. Is it because of the hyphens I have in reg_number? If it is could you please tell me the solution to this problem ? Thanks :)

SELECT * FROM mytable 
WHERE reg_number >='01-01-02-000002' AND reg_number <= '01-01-02-000004'

Upvotes: 0

Views: 4617

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1270011

I just built this on SQL Fiddle and it works.

The code is:

create table mytable (
      id int,
      name varchar(255),
      reg_number varchar(255)
  );

insert into mytable
    select 1, 'Charlie Sheen', '01-01-02-000001' union all
    select 2, 'John Cryer', '01-01-02-000002' union all
    select 3, 'Roger Federar', '02-02-02-000001' union all
    select 4, 'Jason Stathum', '01-01-02-000003' union all
    select 5, 'Robert De Niro', '01-01-02-000004'

and then your query.

Upvotes: 2

Alex Monthy
Alex Monthy

Reputation: 1877

How is the code not working? Which result do you get? What works for me is:

set @reg_number = '01-01-02-000003'; 
select @reg_number >='01-01-02-000002' AND @reg_number <= '01-01-02-000004'; 

which results to true.

Upvotes: 1

Related Questions