Click here to Skip to main content
15,920,030 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
SQL
--Declare / get the Phone number in the @PhoneNumber variable
Declare @PhoneNumber nvarchar(15) = '9710600000'
--The below select query can stuff the relevant symbol and space in mentioned position
Select Case When Len(@PhoneNumber)> 10 then Stuff(Stuff(Stuff(@PhoneNumber,1,0,'+'),4,0,' '),9,0,' ')
When Len(@PhoneNumber)= 10 then Stuff(Stuff(@PhoneNumber,5,0,' '),1,0,'+91 ')
When Len(@PhoneNumber)< 10 then 'Wrong Mobile Number'
End As PhoneNumber

 --The Output will be as below
+91 9710 600000
Posted

From MSDN: https://msdn.microsoft.com/en-us/library/ms188043.aspx[^]

SQL
STUFF ( character_expression , start , length , replaceWith_expression )


character_expression
Is an expression of character data. character_expression can be a constant, variable, or column of either character or binary data.
start
Is an integer value that specifies the location to start deletion and insertion. If start or length is negative, a null string is returned. If start is longer than the first character_expression, a null string is returned. start can be of type bigint.
length
Is an integer that specifies the number of characters to delete. If length is longer than the first character_expression, deletion occurs up to the last character in the last character_expression. length can be of type bigint.
replaceWith_expression
Is an expression of character data. character_expression can be a constant, variable, or column of either character or binary data. This expression will replace length characters of character_expression beginning at start.
 
Share this answer
 
Further to solution 1 have a look at this CP article Most Commonly Used Functions in SQL Server 2005/2008[^]
 
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