How to define range in where clause? [message #613401] |
Tue, 06 May 2014 06:49 |
|
indrarai
Messages: 2 Registered: May 2014 Location: Nepal
|
Junior Member |
|
|
Dear All,
I want to define the range in where command like:
WHERE citizenship_cd='64-01-70-04592' to '64-01-70-04670'
then how can i define in sql command.
Please help.
Indra
|
|
|
|
|
|
|
Re: How to define range in where clause? [message #613410 is a reply to message #613406] |
Tue, 06 May 2014 07:29 |
|
Maaher
Messages: 7065 Registered: December 2001
|
Senior Member |
|
|
How does this work for you?
With yourtable as
( Select 1 col_1, '64-01-70-04590' citizenship_cd from dual union all -- rejected
Select 2 col_1, '64-01-70-04592' citizenship_cd from dual union all
Select 3 col_1, '64-01-70-04594' citizenship_cd from dual union all
Select 4 col_1, '64-01-70-04596' citizenship_cd from dual union all
Select 5 col_1, '64-01-70-04710' citizenship_cd from dual union all -- rejected
Select 6 col_1, '64-01-70-04612' citizenship_cd from dual union all
Select 7 col_1, '64-01-70-04814' citizenship_cd from dual union all -- rejected
Select 8 col_1, '64-01-70-04616' citizenship_cd from dual
)
Select col_1
, citizenship_cd
From yourtable
Where substr(citizenship_cd, -5) between '04592' and '04670'
/
SUBSTR
MHE
|
|
|
|
Re: How to define range in where clause? [message #613413 is a reply to message #613410] |
Tue, 06 May 2014 07:39 |
Solomon Yakobson
Messages: 3273 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
Are you sure?
Your code will select '64-01-70-04592', '68-01-70-04592', '64-01-99-04592', etc. while OP wants '64-01-70-04592' to '64-01-70-04670'.
I'd say:
citizenship_cd between '64-01-70-04592' and '64-01-70-04670'
SY.
P.S. I assume citizenship_cd always has format XX-XX-XX-XXXXX where X is any digit.
[Updated on: Tue, 06 May 2014 07:41] Report message to a moderator
|
|
|
Re: How to define range in where clause? [message #613414 is a reply to message #613413] |
Tue, 06 May 2014 07:43 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
Solomon Yakobson wrote on Tue, 06 May 2014 18:09Your code will select '64-01-70-04592', '68-01-70-04592', '64-01-99-04592', etc.
OP mentioned that "64-01-70-" will not change :
indrarai wrote on Tue, 06 May 2014 17:32"64-01-70-" which is static, only last five digits (04592) dynamic.
From design perspective, the data should be first normalized.
[Updated on: Tue, 06 May 2014 07:45] Report message to a moderator
|
|
|
|
|
|
Re: How to define range in where clause? [message #613418 is a reply to message #613416] |
Tue, 06 May 2014 07:51 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
Solomon Yakobson wrote on Tue, 06 May 2014 18:16I am 99.9% sure OP meant starts with '64-01-70' with last 5 digits changing from 04592 to 04592.
Perhaps you are 100% correct. It's the way OP has explained in words - "static". I assume it is always same for all rows. I wish OP posted a test case.
|
|
|
Re: How to define range in where clause? [message #613419 is a reply to message #613418] |
Tue, 06 May 2014 07:54 |
Solomon Yakobson
Messages: 3273 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
Well, it is possible but hard to believe ALL citizenship codes start with '64-01-70', but if they do storing '64-01-70' is the database is complete waste. Anyway, I think we dug in too deep into assumptions - we have to wait for OP for clear explanations.
SY.
|
|
|
Re: How to define range in where clause? [message #613420 is a reply to message #613415] |
Tue, 06 May 2014 07:56 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
Maaher wrote on Tue, 06 May 2014 18:13 @Lalit: I have encountered enough examples of bad design throughout the years and in most cases there was no opportunity to change that.
I agree. Some, few, many, most of us have certainly encountered such examples. But at a later stage we end up with more issues and spend more time finding alternatives to fix it. It is a suggestion, rest depends on OP whether or not to implement it.
|
|
|