Azure 上SQL Database(PaaS)Time Zone时区问题处理
在Azure上面的PaaS时间都是以UTC时间为准(云的世界里基本上都是以UTC时间为标准的),所以以前在本地SQL Server上面常用的GetDate()方法会碰到问题,在中国获取的时间会被当前时间少了8个小时,因为Azure上的时间都是UTC之间,中国的时区是+8.所以你通过GetDate()获取的时间少了8个小时是正常的。但是碰到这个问题有什么好办法可以解决呢?怎样才能获取带上时区偏移之后的时间呢?。在Azure SQL Database上面是没有办法直接通过设置某个参数或者变量就可以解决的。不过SQL Database V12之后开始支持SQL Server 2016才支持的一个新的语法:AT Time Zone
inputdate AT TIME ZONE timezone
- inputdate
-
Is an expression that can be resolved to a smalldatetime, datetime, datetime2, or datetimeoffset value.
- timezone
-
Name of the destination time zone. SQL Server relies on time zones that are stored in the Windows Registry. All time zones installed on the computer are stored in the following registry hive: KEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones. A list of installed time zones is also exposed through the sys.time_zone_info (Transact-SQL) view.
-
有了这个新的语法之后我们解决这个时区的问题就容易一些了。
-
假设我们有一张表appcount有两个字段一个是自增长字段id,和另外一个字段是日期型字段Createdate
-
CREATE TABLE [dbo].[appcount](
[Id] [int] IDENTITY(1,1) NOT NULL,[createtime] [datetime] NULL,
PRIMARY KEY CLUSTERED (Id)
)
通常我们往这个表里面插进去一条数据时会用下面这个语句:
INSERT INTO appcount(createtime) VALUES(getdate())
用来标记着这条记录的创建时间是当前时间的,但是直接在Azure SQL Database上面执行出来会变成这个结果:
-

-
如果我们在显示这个记录创建时间的时候不带上Time Zone相关的处理,时间就跟当前时间对不上号了。
-
如果要获取到当前时区的时间的话,我们可以将GetDate()这个方法稍微改一下
-
INSERT INTO appcount(createtime) VALUES(CONVERT(DATETIME,SYSDATETIMEOFFSET() AT TIME ZONE 'China Standard Time'))
-

从图里我们会发现时间变回了我们想要的本地时间了
注意:因为AT Time Zone语法是Azure SQL Database V12的功能,所以如果你的数据库不是V12版本的话是不支持这个语法的,你执行语句是会抛出下面的错误

老版本的的Azure SQL Datbase解决办法可以参考下面这个博客,通过自定义函数来解决
http://wely-lau.net/2011/07/10/managing-timezone-in-sql-azure-2/
Azure 上SQL Database(PaaS)Time Zone时区问题处理的更多相关文章
- Azure 上SQL Database(PaaS)Time Zone时区问题处理(进阶篇)
通常ISV在面对本地客户时对时间相关的处理,一般都时区信息都是不敏感的.但是现在云的世界里为了让大家把时间处理的方式统一起来,云上的服务都是以UTC时间为准的,现在如果作为一个ISV来说就算你面对的客 ...
- [Windows Azure] Managing SQL Database using SQL Server Management Studio
Managing Windows Azure SQL Database using SQL Server Management Studio You can use Windows Azure SQL ...
- [Windows Azure] Windows Azure Storage & SQL Database
http://channel9.msdn.com/Series/Windows-Azure-Storage-SQL-Database-Tutorials Windows Azure offers mu ...
- [Windows Azure] Monitoring SQL Database Using Dynamic Management Views
Monitoring Windows Azure SQL Database Using Dynamic Management Views 5 out of 7 rated this helpful - ...
- Java连接Azure SQL Database
Azure SQL Database是Azure上的数据库PAAS服务,让用户可以快速的创建和使用SQL数据库而不用担心底层的备份,安全,运维,恢复等繁琐的工作,本文简单介绍如何使用Java程序连接到 ...
- [Windows Azure] Guidelines for Connecting to Windows Azure SQL Database
Guidelines for Connecting to Windows Azure SQL Database 6 out of 12 rated this helpful - Rate this t ...
- [Windows Azure] Learn SQL Reporting on Windows Azure (9-Step Tutorial)
Learn SQL Reporting on Windows Azure (9-Step Tutorial) 4 out of 4 rated this helpful - Rate this top ...
- Azure SQL Database (21) 将整张表都迁移到Azure Stretch Database里
<Windows Azure Platform 系列文章目录> Azure SQL Database (19) Stretch Database 概览 Azure SQL Da ...
- Azure SQL Database (19) Stretch Database 概览
<Windows Azure Platform 系列文章目录> Azure SQL Database (19) Stretch Database 概览 Azure SQL Da ...
随机推荐
- HTML/CSS的基本使用
本篇博客主要介绍一下HTML/CSS的基本使用,关于它们的介绍便不在赘述,读者可自行google或百度. 一.HTML 先来简单介绍一下HTML标签: HTML 标签是由尖括号包围的关键词,比如 &l ...
- mac react-native从零开始android真机测试
1. 安装android相关jdk,(https://blog.csdn.net/vvv_110/article/details/72897142) 2. 手机和mac使用usb连接, 手机开发者设置 ...
- 吴恩达机器学习笔记——正规方程(Normal Equation)
问题描述:m examples : (x(1),y(1)), (x(2),y(2)),..., (x(m),y(m)) and n features; 计算方法:θ = (XTX)-1XTy; 计算过 ...
- 关于cnblog.com的用户体验
首先我自己目前是一个学生党,每天在博客园上就上发布一些自己做的东西以及老师布置的作业,还能在上面学习很多别人的一些好的列子,我就希望博客园能够很好地为我们这些学生服务,当我们用它时能够很好地达到我们的 ...
- JavaWeb应用的生命周期
JavaWeb应用的生命周期是由Servlet容器来控制. 启动阶段 (1)读取web.xml配置文件数据 (2)为JavaWeb应用创建一个ServletContext对象 (3)对所有的Filte ...
- 第13章 学习shell script
由于博客园中dollar符号有别的意义,所以文中的dollar符号使用¥表示 第一个script [root@localhost script]# cat -n sh01.sh #!/bin/bash ...
- bash循环语句
1 )单分支if语句 if 测试条件 :then 如果满足条件就执行这里的代码 f 2)双分支的if语句 if 测试条件:then 如果满足条件就执行这里的代码 else 如果不满足条件就执行这里 ...
- Qt多线程-QThreadPool线程池与QRunnable
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Qt多线程-QThreadPool线程池与QRunnable 本文地址:https:/ ...
- 【第一周】PSP
日期 C类别 C内容 S开始时间 E结束时间 I间隔(单位:分钟) T净时间(单位:分钟) 9月2日 编程 词频统计 7:35 9:35 10 110 9月3日 读书 构建之法 8:00 9:00 5 ...
- mysql项目路径URL编码
jdbc_url=jdbc:mysql://127.0.0.1:3306/test?serverTimezone=UTC&useUnicode=true&characterEncodi ...