guimarques8
guimarques8

Reputation: 27

Mysql function DATE() on sql server?

I need to convert this MYSQL query to sql server:

DATE(datetime_field);

How can i translate this query on sql server language?

Thanks.

Upvotes: 0

Views: 1366

Answers (6)

ADNAN ZAMAN
ADNAN ZAMAN

Reputation: 68

Try this

Select Convert(varchar(10),GETDATE(),103) AS CurrentDate

Upvotes: 0

Antonio
Antonio

Reputation: 535

CONVERT also has an handy 3rd argument which allows to format the date using some predefined styles (returns varchar)

so for example:

SELECT CONVERT(varchar, GETDATE(), 112)

will output today's daye in ISO format like 20160831

SELECT CONVERT(varchar, GETDATE(), 103)

would output 31/08/2016

...and so on...

see: https://msdn.microsoft.com/en-us/library/ms187928.aspx

Upvotes: 0

Kannan Kandasamy
Kannan Kandasamy

Reputation: 13969

You can use

select try_convert(date, datetimefield)

or

select try_cast(datetimefield as date)

If you get invalid date it will not error out instead it will return null

Upvotes: 0

Unnikrishnan R
Unnikrishnan R

Reputation: 5031

You can use either of the following ..

SELECT CONVERT(DATE,datetime_field)

OR

 SELECT CAST (datetime_field as date)

Upvotes: 3

Vishal Gupta
Vishal Gupta

Reputation: 124

If you are using SQL server 2008 or above, try this:

SELECT CONVERT(date, GETDATE())

Upvotes: 1

AT-2017
AT-2017

Reputation: 3149

Simply use the following:

 SELECT CONVERT(date, getdate())

Upvotes: 3

Related Questions