25

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! :)

Martin Smith
  • 438,706
  • 87
  • 741
  • 845
challengeAccepted
  • 7,106
  • 20
  • 74
  • 105
  • 3
    What RDBMS and Version? And is your desired format `dd/mm/yyyy` or `mm/dd/yyyy` – Martin Smith Mar 07 '11 at 16:20
  • My desired format us mm/dd/yyyy. I am a newbie for SQL, i have no idea how i can find the RDBMS type and version. All i know is SQL server 2005. – challengeAccepted Mar 07 '11 at 16:29
  • Possible duplicate of http://stackoverflow.com/questions/133081/most-efficient-way-in-sql-server-to-get-date-from-datetime and others. – goodeye Aug 31 '13 at 03:29
  • Possible duplicate of [How to remove the time portion of a datetime value (SQL Server)?](http://stackoverflow.com/questions/2775/how-to-remove-the-time-portion-of-a-datetime-value-sql-server) – Michael Freidgeim Aug 31 '16 at 05:49

6 Answers6

35

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 
Martin Smith
  • 438,706
  • 87
  • 741
  • 845
22

try the following as there will be no varchar conversion

SELECT Subject, CAST(DeliveryDate AS DATE)
from Email_Administration 
where MerchantId =@ MerchantID
khr055
  • 28,690
  • 16
  • 36
  • 48
efatihan
  • 347
  • 2
  • 8
4

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'

Theresa Forster
  • 1,914
  • 3
  • 19
  • 35
2

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

2
SELECT Subject, CONVERT(varchar(10),DeliveryDate) as DeliveryDate
from Email_Administration 
where MerchantId =@ MerchantID
Manish
  • 517
  • 1
  • 3
  • 19
1

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

Eray Balkanli
  • 7,752
  • 11
  • 48
  • 82
SteveB
  • 1,474
  • 1
  • 13
  • 21