如何从datetime中删除时间 [英] how to remove time from datetime

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

问题描述

数据库中的DATE字段具有以下格式:

The field DATE in the database has the following format:

2012-11-12 00:00:00

我想从日期删除时间,并返回日期如下:

I would like to remove the time from the date and return the date like this:

11/12/2012


推荐答案

首先是第一件事,如果你的日期是varchar格式的变化,那么将日期存储为日期,它会为您节省很多麻烦,这是最好的做得更早而不是晚了。这个问题只会变得更糟。

First thing's first, if your dates are in varchar format change that, store dates as dates it will save you a lot of headaches and it is something that is best done sooner rather than later. The problem will only get worse.

其次,一旦你有一个日期不要将日期转换为varchar!保留日期格式,并在应用程序方面使用格式来获​​取所需的日期格式。

Secondly, once you have a date DO NOT convert the date to a varchar! Keep it in date format and use formatting on the application side to get the required date format.

根据您的DBMS有各种各样的方法:

There are various methods to do this depending on your DBMS:

SQL Server 2008 及更高版本:

SQL-Server 2008 and later:

SELECT  CAST(CURRENT_TIMESTAMP AS DATE)






SQL-Server 2005 和更早的


SQL-Server 2005 and Earlier

SELECT  DATEADD(DAY, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP), 0)






MySQL & SQLite


MySQL & SQLite

SELECT  DATE(NOW())






Oracle


Oracle

SELECT  TRUNC(CURRENT_TIMESTAMP)






Postgresql


Postgresql

SELECT  CURRENT_TIMESTAMP::DATE






如果您需要在报告中使用文化特定格式,您可以明确说明接收文本框的格式(例如,dd / MM / yyyy),也可以设置语言,以显示该语言的相关日期格式。


If you need to use culture specific formatting in your report you can either explicitly state the format of the receiving text box (e.g. dd/MM/yyyy), or you can set the language so that it shows the relevant date format for that language.

无论哪种方式在SQL之外处理得更好,因为在SQL中转换为varchar会影响您在报告中可能执行的任何排序。

Either way this is much better handled outside of SQL as converting to varchar within SQL will impact any sorting you may do in your report.

如果您不能/不会将数据类型更改为DATETIME,则sti将其转换为SQL内的日期(例如, CONVERT(DATETIME,yourField)),然后发送到报告服务并按照上述处理。

If you cannot/will not change the datatype to DATETIME, then still convert it to a date within SQL (e.g. CONVERT(DATETIME, yourField)) before sending to report services and handle it as described above.

这篇关于如何从datetime中删除时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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