Tuesday, March 24, 2009

Extract Day, Hour, Time from Date Datatype in Oracle

There was a need to write a query which would give output for data which took place between two specific hours in a day, in a particular a month, for a set of departments.

Set of departments - group by function
for a month - simple between and to_date conversion

for specific hours, day, time - Extract function

Extract(year from to_timestamp(to_char(column_name,'dd-Mon-yyyy HH:MI:SS AM')))
Extract(month from to_timestamp(to_char(column_name,'dd-Mon-yyyy HH:MI:SS AM')))
Extract(day from to_timestamp(to_char(column_name,'dd-Mon-yyyy HH:MI:SS AM')))

Extract(hour from to_timestamp(to_char(column_name,'dd-Mon-yyyy HH:MI:SS AM')))
Extract(minute from to_timestamp(to_char(column_name,'dd-Mon-yyyy HH:MI:SS AM')))
Extract(sec from to_timestamp(to_char(column_name,'dd-Mon-yyyy HH:MI:SS AM'))) 

There was some bit of struggle in finding the correct format. If column is of the datatype DATE then, one has to to_char it. Without to_charing it, it invariably returns only 0 (zero)

Extract Day, Hour, Time from Date Datatype in OracleSocialTwist Tell-a-Friend
Bookmark and Share

No comments:

Post a Comment

Blog Widget by LinkWithin
 
Clicky Web Analytics