tags:

views:

11

answers:

3

Hi What is the problem with the mysql syntax SELECT FORMAT(dCreatedDate,'YYYY-MM-DD') as date1 FROM tbl_book_self I want to select the date from mysql database in this format,How to use this syntax

+1  A: 

Did you mean to use DATE_FORMAT rather than just FORMAT?

Also the format needs to be specified using % notation so the corrected version of your example would be

DATE_FORMAT(dCreatedDate, '%Y-%m-%d')

You can find a list of the specifiers you can use in the format string in the MySQL documentation on Date and Time Functions.

mikej
@mikej:http://w3schools.com/sql/sql_func_format.aspPlease have an look at this link
udaya
@mikej: The query actually resulted in fetching date in correct format these days i did the same work by exploding the datas in the view page and by displaying in some format
udaya
The w3school tutorial is a general SQL tutorial and is not specific to MySQL. In MySQL the function for formatting dates is `DATE_FORMAT`. MySQL does have a function called `FORMAT` but that is for [formatting numbers to a format like '#,###,###.##'](http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_format).
mikej
@mikej : i can can understand what you mean
udaya
From your second comment where you say it *actually resulted in fetching date in correct format these days* do you mean that you have fixed your problem now? If not, I think you might need to edit your question to clarify what it is you're asking. Attempting to use `FORMAT` with a 2nd parameter of `YYYY-MM-DD` will definitely result in `You have an error in your SQL syntax`.
mikej
@mike j : actually it results weong i tried what you said.....You are correct SELECT DATE_FORMAT(dCreatedDate, '%d-%M-%Y') as perdate FROM tbl_book_selfThis works perfect for me
udaya
@mikej : thanks
udaya
A: 
mysql> SELECT DATE_FORMAT('2009-10-04 22:23:00', '%W %M %Y');
        -> 'Sunday October 2009'
mysql> SELECT DATE_FORMAT('2007-10-04 22:23:00', '%H:%i:%s');
        -> '22:23:00'
mysql> SELECT DATE_FORMAT('1900-10-04 22:23:00',
    ->                 '%D %y %a %d %m %b %j');
        -> '4th 00 Thu 04 10 Oct 277'
mysql> SELECT DATE_FORMAT('1997-10-04 22:23:00',
    ->                 '%H %k %I %r %T %S %w');
        -> '22 22 10 10:23:00 PM 22:23:00 00 6'
mysql> SELECT DATE_FORMAT('1999-01-01', '%X %V');
        -> '1998 52'
mysql> SELECT DATE_FORMAT('2006-06-00', '%d');
        -> '00'

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_str-to-date

Judas Imam
A: 

SELECT DATE_FORMAT(dCreatedDate,'%Y-%m-%d') as date1 FROM tbl_book_self

ritesh choudhary