SQL Server 2005:将smalldatetime 转换为varchar 在视图和“正常”中的行为不同询问

发布于 2024-08-16 22:11:46 字数 690 浏览 6 评论 0原文

我有一个视图,基本上只返回表中的所有记录,并添加一列“isodate”,该列应该是 ISO 格式的日期。

CREATE VIEW [dbo].[v_bedarfe]
AS 
   SELECT *,convert(varchar(16),datum,20) As isodat FROM bedarfe
GO

“数据”字段是小日期时间。 isodat 上的查询结果......“令人惊讶”,因此为了明确这一点,我尝试了以下操作:

select top 10 datum,isodat,convert(varchar(16),datum,20) As isodat2 from v_bedarfe

这导致:

查询结果的屏幕截图

这看起来非常错误。

所以我认为我有错误的期望或者在这里“滥用”某些东西,但我不知道我可能做错了什么,并且希望有任何建议如何在这里回到正轨......

谢谢

迈克尔
(希望发布此内容时屏幕截图能够正确显示,预览不会显示)

I have a view that basically just returns all records from a table, and adds a column 'isodate' which is supposed to be the date in ISO-Format.

CREATE VIEW [dbo].[v_bedarfe]
AS 
   SELECT *,convert(varchar(16),datum,20) As isodat FROM bedarfe
GO

The "datum"-field is smalldatetime.
The results of a query on isodat were...'surprising', so to make the point clear, I tried this:

select top 10 datum,isodat,convert(varchar(16),datum,20) As isodat2 from v_bedarfe

which led to:

Screenshot of result from query

and that looks very wrong.

So I assume I have wrong expectations or am 'abusing' something here, but I don't see what I could be doing wrong and would appreciate any suggestions how to get back on track here...

Thanks

Michael
(hope the screenshot will display correctly when posting this, preview doesn't show it)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

很酷不放纵 2024-08-23 22:11:46

在视图中使用 * 是危险的。如果表定义发生更改,* 可能会导致视图错误地映射列。掉落&重新创建不带 * 的视图,看看是否可以解决问题。

PS Convert 20 实际上是 ODBC 规范,yyyy-mm-dd hh:mi:ss(24h),请参阅MSDN 页面

Using * in views is dangerous. If the table definition changes, * can cause the view to map the columns wrong. Drop & recreate the view without *, and see if that fixes the problem.

P.S. Convert 20 is actually ODBC canonical, yyyy-mm-dd hh:mi:ss(24h), see the MSDN page.

夜清冷一曲。 2024-08-23 22:11:46

Convert 中的最后一个参数是输出格式。

20=>年-月-日 时:分:秒(24小时)

131 => dd/mm/yy hh:mi:ss:mmmAM

请参阅 MS SQL 转换()

The last parm in convert is the output format.

20 => yyyy-mm-dd hh:mi:ss(24h)

131 => dd/mm/yy hh:mi:ss:mmmAM

see MS SQL convert()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文