Click here to Skip to main content
15,890,670 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i Have table as follows

Qty1 | Qty2 | Rate
________________________
1 | 5 | 900
6 | 10 | 800
11 | 19 | 600
_______________________


Now i need to find rate of value 1 of qty, means is it between qty1 and qty2 i.e. between (1 to 5 or 6 to 10 and so on... )
Posted
Updated 27-Aug-13 0:46am
v2
Comments
Nelek 27-Aug-13 7:02am    
hareshdgr8 27-Aug-13 7:04am    
i tried select rate from tablename where qty1>=1 and qty2<=1 but it is wrong i know that . what can i do for this type of query.
gvprabu 27-Aug-13 8:03am    
If possible change the DB design.... :-)

Hi,

As per your DB design.... Its difficult to do like this operation. If possible change the single column to 3 columns like Qty1,Qty2 and Rate.
Try this below code....
SQL
DECLARE @Test AS TABLE (Data VARCHAR(200)) 
DECLARE @Qty1 INT , @Qty2 INT

SELECT @Qty1=6,@Qty2=10

INSERT INTO @Test (Data) 
SELECT '1 | 5 | 900'
UNION ALL SELECT '6 | 10 | 800'
UNION ALL SELECT '11 | 19 | 600'

SELECT Data FROM @Test

SELECT LTRIM(RTRIM(M.Qty1)) 'Qty1', 
	   LTRIM(RTRIM(M.Qty2)) 'Qty2', 
	   SUBSTRING(M.Rate,CHARINDEX('|',M.Rate,1)+1,LEN(M.Rate)) 'Rate'
FROM (SELECT SUBSTRING(Data,1,CHARINDEX('|',Data,1)-1) 'Qty1',
	         SUBSTRING(Data,CHARINDEX('|',Data,1)+1,CHARINDEX('|',Data,CHARINDEX('|',Data,1)+1)-CHARINDEX('|',Data,1)-1) 'Qty2',
	         SUBSTRING(Data,CHARINDEX('|',Data,1)+1,LEN(Data)) 'Rate'
	  FROM @Test) M
WHERE M.Qty1 = @Qty1 AND M.Qty2 =@Qty2 

Note : same this we can use UDF for split process and do like this logic.

SQL
-- Logic 2
DECLARE @Input VARCHAR(50) 
SELECT @Input =CAST(@Qty1 AS VARCHAR(10))+'|'+CAST(@Qty2 AS VARCHAR(10))+'|'
SELECT REPLACE(Data,' ', '') 'Data' ,
	   LTRIM(RTRIM(REPLACE(REPLACE(Data,' ', ''),@Input,''))) 'Rate'
FROM @Test
WHERE REPLACE(Data,' ', '') LIKE @Input+'%'

Regards,
GVPrabu
 
Share this answer
 
v2
use below Query :

SQL
Select rate from tableName where 1 between qty1 and qty2
 
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