l0r3nz4cc10
l0r3nz4cc10

Reputation: 1283

Why does the time appear in this query result

In the following code, I use trunc(date) in the query, because I am only interested in the date, not the time.

However, I get this printed :

[2019-11-28 00:00:00=653] instead of [2019-11-28=653].

Here is the code :

Map<String, Integer> map = jdbcTemplate.query("select trunc(closing_date) as closing_date, count(*) as total from some_table group by closing_date", new ResultSetExtractor<Map>() {
    @Override
    public Map extractData(ResultSet rs) {
        HashMap<String, Integer> mapRet = new HashMap<>();
        while(rs.next()) {
            mapRet.put(rs.getString("closing_date"), rs.getInt("total"));
        }
        return mapRet;
    }
});
LOGGER.info(Arrays.toString(map.entrySet().toArray()));

Why ?

Upvotes: 0

Views: 66

Answers (3)

it_iz_code
it_iz_code

Reputation: 43

While connecting from Java to Oracle database, If you can pass below Alter session command, then it can resolve your issue.

Alter Session Set NLS_DATE_FORMAT='DD-MON-YYYY';

Upvotes: 0

APC
APC

Reputation: 146249

TRUNC() removes the time element of a date in the sense that it rounds the time down to midnight (in the same way that trunc(23.42) gives us 23). But Oracle DATE is still actually a datetime and so there's a time element to display, unless we use a format mask which only shows the date element.

Upvotes: 2

Popeye
Popeye

Reputation: 35920

Trunc function only truncate the date. It do not change the display format.

You can use to_char(date, 'yyyy-mm-dd') to display date according to your requirement.

Cheers!!

Upvotes: 1

Related Questions