텍스트 형식으로 값을 나타내는 계산 열의 예 (완료되지 않은)는 다음과 같습니다.
ALTER TABLE [IPAddress]
ADD TextAddress AS (
IIF([Group8] IS NULL,
-- IPv4
CONCAT(CONVERT(TINYINT, [Group4]), '.', CONVERT(TINYINT, [Group3]), '.',
CONVERT(TINYINT, [Group2]), '.', CONVERT(TINYINT, [Group1]),
IIF([Network] IS NOT NULL, CONCAT('/', [Network]), '')),
-- IPv6
LOWER(CONCAT(
CONVERT(VARCHAR(4), [Group8], 2), ':', CONVERT(VARCHAR(4), [Group7], 2), ':',
CONVERT(VARCHAR(4), [Group6], 2), ':', CONVERT(VARCHAR(4), [Group5], 2), ':',
CONVERT(VARCHAR(4), [Group4], 2), ':', CONVERT(VARCHAR(4), [Group3], 2), ':',
CONVERT(VARCHAR(4), [Group2], 2), ':', CONVERT(VARCHAR(4), [Group1], 2),
IIF([Network] IS NOT NULL, CONCAT('/', [Network]), '')
))
) -- end of IIF
);
테스트:
INSERT INTO IPAddress VALUES (127, 0, 0, 0, 4, 22, 222, 63, NULL); -- IPv6
INSERT INTO IPAddress VALUES (27, 10, 1234, 0, 45673, 200, 1, 6363, 48); -- IPv6
INSERT INTO IPAddress VALUES (NULL, NULL, NULL, NULL, 192, 168, 2, 63, NULL); -- v4
INSERT INTO IPAddress VALUES (NULL, NULL, NULL, NULL, 192, 168, 137, 29, 16); -- v4
SELECT [IPAddressID], [Group8], [Group1], [Network], [TextAddress]
FROM IPAddress ORDER BY [IPAddressID];
결과:
IPAddressID Group8 Group1 Network TextAddress
----------- ------ ------ ------- ---------------------
-2147483646 0x007F 0x003F NULL 007f:0000:0000:0000:0004:0016:00de:003f
-2147483645 0x001B 0x18DB 48 001b:000a:04d2:0000:b269:00c8:0001:18db/48
-2147483644 NULL 0x003F NULL 192.168.2.63
-2147483643 NULL 0x001D 16 192.168.137.29/16