Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

BETWEEN operator for Varchar field Expand / Collapse
Author
Message
Posted Friday, May 07, 2010 1:09 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, May 17, 2013 5:07 AM
Points: 162, Visits: 291
Hi All,

I've a table name consumption_detail

CREATE TABLE [dbo].[cunsumption_detail](
[form38_no] [varchar](15) NULL,
[isused] [bit] NULL,
[used_Date] [datetime] NULL,
[Demand_No] [varchar](14) NULL,
[forConsumptionReport] [bit] NOT NULL
)

Column Information from sp_help
Column_name	Type	Computed	Length	Prec	Scale	Nullable	TrimTrailingBlanks	FixedLenNullInSource	Collation
form38_no varchar no 15 yes no yes SQL_Latin1_General_CP1_CI_AS


PROBLEM:

I've been trying to get a result set showing me form38_no between AA-14 to AA-110
Used the following query:

DECLARE @form_prefix varchar(5), @form_No1 varchar(10), @form_No2 varchar(10)
SET @form_prefix = 'AA'
SET @form_No1 = 11
SET @form_No2 = 110

Select * FROM cunsumption_detail
WHERE RTRIM(LTRIM(form38_no)) BETWEEN @form_prefix+'-'+@form_No1 and @form_prefix+'-'+@form_No2
AND CAST(REVERSE(LEFT(REVERSE(form38_no),CHARINDEX('-',REVERSE(form38_no))-1)) as INT)>=@form_No1
AND CAST(REVERSE(LEFT(REVERSE(form38_no),CHARINDEX('-',REVERSE(form38_no))-1)) as INT)<=@form_No2
ORDER BY SUBSTRING(form38_no,0,CHARINDEX('-',form38_no)) ASC,
CAST(REVERSE(LEFT(REVERSE(form38_no),CHARINDEX('-',REVERSE(form38_no))-1)) as INT) ASC

Unfortunately, am not getting the desired result. It just brings out two results having AA-11 and AA-110 form numbers.

If I remove my first where condition and then run the query it shows all the results BUT shows some forms having form_prefix of AAM. Sample Result set is as below.
form38_no	isused	used_Date	Demand_No	forConsumptionReport
AAM-106 0 1/18/10 3:01 PM 1018999000014 0
AAM-108 0 1/18/10 7:56 PM 1018999000014 0
AA-77 0 1/19/10 1:14 PM NULL 0
AA-78 0 1/19/10 1:15 PM NULL 0
AA-79 0 1/19/10 1:23 PM NULL 0
AA-80 0 1/19/10 1:24 PM NULL 0
AA-76 0 1/19/10 1:30 PM NULL 0
AA-33 0 1/20/10 11:16 AM NULL 0

Can someone help me write the correct query which'll fetch me the result set I want.

I want only AA series form numbers between 11 and 110.

SQL Statements have been attached.

Thanks
Ankit Mathur


  Post Attachments 
SQL Statements.rar (1 view, 1.45 KB)
Post #917667
Posted Friday, May 07, 2010 3:33 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 3:48 AM
Points: 3,125, Visits: 4,311
Try the following:
Select *
FROM cunsumption_detail
WHERE LEFT(form38_no, CHARINDEX('-',form38_no)-1) = @form_prefix
AND CONVERT(INT, RIGHT(RTRIM(form38_no), LEN(form38_no) - CHARINDEX('-',form38_no))) BETWEEN @form_No1 AND @form_No2
ORDER BY form38_no



____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
Post #917742
Posted Friday, May 07, 2010 4:39 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, May 17, 2013 12:14 PM
Points: 125, Visits: 498
Hope this helps...

select * from cunsumption_detail
where
(form38_no like ('AA-[1-9][0-9]') and form38_no not like ('AA-10'))
or
form38_no like ('AA-10[0-9]') and form38_no like ('AA-110')


Cheers,
Ankur
Post #917784
Posted Friday, May 07, 2010 6:26 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, May 17, 2013 5:07 AM
Points: 162, Visits: 291
Thanks Stewart,

Your solution worked like a charm. But if I'm not bothering much can you guide me where I was wrong in my trials.

Ankur: Thanks to you too. Your solution also worked but I was looking for a more universal query so Stewart's query helps me more.

Thanks to both of you once again.

Ankit Mathur
Post #917865
Posted Friday, May 07, 2010 7:10 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 3:48 AM
Points: 3,125, Visits: 4,311
Ankit Mathur-481681 (5/7/2010)
But if I'm not bothering much can you guide me where I was wrong in my trials.


remember, when using a predicate operator like "between" on character data types, SQL uses a list of the ASCII codes to determine which to sort where and in what order.
using this logic, "AA-10" is followed directly by "AA-110", then e.g. "AA-2" and "AA-3" et cetera.
that is why the items requested did not appear in the output.

This ofttimes delivers unexpected result sets.
in such a case, it does make sense to split the column (in this case using the "-" character as the marker.
However, if the table you need to execute this on becomes large (millions of rows), performance will suffer noticably, as full table scans will be effected.


____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
Post #917918
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse