简体   繁体   English

在SELECT SQL语句中仅将DateTime转换为Date

[英]Convert DateTime into Date only, inside of the SELECT SQL statement

I have an SQL statement 我有一条SQL语句

SELECT NO, MEMBID, DATEFROM, DATETO from Member where MEMBID ='xxyy'

I get a return where the dates are in DateTime format. 我得到的日期是DateTime格式的返回。 I only want them to be in Date format. 我只希望它们采用日期格式。

I have tried:- 我努力了:-

SELECT NO, MEMBID, CONVERT(DATEFROM, GETDATE()), DATETO from Member where MEMBID ='xxyy'

SELECT NO, MEMBID, CAST(DATEFROM as DATE()), DATETO from Member where MEMBID ='xxyy'

these don't seem to be working. 这些似乎不起作用。

I need to convert inside the SQL Statement itself. 我需要在SQL语句本身内部进行转换。 Been looking around on Google but can't seem to find anything. 一直在Google上四处寻找,但似乎找不到任何东西。 Any insights? 有什么见解吗?

EDIT :- What finally worked for me is the following conversion technique. 编辑:-最终对我有用的是以下转换技术。

CONVERT(VARCHAR(10), DATEFROM, 101) 转换(VARCHAR(10),DATEFROM,101)

这是一个将日期时间转换为日期的查询。

select cast('2014-06-04 00:00:00.000' as date)

There is no format in a DateTime or Date object. DateTimeDate对象中没有格式。 That only comes with the display of the data. 这仅与数据显示一起出现。

You were very close with the Convert, but needed just the output format . 您与Convert非常接近,但只需要输出格式

CONVERT(VARCHAR(10), DATEFROM, 101)

This will output the DATEFROM column as mm/dd/yyyy 这会将DATEFROM列输出为mm/dd/yyyy

This should work 这应该工作

SELECT NO, MEMBID, CAST(DATEFROM AS DATE), DATETO from Member where MEMBID ='xxyy'

for SQL SERVER 2012 对于SQL SERVER 2012

it should be 它应该是

SELECT NO, MEMBID, CONVERT(date, DATEFROM), DATETO from Member where MEMBID ='xxyy'

http://sqlfiddle.com/#!6/287dd/6 example http://sqlfiddle.com/#!6/287dd/6示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM