Remove the last character in a string in T-SQL?

前端 未结 21 2369
不思量自难忘°
不思量自难忘° 2020-12-07 09:22

How do I remove the last character in a string in T-SQL?

For example:

\'TEST STRING\'

to return:

\'TES         


        
21条回答
  •  心在旅途
    2020-12-07 09:54

    I love @bill-hoenig 's answer; however, I was using a subquery and I got caught up because the REVERSE function needed two sets of parentheses. Took me a while to figure that one out!

    SELECT
       -- Return comma delimited list of all payment reasons for this Visit
       REVERSE(STUFF(REVERSE((
            SELECT DISTINCT
                   CAST(CONVERT(varchar, r1.CodeID) + ' - ' + c.Name + ', ' AS VARCHAR(MAX))
              FROM VisitReason r1
              LEFT JOIN ReasonCode c        ON c.ID = r1.ReasonCodeID
             WHERE p.ID = r1.PaymentID
             FOR XML PATH('')
                  )), 1, 2, ''))                        ReasonCode
      FROM Payments p
    

提交回复
热议问题