1

Have migrated my database from mysql to SQL Server. When I run my query I get the error:

'DATE_FORMAT' is not a recognized built-in function name.

This is the query, where I am trying to convert the date as it's saved in the database 2014/03/03 to 03/03/2014 (D-M-Y).

This is the query:

DATE_FORMAT(routines.date, '%d/%m/%Y') as Dato
3
  • My apologies if I have misunderstood you, but I sincerely hope you are not storing dates and/or times in SQL Server as VARCHAR (aka strings). Choose an appropriate data type (DATE, DATETIME, SMALLDATETIME, etc) and perform date formatting at the application level. Commented Apr 11, 2014 at 14:20
  • 1
    Store them as DATE!!!!! The format of how they are stored is completely irrelevant. Change that in the display layer. Commented Apr 11, 2014 at 14:37
  • Does this answer your question? SQL Server date_format Commented Oct 12, 2022 at 15:49

2 Answers 2

8

Use this:

select CONVERT(varchar(12),getdate(),105)

See this for the various options.

Sign up to request clarification or add additional context in comments.

5 Comments

That worked. How can I convert the date to show as D/M/Y istead of D-M-Y ?
Use 103 instead of 105. Check the link out, it shows you all of the values for formatting the date
@user3270211 only change that in the display layer. Store the date and time as DATE, TIME, or DATETIME the format does not matter.
how to specify column name like created_at, updated at?
select CONVERT(varchar(12),getdate(),105) AS Created_at
2

I like to use it in the beginning of my query

SET DATEFORMAT DMY --Day/Month/Year ... you can write YMD or another combination

Syntax:

SET DATEFORMAT { format | @format_var } 

You can see examples in microsoft techNet

If you want to use CONVERT in your SELECT clause you can use:

SELECT convert(varchar, getdate(), 100) -- mon dd yyyy hh:mmAM (or PM) – Oct  2 2008 11:01AM          
SELECT convert(varchar, getdate(), 101) -- mm/dd/yyyy - 10/02/2008                  
SELECT convert(varchar, getdate(), 102) -- yyyy.mm.dd – 2008.10.02  
SELECT convert(varchar, getdate(), 105) -- dd-mm-yyyy 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.