从 SQL SERVER 2005 中的 DATETIME 开始的日期(不使用 VARCHAR)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15761336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DATE from DATETIME in SQL SERVER 2005 (Without using VARCHAR)
提问by Constant Learner
Our application using SQL SERVER 2005
我们的应用程序使用SQL SERVER 2005
I need to show only DATE from DateTimein GridView.
我只需要在 GridView 中显示DateTime中的DATE。
I don't want to convertit into any other format like Varchar
or something.
It should be only in DateTimeformat itself, without Time.
我不想将它转换成任何其他格式之类的Varchar
。它应该只是DateTime格式本身,没有Time。
Please Help.
请帮忙。
回答by RichardTheKiwi
In SQL Server, the code is
在 SQL Server 中,代码是
CAST(datediff(d,0,datetimecol) as datetime)
However, I doubt that does any good for a GridView, which will infer it to be a "datetime" column and show a "date + time" formatting, even if the times are ALL "00:00:00".
但是,我怀疑这对 GridView 有什么好处,它会推断它是“日期时间”列并显示“日期 + 时间”格式,即使时间都是“00:00:00”。
回答by steoleary
In SQL 2005 there is no way of keeping it in datetime format without the time (time is part of the datetime type), the best you could get would be something like:
在 SQL 2005 中,如果没有时间(时间是日期时间类型的一部分),则无法将其保留为日期时间格式,您可以获得的最好结果是:
2013-04-02 00:00:00.000
If you were willing to convert to VARCHAR, you could use something similar to:
如果您愿意转换为 VARCHAR,则可以使用类似于以下内容的内容:
SELECT CONVERT(VARCHAR(10),GETDATE(),111)
Which would display the date portion as you want it. Otherwise, you need to move to SQL 2008+ where there is a proper date type that you can use instead.
这将根据需要显示日期部分。否则,您需要转移到 SQL 2008+,那里有一个您可以使用的正确日期类型。
If you need to stick with SQL 2005, the best way to achive this is to just format it in the datagrid view itself.
如果您需要坚持使用 SQL 2005,那么实现这一目标的最佳方法就是在数据网格视图本身中对其进行格式化。
回答by Pandian
Bound the DateField coloumn in Grid like below, then it works...
像下面这样在 Grid 中绑定 DateField 列,然后它就可以工作了...
<asp:BoundField DataField="Your_Date_Column"
HeaderText="Date_Column"
DataFormatString="{0:d}" />
DataFormatString="{0:d}"
it display the Date like 3/11/2013
DataFormatString="{0:d}"
它显示日期像 3/11/2013
回答by Constant Learner
If you want to truncate a datetime so that it only returns the date as at midnight, see the accepted answer to this question- the question relates specifically to SQLServer 2008, but the answer includes details of how to achieve this in SQLServer 2005.
如果您想截断日期时间以便它只返回午夜的日期,请参阅此问题的已接受答案- 该问题专门与 SQLServer 2008 相关,但答案包括如何在 SQLServer 2005 中实现此目标的详细信息。
If you want to convert a datetime field into a date-only field - you cannot achieve this in SQLServer 2005 in SQL alone, as there is no date-only datatype - see here.
如果您想将日期时间字段转换为仅日期字段 - 您无法在 SQLServer 2005 中单独使用 SQL 实现此目的,因为没有仅日期数据类型 - 请参阅此处。