我想在列的中间插入连字符到表的所有行。我非常确定这里有一个内置功能。
如何指定将连字符放在列中的位置?
答案 0 :(得分:3)
DECLARE @TABLE TABLE (MMDD_INT INT, MMDD_CHAR VARCHAR(4))
INSERT INTO @TABLE
VALUES
(1201, '1201'),
(1110, '1110'),
(910 , '0910'),
(101, '0101')
SELECT LEFT(RIGHT('0'+CAST(MMDD_INT AS VARCHAR(4)), 4),2)
+ '-' + RIGHT(CAST(MMDD_INT AS VARCHAR(4)),2) AS Int_Column
,LEFT(MMDD_CHAR, 2) + '-'+ RIGHT(MMDD_CHAR, 2) AS Char_Column
FROM @TABLE
╔════════════╦═════════════╗
║ Int_Column ║ Char_Column ║
╠════════════╬═════════════╣
║ 12-01 ║ 12-01 ║
║ 11-10 ║ 11-10 ║
║ 09-10 ║ 09-10 ║
║ 01-01 ║ 01-01 ║
╚════════════╩═════════════╝