Workaround for casting issues and Postgres?

3,821 views
Skip to first unread message

Michael Gorsuch

unread,
Dec 8, 2011, 5:23:52 PM12/8/11
to Korma
Howdy!

When attempting to run code like this:

(insert issue (values {:timestamp "2004-10-19 10:23:54"}))

I receive an error like so:

Failure to execute query with SQL:
INSERT INTO issue (timestamp) VALUES (?) :: [2004-10-19 10:23:54]
PSQLException:
Message: ERROR: column "timestamp" is of type timestamp without time
zone but expression is of type character varying
Hint: You will need to rewrite or cast the expression.
Position: 39
SQLState: 42804
Error Code: 0

Any suggestions on how to deal with this?

Thanks a lot. I'm really liking the project so far.

Best,

Michael

Chris Granger

unread,
Dec 8, 2011, 5:47:27 PM12/8/11
to sqlk...@googlegroups.com
Hey Michael,

JDBC deals with timestamps as java.sql.Timestamp so all you need to do is convert your string into that. I tend to use millis, but if you want to always use strings, you could set up a (prepare ..) that converts them before they go into the DB.

Based on a quick look, it looks like you want to do (java.sql.Timestamp/valueOf "2004-10-19 10:23:54") or if you use millis you can just do (java.sql.Timestamp. ms)

Cheers,
Chris.

Craig Tataryn

unread,
Dec 8, 2011, 5:59:29 PM12/8/11
to sqlk...@googlegroups.com
FYI I've been using clj-time [1] and it's been working well for Timestamp conversions

(:require [clj-time.coerce :as dateconv])
.
.
.
(dateconv/from-long (. (:UPDATED_ON result) getTime)) ;;get it from a TimeStamp to a DateTime, result is in UTC

Michael Gorsuch

unread,
Dec 8, 2011, 6:11:20 PM12/8/11
to sqlk...@googlegroups.com
Thanks all! That fixed me up. My app is using millis, but I thought
it'd be easier to illustrate with a string.

Appreciate it,

Michael

Reply all
Reply to author
Forward
0 new messages