没有时间的日期 [英] Date without the time

查看:45
本文介绍了没有时间的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SQL Server 2005.

I'm working with SQL Server 2005.

我有一列名为purchase_time 的类型为datetime.如何使用时间部分选择此列 - 只是日期.

I have a column called purchase_time of type datetime. How do I select this column with the time part - just the date.

谢谢,

巴里

获取日期时间并在第一个空间上通过 Python 拆分它是否安全,或者这种格式是否依赖于语言环境?

Would it be safe to get the datetime and split it via Python on the first space, or is this format locale dependant?

推荐答案

In versions <2008(根据对某些答案的其他评论,我相信您正在运行),最有效的方法是将其保留为日期时间类型并使用日期数学来避免字符串转换.

In versions < 2008 (which, based on other comments to some of the answers, I believe you are running), the most efficient way is to keep it as a datetime type and use date math to avoid string conversions.

SELECT DATEADD(DAY, DATEDIFF(DAY, '20000101', purchase_time), '20000101') 
  FROM dbo.table;

编辑

如果您希望日期仅用于显示目的,而不是用于计算或分组,那么最好在客户端处理.您可以在 SQL 中简单地说:

If you want the date only for display purposes, not for calculations or grouping, that is probably best handled at the client. You can do it in SQL simply by saying:

SELECT dt = CONVERT(CHAR(10), purchase_time, 120)
  FROM dbo.table;

这篇关于没有时间的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆