Click here to Skip to main content
15,890,946 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a table billing and there from i need to search data in which invoice_no(its column name)should begin with a letter between a to e( a,b,c,d or e) and not beyond e.

here is the code i am trying to execute.
select COALESCE(sum(payable_amount),0.00) from billing where bill_date = '2013/12/05' and pay_mode = 'Cash' AND invoice_no  like '[a-eA-E]%'


billing table has data having invoice_no that begins with a,b and c as well. But this query returns me zero(0).

Is there any mistake in code writing. Plz help.
Posted
Updated 6-Jan-14 1:15am
v2

The LIKE operator in MySQL is not that versatile I'm afraid. There are a number of alternatives you can sue though:

SQL
select COALESCE(sum(payable_amount),0.00) from billing where bill_date = '2013/12/05' and pay_mode = 'Cash' AND ASCII(invoice_no) in (65,66,67,68,69)


or

SQL
select COALESCE(sum(payable_amount),0.00) from billing where bill_date = '2013/12/05' and pay_mode = 'Cash' AND LEFT(invoice_no, 1) in ('A','B','C','D','E')


Both of these will only match against the uppercase characters - you'll ned to add the relevant codes if you want to match lowercase as well.

There is also the REGEXP function, but damned if I can understand it ro get it to work,,,
http://dev.mysql.com/doc/refman/5.0/en/regexp.html#operator_regexp[^]
 
Share this answer
 
v3
Learn this, it will solve your current problem and also future problem.
Regular Expression[^]
 
Share this answer
 
Comments
Wombaticus 7-Jan-14 4:32am    
I don't find the examples on that page the least bit enlightening. I'd be interested to see how you would use regexp in this case - that might help me learn how to use it!
[no name] 8-Jan-14 5:58am    
where invoice_no REGEXP '[abcde]';
Wombaticus 8-Jan-14 6:28am    
hmm.. don't think it's quite as simple as that, unfortunately. I just tried this on a table of my own and it returned all rows with an A,B,C,D or E somewhere within the target field, not just those beginning with one of these. I then tried
REGEXP '[ABCDE]*'
but still with no luck....
[no name] 8-Jan-14 7:08am    
we can use second solution give by womb. here is the syntax i have tried, and it works fine.f_name is target field.

SELECT * FROM employee_member where LEFT(f_name, 1) in ('B');
[no name] 8-Jan-14 7:08am    
we can use second solution give by womb. here is the syntax i have tried, and it works fine.f_name is target field.

SELECT * FROM employee_member where LEFT(f_name, 1) in ('B');
select COALESCE(sum(payable_amount),0.00) from billing where bill_date = '2013/12/05' and pay_mode = 'Cash' and invoice_no REGEXP '[abcde]';
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900