salvo9415
salvo9415

Reputation: 103

Set date format dd/mm/yyyy in SQL table

I want to save the dates in my database in the dd/mm/yyyy format, because I want to import the table that contains them in a JTable (Java SE) and I want to display them in this format. Is it possible to directly save the date on my database in this format or I must do it in another way? My DB is write in SQL and I use MySQL.

Upvotes: 1

Views: 1921

Answers (4)

Arun nagar
Arun nagar

Reputation: 176

Use DATE_FORMAT function to change the format of a date in MySQL.

 SELECT DATE_FORMAT(CURDATE(),'%d/%m/%Y')
 SELECT DATE_FORMAT(column_name,'%d/%m/%Y') # FROM tablename

Refer to documentation for more details.

Upvotes: 0

Hytool
Hytool

Reputation: 1368

You can't do it, or else you have to use VARCHAR or CHAR, but thats not recommended.

save the date in DATE datatype with format yyyy-mm-dd. don't mess with it.

When you fetch the records, use DATE_FORMAT function to convert it into your format. (if you use MySQL)

like

    SELECT DATE_FORMAT(CURDATE(), '%d-%m-%Y');

in your case

    SELECT DATE_FORMAT(< your_date_field >, '%d-%m-%Y');

Upvotes: 0

Davide Lorenzo MARINO
Davide Lorenzo MARINO

Reputation: 26926

Date are dates. It doesn't exists a format for dates.

What you can obtain is a string with a particular format from the date.

Note that the format probably is not dd/mm/yyyy but dd/MM/yyyy because mm is for minutes, not for months.

So basically you have two possibilities:

  • Save dates as Date and retrieve them as string with the requested format
  • Convert dates to strings and save them as formatted strings (VARCHAR for example)

To convert a Date to a String in MySql you can use the function DATE_FORMAT

If you like to convert them in java you can use a SimpleDateFormat

Upvotes: 3

Rajnikant Patel
Rajnikant Patel

Reputation: 571

There is no possibility to save the date in the specified format but yes you can set the type of that field as String (in MySql varchar) and you can save whatever you want.

Upvotes: 0

Related Questions