Neither of the answers so far captures your actual problem(s).
While an explicit cast to the appropriate type certainly doesn't hurt, it is not necessary. PostgreSQL coerces a string literal to the appropriate type automatically.
Your problems stem from basic syntax errors:
Double quotes are for identifiers: "MyColumn"
- and only necessary for otherwise illegal identifiers (mixed case, reserved word, ..) which should be avoided to begin with.
Single quotes are for values: 'string literal'
.
You might be interested in the well written chapters on identifiers and constants of the PostgreSQL manual.
While we are at it, never use date
or time
as column names. Both are reserved words in every SQL standard and type names in PostgreSQL. This will lead to confusing code and error messages.
I would recommend to just use a single timestamp
column instead of separate date
and time
:
And you almost certainly don't want character(100)
as data type, ever - especially not for an id
column. This blank-padded type is basically only there for historic reasons. Consider text
or varchar
instead:
Could look like this:
CREATE TABLE tbl (
tbl_id text CHECK(length(id) <= 100)
, ts timestamp
);
Cast to time
or date
where you only need these components, it's short and cheap:
SELECT ts::time AS the_time, ts::date AS the_date FROM tbl;
Use date_trunc()
or extract()
for more specific needs.
To query for ... id values that have a time value > 10 pm on a certain day:
SELECT *
FROM tbl
WHERE ts::time > '22:00'
AND ts::date = '2012-04-18';
Or, for any continuous time period:
...
WHERE ts > '2012-04-18 22:00'::timestamp
AND ts < '2012-04-19 00:00'::timestamp;
The second form can use a plain index on ts
better and will be faster in such cases for big tables.
More about timestamp
handling in PostgreSQL: