如何使用时区在SQL视图中返回Datetimeoffset

时间:2019-10-21 16:28:05

标签: sql-server view timezone

是否可以在使用AT TIME ZONE的视图中从类型为datetime的表中返回一个值作为datetimeoffset?

当我尝试

CREATE VIEW TESTVIEW AS SELECT appointment_start AT TIME ZONE 'central european standard time' AS x FROM dbo.Appointments

结果与预期的一样,但是当我在SQL Management Studio中打开视图时,收到消息“无法解析查询文本”。

当我尝试类似的东西时也是一样 SELECT TODATETIMEOFFSET(appointment_start, DATEPART(tz, appointment_start AT TIME ZONE 'central european standard time')) as y FROM dbo.Appointments

如何在SQL视图中使用AT TIME ZONE将datetime值转换为datetimeoffset?

编辑: 当我从表中另外选择另一列时,我也得到了预期的结果,但是SQL Management Studio抱怨

SELECT id, appointment_start AT TIME ZONE 'central european standard time' AS x FROM dbo.Appointments

Error in SELECT clause: expression near 'TIME'. Missing FROM clause. Error in list of function arguments: 'AT' not recognized. Unable to parse query text.

非常感谢

1 个答案:

答案 0 :(得分:0)

可能与谁有关: 通过使用SQL函数解决了该问题。

也许不是最优雅的方法,但它似乎有效。

[System.Diagnostics.Process]::Start("c:\file\path\file.rdp")

在SQL视图中

<div class="columns"> <div class="column is-8"> <h1 class="title is-2 has-text-grey discussion-title"><%= @discussion.title %></h1> <h3 class="subtitle is-5 has-text-grey-lighter">by <%= @discussion.user.username %> in <%= link_to @discussion.channel.channel, @discussion.channel %></h3> <div class="level"> <div class="level-left"></div> <div class="level-right"> <% if discussion_author(@discussion) %> <div class="buttons"> <%= link_to 'Edit Discussion', edit_discussion_path(@discussion), class:'button'%> <%= link_to 'Delete', discussion_path(@discussion), method: :delete, data: { confirm: "Delete discussion?" }, class:'button' %> </div> <% end %> </div> </div> <div class="content"><%= markdown(@discussion.content) %></div> <h2 class="subtitle is-5 has-text-grey"><%= @discussion.replies.count %> Replies</h2> <div id="discussion-replies"> <%=# render @discussion.replies %> </div> <hr/> <h3 class="subtitle is-3 has-text-grey">Leave a reply</h3> <% if user_signed_in? %> <%=# render 'replies/form' %> <% else %> <p>To reply you need to <%= link_to 'login', new_user_session_path %>. Don't have an account? <%= link_to 'Sign up', new_user_registration_path %> for one.</p> <% end %> </div> <%= render 'sidebar' %> </div> <div class="columns"> <div class="column is-8"> <h1 class="title is-2 has-text-grey discussion-title"><%= @discussion.title %></h1> <h3 class="subtitle is-5 has-text-grey-lighter">by <%= @discussion.user.username %> in <%= link_to @discussion.channel.channel, @discussion.channel %></h3> <div class="level"> <div class="level-left"></div> <div class="level-right"> <% if discussion_author(@discussion) %> <div class="buttons"> <%= link_to 'Edit Discussion', edit_discussion_path(@discussion), class:'button'%> <%= link_to 'Delete', discussion_path(@discussion), method: :delete, data: { confirm: "Delete discussion?" }, class:'button' %> </div> <% end %> </div> </div> <div class="content"><%= markdown(@discussion.content) %></div> <h2 class="subtitle is-5 has-text-grey"><%= @discussion.replies.count %> Replies</h2> <div id="discussion-replies"> <%= render @discussion.replies %> </div> <hr /> <h3 class="subtitle is-3 has-text-grey">Leave a reply</h3>() <% if user_signed_in? %> <%= render 'replies/form' %> <% else %> <p>To reply you need to <%= link_to 'login', new_user_session_path %>. Don't have an account? <%= link_to 'Sign up', new_user_registration_path %> for one.</p> <% end %> </div> <%= render 'sidebar' %> </div>