我有数据库值,可能有更多不同的结构
NAME1
NAME1 NAME2
NAME1-NAME2
NAME1 NAME2-NAME3
NAME1-NAME2 NAME3
我希望我的select查询返回以下结构中的名称:
Name1
Name1 Name2
Name1-Name2
Name1 Name2-Name3
Name1-Name2 Name3
我找到了一个应该执行此操作的函数(Click),但是我收到以下错误消息:
Msg 262, Level 14, State 18, Procedure InitCap, Line 1
CREATE FUNCTION permission denied in database 'C30_SZV-PRIS'.
我对功能了解不多但似乎我需要一些我不具备的权限。我没有很多权限,但很遗憾我无法获得这些权限。
因此,我正在寻找另一种方法来解决这个问题,可能在select语句中?
答案 0 :(得分:0)
这是一种方法。用select语句替换游标中的select语句。我知道光标是恶魔......我借用了你在问题中链接到的@Espo的一些代码。
/* declare variables */
DECLARE @origName Varchar(255)
DECLARE @Index INT
DECLARE @Char CHAR(1)
DECLARE @PrevChar CHAR(1)
DECLARE @InputString Varchar(255)
DECLARE @OutputString VARCHAR(255)
CREATE TABLE #names1 (
id int IDENTITY(1,1)
,names varchar(255));
DECLARE name_cursor CURSOR FOR SELECT names FROM test1
OPEN name_cursor
FETCH NEXT FROM name_cursor INTO @origName
WHILE @@FETCH_STATUS = 0
BEGIN
SET @InputString = @origName
SET @OutputString = LOWER(@InputString)
SET @Index = 1
WHILE @Index <= LEN(@InputString)
BEGIN
SET @Char = SUBSTRING(@InputString, @Index, 1)
SET @PrevChar = CASE WHEN @Index = 1 THEN ' '
ELSE SUBSTRING(@InputString, @Index - 1, 1)
END
IF @PrevChar IN (' ', ';', ':', '!', '?', ',', '.', '_', '-', '/', '&', '''', '(')
BEGIN
IF @PrevChar != '''' OR UPPER(@Char) != 'S'
SET @OutputString = STUFF(@OutputString, @Index, 1, UPPER(@Char))
END
SET @Index = @Index + 1
END
INSERT INTO #names1
(names)
VALUES (@OutputString)
FETCH NEXT FROM name_cursor INTO @origName
END
CLOSE name_cursor
DEALLOCATE name_cursor
SELECT names FROM #names1
DROP TABLE #names1
答案 1 :(得分:0)
基于this的答案,我编写了此函数:
const arr = ['1', '2A', '2B', '2AA', '2', '10A', '10', '11', '12A', '12B', '12']
arr.sort((a, b) => {
// extract digit and non-digit part from string
let as = a.match(/(\d+)(\D*)/);
let bs = b.match(/(\d+)(\D*)/);
// either return digit differennce(for number based sorting)
// in addition to that check string length(in case digits are same)
// or compare non-digit part(string comparison)
return as[1] - bs[1] || a.length - b.length ||as[2].localeCompare(bs[2]);
})
console.log(arr)