Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when the expression
SQL
when
	(CHARINDEX('₹', @promo_text) > 0 and ISNUMERIC(SUBSTRING(@promo_text, (CHARINDEX('₹', @promo_text)+1),3))=1) then
	try_cast(try_convert(money,(substring(@promo_text,(charindex('₹', @promo_text)+1),3))) as decimal)
need a stored procedure to extract indian rupee sign ₹ from number or string. Eg. ₹1258 discount, has to extract 1258 as output. Need a stored procedure in SQL SERVER

What I have tried:

i tried , if i enter 4 digit number getting only 3 digits output, where i am passing a single parameter at running procedure.
Also need how to pass a column name as parameter in stored procedure
Posted
Updated 8-Dec-22 3:04am
v2
Comments
Maciej Los 11-Sep-22 7:40am    
Show us sample data. Use "improve question" widget.

Basically, change your database.
Don't store numeric values as strings at all: store them as INT, FLOAT, or DECIMAL - and the latter is the most appropriate for currency values.
And remove the currency symbol in your presentation code when you validate and convert the user input, so that only value numbers can reach the DB.

If you don't, you get problems: what if a user enters a US$ or £ value instead? You can't just "remove the currency symbol" because the number isn't valid as Rupees anyway if it started out in a different currency - and the only way to fix that is at the point when the user enters the data.

And than there's using the data later: you can't do maths with strings: so even if you take off the currency indicator, every time you want to work out a total (price per * units sold for an invoice for example) you have to convert the string to a number, do the maths, and then use it - and at that point you find the user typed "₹12,3R,567" by mistake and you don't know what the value should have been. And that could be months later!

So always store data in the most appropriate format: numbers in numeric fields, dates in date / time fields. It ensures your data integrity, and saves you a huge amount of work later!
 
Share this answer
 
Comments
Sridhar Babu 2022 11-Sep-22 8:56am    
create table dbo.test1(promo_text nchar(200))
--insert into test1 values('₹1234 discount')
--insert into test1 values('discount sales price ₹2345 rupees')

create or alter procedure usp_ExtractSign(@promo_text nchar(200))
as
begin
set nocount on

select
case
when
(CHARINDEX('₹', @promo_text) > 0 and ISNUMERIC(SUBSTRING(@promo_text, (CHARINDEX('₹', @promo_text)+1),3))=1) then
try_cast(try_convert(money,(substring(@promo_text,(charindex('₹', @promo_text)+1),3))) as decimal)
end
from test1


end

exec usp_ExtractSign '₹1234 discount'


My requirement:
i entered two records
when executed procedure output should be 1234 and 2345. if required any changes in procedure,
post the code,
OriginalGriff 11-Sep-22 9:55am    
So you ask for advice, then when given it you ignore what is said and go "give me code" instead?

No. It doesn't work like that.
Check this out:
DECLARE @v NVARCHAR(30) = N'₹1234 discount';

select @v AS InitialValue;
select CAST(
  SUBSTRING(@v, CHARINDEX(N'₹', @v) +1, CHARINDEX(' ', @v) - CHARINDEX(N'₹', @v)
  ) AS DECIMAL(8,0)) AS Rupees;


SQL batch[^]
 
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