Sqlserver
 sql >> Teknologi Basis Data >  >> RDS >> Sqlserver

Cara membalikkan string (SQL Server 2005, SET BERBASIS)

Sunting Jawaban asli salah memahami persyaratan. Saya telah melakukan perbaikan tetapi pemisahan ke level karakter sama sekali tidak perlu sekarang. Setidaknya itu bisa memberikan beberapa ide!

WITH Strings AS
(
select 'A Student' as String
UNION ALL
select 'blah' as String
UNION ALL
select 'the quick brown fox jumped over the lazy dog' as String
),
SplitChars As
(
SELECT ROW_NUMBER() OVER (ORDER BY number) AS number, String, SUBSTRING(String,number,1) AS Ch FROM Strings
JOIN master.dbo.spt_values on number BETWEEN 1 AND LEN(String) AND type='P'
)


SELECT String,
replace(Stuff(
            (
            Select '' + Ch
            From SplitChars SC3
            WHERE SC3.String = SC.String
            Order By (SELECT COUNT(*) FROM SplitChars SC2 WHERE SC2.String = SC3.String AND SC2.Ch = ' ' AND SC2.number < SC3.number) desc, case when SC3.ch = ' ' then -1 else number end
            For Xml Path('')
            ),1, 0, ''), '&#x20;', ' ') AS Reversed
FROM SplitChars SC 
GROUP BY String

Pengembalian

  • Siswa A
  • bla
  • anjing malas si rubah coklat yang melompat cepat


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. SQL Group By - Hasilkan beberapa kolom agregat dari satu kolom

  2. Perbarui Profil Email Database di SQL Server (T-SQL)

  3. Penyisipan Massal Gagal Kesalahan konversi data pemuatan massal (pemotongan)

  4. Kembalikan Tipe Data Dasar dari Nilai Varian SQL di SQL Server

  5. Pengantar Keamanan Tingkat Baris di SQL Server