从时区列中获取时间

时间:2015-04-02 07:27:23

标签: sql sql-server sql-server-2008 sql-server-2008-r2

我有一个包含用户文化的用户表。我需要从时间文化中获得时间。如需参考,请参阅下表。

           UserCulture 
------------------------------------------
(UTC-12:00) International Date Line West
(UTC-08:00) Baja California
(UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi
(UTC+08:00) Kuala Lumpur, Singapore
(UTC+08:00) Kuala Lumpur, Singapore
(UTC+08:00) Kuala Lumpur, Singapore
(UTC+08:00) Kuala Lumpur, Singapore
(UTC+08:00) Kuala Lumpur, Singapore
(UTC+08:00) Kuala Lumpur, Singapore
(UTC+08:00) Kuala Lumpur, Singapore
(UTC+08:00) Kuala Lumpur, Singapore

我想放弃" +8:00"如果是吉隆坡,新加坡

1 个答案:

答案 0 :(得分:1)

如果这只是varchar列,那么这可以有效:

DECLARE @t TABLE(TimeZone NVARCHAR(max))

INSERT INTO @t VALUES
('(UTC-12:00) International Date Line West'),
('(UTC-08:00) Baja California'),
('(UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi'),
('(UTC+08:00) Kuala Lumpur, Singapore')


SELECT TimeZone, SUBSTRING(timezone, 5, 6) AS Time FROM @t

输出:

TimeZone                                         Time
(UTC-12:00) International Date Line West         -12:00
(UTC-08:00) Baja California                      -08:00
(UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi  +05:30
(UTC+08:00) Kuala Lumpur, Singapore              +08:00