2

I have maintained a table with start_date as "date" datatype and now I wanted to display the date in dd-mmm-yyyy format.

public static BusObjectIterator<com.timesheetmd.weeksView> weeksOfYear(int year) { String queryText = "SELECT `id`, `year`, CONCAT(CAST(`start_date` AS CHAR), ' to ', CAST(`end_date` AS CHAR)) AS 'week' FROM `weeks` WHERE `weeks`.`year` = :year AND end_date <= CURRENT_DATE ORDER BY start_date DESC"; QueryObject query = new QueryObject(queryText); query.addParameter("year", "weeks.year ", QueryObject.PARAM_INT,year);//NOPMD query.setResultClass(weeksView.class); return query.getObjects(); } 

As I want to return the result in the form of BusObjectIterator. Is it possible to change the format of the date in the query itself?

1 Answer 1

6

You need DATE_FORMAT(start_date,'%d-%b-%Y') instead of your CAST(...AS CHAR).

Here's the DATE_FORMAT() writeup.

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

2 Comments

If I use DATE_FORMAT. Will it affect the return type?
You've had a string by your concatenation, it will remain a string. You don't need to cast it anymore though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.