SHARE
TWEET

Untitled

SQLSoldier Mar 16th, 2015 204 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. WITH Chars (MaxLen)
  2. AS (SELECT MAX(len(name))
  3.         FROM sys.DATABASES),
  4. FixBadChars
  5. AS (SELECT CAST(name AS nvarchar(1000)) AS FixedString, 1 AS MyChar
  6.         FROM sys.DATABASES
  7.         UNION ALL
  8.         SELECT CASE WHEN Unicode(SUBSTRING(FixedString, MyChar + 1, 1)) > 255 THEN
  9.         CAST(Stuff(FixedString, MyChar + 1, 1, N'') AS nvarchar(1000))
  10.                 ELSE FixedString
  11.                 END AS FixedString, MyChar + 1
  12.         FROM FixBadChars
  13.         CROSS Apply Chars
  14.         WHERE MyChar <= MaxLen)
  15. SELECT FixedString
  16. FROM FixBadChars
  17. CROSS Apply Chars
  18.         WHERE MyChar = MaxLen
  19. OPTION (MAXRECURSION 8000)
RAW Paste Data
Want to get better at SQL?
Learn to code SQL in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top