SQL Server SQL datetime format to date only

qmelpv7a  于 2022-12-22  发布在  其他
关注(0)|答案(6)|浏览(224)

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??

SELECT Subject, DeliveryDate 
from Email_Administration 
where MerchantId =@ MerchantID

03/06/2011 12:00:00 Am just be selected as 03/06/2011..
Thanks alot in advance! :)

arknldoa

arknldoa1#

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

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

p4tfgftt2#

try the following as there will be no varchar conversion

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

mrphzbgm3#

With SQL server you can use this

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

with mysql server you can do the following
SELECT * FROM my_table WHERE YEAR(date_field) = '2006' AND MONTH(date_field) = '9' AND DAY(date_field) = '11'

chhkpiq4

chhkpiq44#

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

mklgxw1f5#

Create a function, do the conversion/formatting to "date" in the function passing in the original datetime value.

CREATE FUNCTION dbo.convert_varchar_datetime_to_date (@iOriginal_datetime varchar(max) = '')
RETURNS date
AS BEGIN
    declare @sReturn date

    set @sReturn = convert(date, @iOriginal_datetime)

    return @sReturn
END

Now call the function from the view:

select dbo.convert_varchar_datetime_to_date(original_datetime) as dateOnlyValue
yiytaume

yiytaume6#

if you are using SQL Server use convert
e.g. select convert(varchar(10), DeliveryDate, 103) as ShortDate
more information here: http://msdn.microsoft.com/en-us/library/aa226054(v=sql.80).aspx

相关问题