按名称排序,不带前导"" (和其他复杂的代码)

时间:2015-09-18 16:36:03

标签: sql sqlite

我正在从SQLite数据库中对歌曲名称进行排序,并且我想要忽略任何领先的""。所以,例如:

1  Labradors are Lovely
2  The Last Starfighter
3  Last Stop before Heaven

This answer在简单的情况下解决了这个问题:

SELECT name FROM songs
ORDER BY
  CASE WHEN instr(lower(name),'the ')=1 THEN substr(name,5)
       ELSE name
  END
COLLATE NOCASE;

但是,我已经使用了complex transformation on the name column。结合这两个我得到这个丑陋,非DRY代码:

SELECT n, name
FROM songs
ORDER BY
  CASE WHEN name GLOB '[0-9]*' THEN 1
       ELSE 0
  END,
  CASE WHEN name GLOB '[0-9]*' THEN CAST(name AS INT)
       ELSE CASE WHEN instr(lower(name),'the ')=1 THEN
                   replace(
                     replace(
                       replace(
                         replace(
                           substr(name,5),
                           '.',''
                         ),
                         '(',''
                       ),
                       '''',''
                     ),
                     '  ',' '
                   )
                 ELSE
                   replace(
                     replace(
                       replace(
                         replace(name,'.',''),
                         '(',''
                       ),
                       '''',''
                     ),
                     '  ',' '
                   )
                 END
  END
COLLATE NOCASE;

有没有办法在查询期间使用变量或其他东西,以便我可以干掉代码,并且只在一个位置而不是两个不同的案例分支中进行所有标点符号替换?

1 个答案:

答案 0 :(得分:2)

这样的事情应该有效。

@echo off

rem Next command is required to easily manage array elements
setlocal EnableDelayedExpansion

rem Create a loop that reads file (line by line)
set numLines=0
for /F "delims=" %%a in (file.txt) do (
   rem Each loop read each line into an array
   set /A numLines+=1
   set "line[!numLines!]=%%a"
)

rem Use that variable, let's say echo that variable
for /L %%i in (1,1,%numLines%) do echo !line[%%i]!