Oracle date handling and ColdFusion on INSERT TO

To issue a “INSERT TO” with a “SELECT” statement is a common practice to copy records from one table to another. A statement like this might look like;

[code]

<cfquery datasource="mydb">

INSERT INTO mytable

(id, somefield)

SELECT '1', thefield

FROM demo

WHERE id = 1

</cfquery>

[/code]

No big deal. Al right, now many times one also adds the current date to the table. Now as ColdFusion developers you might me inclined to write this code;

[code]

<cfquery datasource="mydb">

INSERT INTO mytable

(id, somefield, thedate)

SELECT '1', thefield, '#now()#'

FROM demo

WHERE id = 1

</cfquery>

[/code]

But what a surprise the code above will not work since Oracle is not able to interpret the “#now()#’ field (yes, we are on a CFM page, but it still does not work).Simple solution is to use the Oracle timestamp with;

[code]

<cfquery datasource="mydb">

INSERT INTO mytable

(id, somefield, thedate)

SELECT '1', thefield, systimestamp

FROM demo

WHERE id = 1

</cfquery>

[/code]

The “systimestamp” will insert the current date and time for you.

If you enjoyed this post, make sure you subscribe to my RSS feed!

Tags: , ,

About Razuna

This is the official blog of the Open Source Digital Asset Management System - Razuna. Razuna is the incredible easy way to manage all your Digital Assets. As the Open Source Alternative to Digital Asset Management (DAM) System you can save up to 90% with Razuna compared to any proprietary System. By using Razuna you get the benefit of it being free and open source and supported by a professional company. There are many options to try out Razuna today! Either, download your own version, create a free account on our Razuna Hosted Platform or get your own Razuna Cloud Server.

No comments yet.

Leave a Reply

You must be logged in to post a comment.