Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Regular Expression and BETWEEN in same query

Author  Topic 

kushalagarwal
Starting Member

1 Post

Posted - 2010-07-30 : 06:54:44
Hello,
Could anyone please tell me if there is a way to write Regular Expressions and Between in the same query?
I have a table with a column orderNumber.
The orderNumber has preceeding 0's and .'s something like
"00000034500.1.2.1.1".
There are two input textboxes according to whose values search queries should be fired.
What I would like to do is:
Lets say there are 3 orders with orderNumber "00000034500" ,"00000034600" and "00000034700".
1) If 1 textbox is entered with value "345" and the other is blank I would like to search for the order "00000034500.1.2.1.1"
I am pretty new to DB's, but I suppose I can use a regular expression (REGEX_LIKE) to search this order number(Please correct me if I am wrong).

2) If both textboxes are entered with values "345" and "347"
I would like it to search for all 3 orders.

Also, the length of the orderNumbers is not fixed
Is it possible to do this?
Thanks in advance,
Cheers,
Kushal

jen
Master Smack Fu Yak Hacker

4110 Posts

Posted - 2010-08-17 : 09:06:43
your sample data is vague...but here is something that you can play around with...


create table table1(col1 varchar(100))

insert into table1
select '00000034500.1.2.1.1'
union
select '00000034600.1.2.1.1'
union
select '00000034700.1.2.1.1'


declare @text1 int
declare @text2 int

set @text1=345
set @text2=346

select *
from table1
where left(replace(col1,'0',''),patindex('%.%',replace(col1,'0',''))-1) between @text1 and coalesce(@text2,@text1)

drop table table1

--------------------
keeping it simple...
Go to Top of Page
   

- Advertisement -