Skip to content
Advertisement

Oracle query, get count of records by hour

I am trying to get transaction counts for every hour. Normally it is a straight forward query by unfortunately the timestamp column I have to work with is not timestamp but varchar2! No matter what I try I get either “not a valid month” or “invalid number”, depending on the format I use.

The timestamp looks like: 2021-08-08 00:00:52:632

I also executed the following to get NLS format:

and get

This is the latest I tried among a dozen others (I commented out the “group by” to just get the darn thing to display).

Also

At my wits end and need some help.

Advertisement

Answer

Assuming that your column is always in the format 2021-08-08 00:00:52:63 then group on the substring up to the 13th character:

If you do want to convert to a date then, from Oracle 12.2, you can use TO_TIMESTAMP(string_value DEFAULT NULL ON CONVERSION ERROR, 'YYYY-MM-DD HH24:MI:SS:FF'):

db<>fiddle here

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement