Reputation: 27
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
Reputation: 68
Try this
Select Convert(varchar(10),GETDATE(),103) AS CurrentDate
Upvotes: 0
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
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
Reputation: 5031
You can use either of the following ..
SELECT CONVERT(DATE,datetime_field)
OR
SELECT CAST (datetime_field as date)
Upvotes: 3
Reputation: 124
If you are using SQL server 2008 or above, try this:
SELECT CONVERT(date, GETDATE())
Upvotes: 1