Nimmagadda Gowtham
Nimmagadda Gowtham

Reputation: 270

hour from timestamp in postgresql for dynamic queries

I have a table named employee with 5 columns. One column is named emp_empdt it's type is timestamp .

I need to get hour value of emp_empdt of every record in the employee table.

I know that, we can get hour by using like

SELECT extract(hour from timestamp '2015-05-02 20:05:12'); 

This is an static query. But now i need to get timestamp value from employee table.

I used like:

SELECT h1 
from (
   select extract(hour from timestamp emp.emp_empdt) as h1 
   from employee emp
);

Upvotes: 0

Views: 848

Answers (1)

user330315
user330315

Reputation:

The timestamp keyword is not required:

select extract(hour from emp_empdt) as h1 
from employee emp

The expression timestamp '2015-05-02 20:05:12' is an ANSI SQL timestamp literal and the keyword timestamp is only required for a literal like that. This is necessary to distinguish such an expression from a character literal ('2015-05-02 20:05:12').

Upvotes: 3

Related Questions