I have a table
BookNName,Col1
Col1(nvarchar) has data in the following format
BookNo|ShelfNo|BookQty
Col1
-----
1|1|763
2|2|989
3|3|431
4|4|
I have to display only the BookQty and also add a condition that BookQty<900
So, my output should be
BookName BookQty
ABC 763
XYZ 431
WEST <----------Should dislpay blank value also
What I tried
To display only BookQty, I wrote the following -
select BookName, case when charindex('|',Col1,1)>=2 then right(Col1, charindex('|', reverse(col1))-1)
else ''
end
as 'BookQty'
This is giving output as
BookName BookQty
ABC 763
SOUTH 989
XYZ 431
WEST
Now, I have to add the condition that BookQty<900
For that I tried -
select BookName, case when charindex('|',Col1,1)>=2 then right(Col1, charindex('|', reverse(col1))-1)
else ''
end
as 'BookQty'
from Table1
where right(Col1, charindex('|', reverse(col1))-1)<'900'
This is giving me error
Invalid length parameter passed to the RIGHT function
I know this is due to the blanks in the column. But is there any other way to do this. I cannot change the database values.