September 15, 2014 at 2:58 am
[font="Arial Black"]Hi,
I have 10 digit Mobile numbers in one column of table.
I have to display the data in the following way
933445758293******82
123456789012******90
I have to display two digits from starting and two digit from last
and remaining six digit should be replace with any special character.
Please help me.
[/font]
September 15, 2014 at 3:11 am
declare @mobile as varchar(10) = '9334457582'
select replace(@mobile,SUBSTRING(@Mobile,3,6),'******')
Does this do what you want?
How to post a question to get the most help http://www.sqlservercentral.com/articles/Best+Practices/61537
September 15, 2014 at 3:16 am
Quick solution, allows for variable length input
😎
;WITH SAMPLE_DATA(MOBILE) AS
(SELECT * FROM
(VALUES
('9334457582')
,('1234567890')
) AS X(MOBILE)
)
SELECT
SD.MOBILE
,STUFF(SD.MOBILE,3,LEN(SD.MOBILE)-3,REPLICATE('*',LEN(SD.MOBILE)-3))
FROM SAMPLE_DATA SD;
Results
MOBILE
---------- -----------
9334457582 93*******2
1234567890 12*******0
September 15, 2014 at 3:21 am
Eirikur Eiriksson (9/15/2014)
Quick solution, allows for variable length input😎
;WITH SAMPLE_DATA(MOBILE) AS
(SELECT * FROM
(VALUES
('9334457582')
,('1234567890')
) AS X(MOBILE)
)
SELECT
SD.MOBILE
,STUFF(SD.MOBILE,3,LEN(SD.MOBILE)-3,REPLICATE('*',LEN(SD.MOBILE)-3))
FROM SAMPLE_DATA SD;
Results
MOBILE
---------- -----------
9334457582 93*******2
1234567890 12*******0
One minor quibble with that Eirikur, that only gives one digit visible at the end :ermm:
How to post a question to get the most help http://www.sqlservercentral.com/articles/Best+Practices/61537
September 15, 2014 at 3:40 am
gouri92 (9/15/2014)
[font="Arial Black"]Hi,I have 10 digit Mobile numbers in one column of table.
I have to display the data in the following way
933445758293******82
123456789012******90
I have to display two digits from starting and two digit from last
and remaining six digit should be replace with any special character.
Please help me.
[/font]
Can you post the exact code that caused the error message. I suspect I know what you've done but I can't see without the code you used.
N
How to post a question to get the most help http://www.sqlservercentral.com/articles/Best+Practices/61537
September 15, 2014 at 3:46 am
BWFC (9/15/2014)
Eirikur Eiriksson (9/15/2014)
Quick solution, allows for variable length input😎
;WITH SAMPLE_DATA(MOBILE) AS
(SELECT * FROM
(VALUES
('9334457582')
,('1234567890')
) AS X(MOBILE)
)
SELECT
SD.MOBILE
,STUFF(SD.MOBILE,3,LEN(SD.MOBILE)-3,REPLICATE('*',LEN(SD.MOBILE)-3))
FROM SAMPLE_DATA SD;
Results
MOBILE
---------- -----------
9334457582 93*******2
1234567890 12*******0
One minor quibble with that Eirikur, that only gives one digit visible at the end :ermm:
Opps :w00t:
Sorry about that, getting sloppy am I;-)
Here is a parametrized version
😎
DECLARE @DIGITS_LEFT INT = 2;
DECLARE @DIGITS_RIGHT INT = 2;
;WITH SAMPLE_DATA(MOBILE) AS
(SELECT * FROM
(VALUES
('9334457582')
,('1234567890')
) AS X(MOBILE)
)
SELECT
SD.MOBILE
,STUFF(SD.MOBILE,@DIGITS_LEFT + 1,LEN(SD.MOBILE)
- (@DIGITS_LEFT + @DIGITS_RIGHT),REPLICATE('*',LEN(SD.MOBILE)
- (@DIGITS_LEFT + @DIGITS_RIGHT)))
FROM SAMPLE_DATA SD;
September 15, 2014 at 3:53 am
create table #tblRegistration
(
Phone1 varchar (10)
)
insert into #tblregistration
select '9334457582' union
select '1234567890'
select
replace(Phone1,SUBSTRING(phone1,3,6),'******') ---This one
from #tblregistration
drop table #tblRegistration
This should stop the error message. Just include the marked line in your select list. Let me know how you get on please.
How to post a question to get the most help http://www.sqlservercentral.com/articles/Best+Practices/61537
September 17, 2014 at 10:09 am
Forgive my impudence, but these solutions seem rather "clever." Given the problem domain, isn't this simpler?
declare @mobile char(10), @maskChar char(1)
select @mobile = '9334457582', @maskChar = '*'
select left(@mobile, 2) + replicate(@maskChar, 6) + right(@mobile, 2)
select @mobile = '1234567890', @maskChar = '#'
select left(@mobile, 2) + replicate(@maskChar, 6) + right(@mobile, 2)
-- if the lengths need to be flexable, then this can be extended as such:
declare @leftLen int, @rightLen int, @mobileLen int
select @leftLen = 3, @rightLen = 4, @mobileLen = len(@mobile)
select left(@mobile, @leftLen) + replicate(@maskChar, @mobileLen - @leftLen - @rightLen) + right(@mobile, @rightLen)
Don Simpson
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply