How do I convert these number columns (timestamp, event_dt) to a date or time mask?
I am trying this:
select to_char(timestamp,'YYYY-MON-DD HH24:MI:SS'), domain_c, to_char(event_date,'YYYY-MON-DD HH24:MI:SS'), total_reads from TOP_READ_EVENTS where timestamp= to_char(sysdate-2,'yyyymmdd') || '0000'
*
ERROR at line 1:
ORA-01481: invalid number format model
SQL> desc top_read_events;
Name Null? Type
----------------------------------------- -------- ----------------------------
YEAR NUMBER
QUARTER NUMBER
MONTH NUMBER
DAY NUMBER
HOUR NUMBER
TIMESTAMP NUMBER
DOMAIN_C VARCHAR2(255)
EVENT_DT NUMBER
TOTAL_READS NUMBER
select timestamp, domain_c, event_dt, total_reads from TOP_READ_EVENTS where timestamp= to_char(sysdate-2,'yyyymmdd') || '0000'
TIMESTAMP DOMAIN_C EVENT_DT TOTAL_READS
---------- ------------------------------ ------------ -------------
2.0111E+11 b.e.att-mail.com 2.0111E+11 14406
2.0111E+11 bounce.emailinfo2.bestbuy.com 2.0111E+11 14156
2.0111E+11 bounce.bluestatedigital.com 2.0111E+11 13701
2.0111E+11 plentyoffish.com 2.0111E+11 13384
2.0111E+11 mail.classmates.com 2.0111E+11 13281
2.0111E+11 comcast.net 2.0111E+11 13241
2.0111E+11 uniquelistsmail.com 2.0111E+11 13135
2.0111E+11 tankgorilla.com 2.0111E+11 12835
2.0111E+11 frigidphoenix.com 2.0111E+11 12657
Firstly, never store date or timestamp data-types in anything but a date or timestamp column. It causes no end of pain; as you’re gathering.
As your “timestamps” have 11 orders of magnitude I’m going to guess that you inserted them in the form
yyyymmddhh24mi, and that they’re not seconds since an unspecified epoch or anything like that.If you want to convert this then you have to firstly convert them into a character, then into a date. You don’t actually need a timestamp as these differ from dates only in fractional seconds.
It’d look something like the following:
On a side note never call a column timestamp, date, group or another reserved word. It causes too many problems. Personally I normally go for “tstamp” but that’s just personal preference.
If you want to convert your “timestamp” into a character you’d then have to convert it into a character again.
The seconds will always be
00as you don’t have these. Note that I only use explicit number to character to date to character conversion. It makes it more obvious to the coders who come after you what you’re doing, including yourself 2 years down the line, and there’s no chance of the interpreter making a mistake over intention and comparing a number to a character etc.For this reason I’d definitely change
where timestamp= to_char(sysdate-2,'yyyymmdd') || '0000'. As you want to date comparison change this into a date and to it that way, i.e.trunc()truncates this at the day level in this instance.