我需要从这种格式转换一些字符串:
"1020202020"
要
"1-0-2-0-2-0-2-0-2-0"
我怎样才能以简单的方式做到这一点?
由于
答案 0 :(得分:2)
每天我醒来并自言自语“今天CTE会有什么不可能的事情发生?”
;with cte as (
select
'1020202020' inputstring,
convert(varchar(max),'') outputstring
union all
select
substring(inputstring,2,len(inputstring)),
outputstring + left(inputstring,1) + '-' + case when len(inputstring) = 2 then right(inputstring,1) else '' end
from cte
where len(inputstring) > 1
)
select top 1 outputstring from cte order by len(outputstring) desc
答案 1 :(得分:0)
关于连字符放置的确切规则,您的问题尚不清楚。
因此,使用replace()
可能会有一些聪明的方法。
对于您的示例字符串:
select replace(replace(col, '02', '-0-2'), '20', '2-0')
答案 2 :(得分:0)
我会这样做:
TRIM(TRAILING "-" FROM REPLACE("10203040", "0", "-0-"))
答案 3 :(得分:0)
这是一个非循环/非递归解决方案。我对REPLACE()有一些乐趣,而且效率非常高。它可以在不到一秒的时间内处理100,000行,这可能比任何循环或递归解决方案更好。
IF OBJECT_ID('strings') IS NOT NULL
DROP TABLE strings;
CREATE TABLE strings (string BIGINT);
--Populate the table
WHILE(SELECT COUNT(*) FROM strings) < 100000
BEGIN
INSERT INTO strings
SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL
SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL
SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL
SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL
SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000 UNION ALL SELECT RAND() * 10000000000
END
--Add the hyphens with REPLACE() and cut off the extra hyphen at the end with SUBSTRING()
SELECT SUBSTRING(
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(string, '0', '0-'),'1','1-'),'2','2-'),'3','3-'),'4','4-'),'5','5-'),'6','6-'),'7','7-'),'8','8-'),'9','9-'),
0,
LEN(string)*2
) AS String
FROM strings;
缩略结果:
String
------------------------
5-6-1-2-9-4-9-0-8-1
7-5-8-5-1-9-3-0-9
2-2-4-7-6-1-5-7-6-9
9-3-4-6-1-0-3-5-0
9-9-5-3-6-8-8-7-8-9
5-3-7-2-0-5-3-0-6
8-4-0-8-9-8-9-4-5-0
9-8-6-4-6-8-4-4-3-9
6-6-5-8-2-8-7-3-9-2
...