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 ...
随机推荐
- 4.openldap创建索引
1.索引的意义 提高对Openldap目录树的查询速度 提高性能 减轻对服务器的压力 2.搜索索引 ldapsearch -Q -LLL -Y EXTERNAL -H ldapi:/// -b cn= ...
- phpquery 学习笔记
phpQuery是一个基于PHP的服务端开源项目,它可以让PHP开发人员轻松处理DOM文档内容,比如获取某新闻网站的头条信息.更有意思的是,它采用了jQuery的思想,你可以像使用jQuery一样处理 ...
- “Hello World!”团队第六周的第五次会议
今天是我们团队“Hello World!”团队第六周召开的第五次会议.博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.todo list 六.会议照片 七.燃尽图 八.代码 一 ...
- 评价Win8自带输入法
对于人机交互设计,有以下四个基本原则:从用户角度考虑.从头到尾记住用户选择.短期刺激和长期使用的好处坏处.不让用户犯简单错误.我用的最多的是我的系统自带的输入法,评价的也只能是它了. 1.从用户角度: ...
- Improving the Safety, Scalability, and Efficiency of Network Function State Transfers
Improving the Safety, Scalability, and Efficiency of Network Function State Transfers 来源:ACM SIGCOMM ...
- css3 伪元素 ::before ::after
键代码分析: /*css代码*/ .effect::before, .effect::after{ content:""; position:absolute; z-index:- ...
- Cobbler环境搭建
Cobbler服务器系统: CentOS 6.6 64位Cobbler版本: cobbler-2.6.11IP地址:192.168.166.136 1.安装epel库 rpm -ivh http:// ...
- c 用指针操作结构体数组
重点:指针自加,指向下一个结构体数组单元 #include <stdio.h> #include <stdlib.h> #include <string.h> #d ...
- 定制安全的PHP环境
除了熟悉各种PHP漏洞外,还可以通过配置php.ini来加固PHP的运行环境.PHP官方也曾经多次修改php.ini的默认设置.在本书中,推荐php.ini中一些安全相关参数的配置. register ...
- IE 之 userData 模拟 localStorage
引 chrome, safari, firefox, ie 9都支持 localStorage. 但可恶的是,中国 ie 6 占有最大的比例. 使用 cookie 不但容量有限,而且给我们增加了不 ...