SQL 日期时间格式仅限日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5222075/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 09:36:35  来源:igfitidea点击:

SQL datetime format to date only

sqlsql-serversql-server-2005datetime

提问by Ram

I am trying to select the DeliveryDate from sql database as just date. In the database, i am saving it as datetime format. How is it possible to get just date??

我试图从 sql 数据库中选择 DeliveryDate 作为日期。在数据库中,我将其保存为日期时间格式。怎么可能只是约会?

SELECT Subject, DeliveryDate 
from Email_Administration 
where MerchantId =@ MerchantID

03/06/2011 12:00:00 Am just be selected as 03/06/2011..

03/06/2011 12:00:00 刚刚被选为 03/06/2011..

Thanks alot in advance! :)

非常感谢!:)

回答by Martin Smith

After perusing your previous questions I eventually determined you are probably on SQL Server 2005. For US format you would use style 101

仔细阅读您之前的问题后,我最终确定您可能使用的是 SQL Server 2005。对于美国格式,您将使用样式 101

select Subject, 
       CONVERT(varchar,DeliveryDate,101) as DeliveryDate
from Email_Administration 
where MerchantId =@MerchantID 

回答by efatihan

try the following as there will be no varchar conversion

尝试以下操作,因为不会有 varchar 转换

SELECT Subject, CAST(DeliveryDate AS DATE)
from Email_Administration 
where MerchantId =@ MerchantID

回答by Theresa Forster

With SQL server you can use this

使用 SQL 服务器,您可以使用它

SELECT CONVERT(VARCHAR(10), GETDATE(), 101) AS [MM/DD/YYYY];

with mysql server you can do the following

使用 mysql 服务器,您可以执行以下操作

SELECT * FROM my_table WHERE YEAR(date_field) = '2006' AND MONTH(date_field) = '9' AND DAY(date_field) = '11'

SELECT * FROM my_table WHERE YEAR(date_field) = '2006' AND MONTH(date_field) = '9' AND DAY(date_field) = '11'

回答by Manish

SELECT Subject, CONVERT(varchar(10),DeliveryDate) as DeliveryDate
from Email_Administration 
where MerchantId =@ MerchantID

回答by bhs

if you are using SQL Server use convert

如果您使用的是 SQL Server,请使用convert

e.g. select convert(varchar(10), DeliveryDate, 103) as ShortDate

例如选择 convert(varchar(10), DeliveryDate, 103) 作为 ShortDate

more information here: http://msdn.microsoft.com/en-us/library/aa226054(v=sql.80).aspx

更多信息:http: //msdn.microsoft.com/en-us/library/aa226054(v=sql.80).aspx