Reputation: 21
I need move last 2 characters of string to become first 2, for example, "ABC PT" become "PT ABC". Thanks for help.
Upvotes: 0
Views: 3500
Reputation: 1512
CREATE TABLE #TEMP
(
ID INT IDENTITY(1,1) ,
NAME VARCHAR(50)
)
INSERT INTO #TEMP VALUES('PC1AB')
INSERT INTO #TEMP VALUES('PC2XY')
INSERT INTO #TEMP VALUES('PC3NA')
INSERT INTO #TEMP VALUES('PC3NAXBBNTEYE12')
SELECT SUBSTRING(NAME,LEN(NAME)-1,2)+LTRIM(LEFT(NAME,LEN(NAME)-2)) FROM #TEMP
Upvotes: 0
Reputation: 69564
DECLARE @String VARCHAR(100) = 'ABC PT'
SELECT RIGHT(@String, 2) + ' ' + LEFT(@String, LEN(@String) -2)
RESULT : PT ABC
Upvotes: 1
Reputation: 1796
below is the select statement, use it to update if you want to c1 is the column name in the table test15. If you have a variable then replace c1 with the variable name and remove the from clause.
select RIGHT(c1,2)+SUBSTRING(c1,1,len(c1)-2) from test15
Upvotes: 0
Reputation: 9724
Query:
DECLARE @Str as nvarchar(10);
SET @Str = 'ABC PT';
SELECT RTRIM(RIGHT(@Str,2)+' '+SUBSTRING(@Str, 1 , LEN(@Str)-2))
Result:
PT ABC
Upvotes: 0
Reputation: 3108
You can use substring
function.
for example :
select substring('ABC PT',len('ABC PT')-1,2)+' '+stuff('ABC PT',len('ABC PT')-1,2,'')
Upvotes: 0