我的数据如下
98-45.3A-22
104-44.0A-23
00983-29.1-22
01757-42.5A-22
04968-37.3A2-23
输出在SQL Server中查找输出如下
00098-BA45.3A-IN-22
00104-BA44.0A-IN-23
00983-BA29.1-IN-22
01757-BA42.5A-IN-22
04968-BA37.3A2-IN-23
答案 0 :(得分:3)
我拆分了部件以应对棘手的数据模板。这应该适用于非破折号2位数的尾部:
WITH Src AS
(
SELECT * FROM (VALUES
('98-45.3A-22'),
('104-44.0A-23'),
('00983-29.1-22'),
('01757-42.5A-22'),
('04968-37.3A2-23')
) T(X)
), Parts AS
(
SELECT *,
RIGHT('00000'+SUBSTRING(X, 1, CHARINDEX('-',X, 1)-1),5) Front,
'BA'+SUBSTRING(X, CHARINDEX('-',X, 1)+1, 2) BA,
SUBSTRING(X, PATINDEX('%.%',X), LEN(X)-CHARINDEX('-', REVERSE(X), 1)-PATINDEX('%.%',X)+1) P,
SUBSTRING(X, LEN(X)-CHARINDEX('-', REVERSE(X), 1)+1, LEN(X)) En
FROM Src
)
SELECT Front+'-'+BA+P+'-IN'+En
FROM Parts
它返回:
00098-BA45.3A-IN-22
00104-BA44.0A-IN-23
00983-BA29.1-IN-22
01757-BA42.5A-IN-22
04968-BA37.3A2-IN-23
答案 1 :(得分:1)
试试这个,
DECLARE @String VARCHAR(100) = '98-45.3A-22'
SELECT ISNULL(REPLICATE('0',6 - CHARINDEX('-',@String)),'') -- Add leading Zeros
+ STUFF(
STUFF(@String,CHARINDEX('-',@String),1,'-BA'), -- Add 'BA'
CHARINDEX('-',@String,CHARINDEX('-',@String)+1)+2, -- 2 additional for the character 'BA'
1,'-IN') -- Add 'IN'
如果我在第一个连字符之前有超过6位数字并且想要删除前导零以使其为6位数,该怎么办。
DECLARE @String VARCHAR(100) = '0000098-45.3A-22'
SELECT CASE WHEN CHARINDEX('-',@String) <= 6
THEN ISNULL(REPLICATE('0',6 - CHARINDEX('-',@String)),'') -- Add leading Zeros
+ STUFF(
STUFF( @String,CHARINDEX('-',@String),1,'-BA'), -- Add 'BA'
CHARINDEX('-',@String,CHARINDEX('-',@String)+1)+2, -- 2 additional for the character 'BA'
1,'-IN') -- Add 'IN'
ELSE STUFF(
STUFF(
STUFF(@String,CHARINDEX('-',@String),1,'-BA'), -- Add 'BA'
CHARINDEX('-',@String,CHARINDEX('-',@String)+1)+2, -- 2 additional for the character 'BA'
1,'-IN'), -- Add 'IN'
1, CHARINDEX('-',@String) - 6, '' -- remove extra leading Zeros
)
END
答案 2 :(得分:1)
假设格式一致(例如总是以&#34; - &#34; + 2个字符......结尾)
DECLARE @Data TABLE (Col1 VARCHAR(100))
INSERT @Data ( Col1 )
SELECT Col1
FROM (
VALUES ('98-45.3A-22'), ('104-44.0A-23'),
('00983-29.1-22'), ('01757-42.5A-22'),
('04968-37.3A2-23')
) x (Col1)
SELECT RIGHT('0000' + LEFT(Col1, CHARINDEX('-', Col1) - 1), 5)
+ '-BA' + SUBSTRING(Col1, CHARINDEX('-', Col1) + 1, CHARINDEX('.', Col1) - CHARINDEX('-', Col1))
+ SUBSTRING(Col1, CHARINDEX('.', Col1) + 1, LEN(Col1) - CHARINDEX('.', Col1) - 3)
+ '-IN-' + RIGHT(Col1, 2)
FROM @Data
在SQL中始终执行此字符串操作并非理想的IMO。您可以将其移出到表示层,或将预先格式化的值存储在数据库中,以便每次都节省成本。
答案 3 :(得分:0)
使用 REPLICATE 和 CHARINDEX :
复制:将重复给定的字符,直到达到函数中指定的所需计数
CharIndex:查找任何字符的第一个匹配项
Declare @Data AS VARCHAR(50)='98-45.3A-22'
SELECT REPLICATE('0',6-CHARINDEX('-',@Data)) + @Data
SELECT
SUBSTRING
(
(REPLICATE('0',6-CHARINDEX('-',@Data)) +@Data)
,0
,6
)
+'-'+'BA'+ CAST('<x>' + REPLACE(@Data,'-','</x><x>') + '</x>' AS XML).value('/x[2]','varchar(max)')
+'-'+ 'IN'+ '-' + CAST('<x>' + REPLACE(@Data,'-','</x><x>') + '</x>' AS XML).value('/x[3]','varchar(max)')
答案 4 :(得分:0)
另一种方法是使用PARSENAME()
,您可以使用此查询:
WITH t AS (
SELECT
PARSENAME(REPLACE(REPLACE(s, '.', '@@@'), '-', '.'), 3) AS p1,
REPLACE(PARSENAME(REPLACE(REPLACE(s, '.', '@@@'), '-', '.'), 2), '@@@', '.') AS p2,
PARSENAME(REPLACE(REPLACE(s, '.', '@@@'), '-', '.'), 1) AS p3
FROM yourTable)
SELECT RIGHT('00000' + p1, 5) + '-BA' + p2 + '-IN-' + p3
FROM t;