• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 574
  • Last Modified:

SQL 2005 T-SQL

I would like to use T-sql to flip a string.

I would like to flip 'green,blue'  to 'blue,green'

i wish to flip the order of two words that are separated by a comma.
0
dastaub
Asked:
dastaub
2 Solutions
 
käµfm³d 👽Commented:
Try:

SELECT SUBSTRING(theString, CHARINDEX(',', theString) + 1) + ',' + SUBSTRING(theString, 1, CHARINDEX(',', theString) - 1)

Open in new window

0
 
selva_konguCommented:
try this

DECLARE @String VARCHAR(200),
@indx int, -- Index of the ,
@tlen int -- Total length of the input

SET @String = 'green, blue'  
--SET @String = REPLACE(@String,' ','') -- Remove the spaces
SELECT @tlen = LEN(@String)
SELECT @indx = CHARINDEX(',', @String)
 
SELECT SUBSTRING(@String,@indx+1,@tlen)+', '+ SUBSTRING(@String,1,@indx-1)
0
 
dastaubAuthor Commented:
below is what worked.

declare @TotalBy as varchar(max)
set @TotalBy = '1FacNNNNNNNNo1,1PDDDDDDoc1'
set @TotalBy = right(@TotalBy,(   len(@TotalBy) - CHARINDEX ( ',' ,@TotalBy))   ) + ',' + left(@TotalBy,CHARINDEX ( ',' ,@TotalBy)-1)
select @TotalBy
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now