?
Solved

How do I insert a timestamp into a SQL database using coldfusion?

Posted on 2009-07-07
8
Medium Priority
?
746 Views
Last Modified: 2013-12-20
I'm created a database driven site in coldfusion and have run up against a wall, I haven't been able to find a clear answer on the web. I'm trying to store the date and time a record is created or updated in a database. I've been unsuccessful in storing and inserting the value from now() into the database. I get error messages that the insert statement doesn't understand ts { .... }. The field data type in the database is date/time. Please help!
0
Comment
Question by:jsoenke
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 19

Expert Comment

by:erikTsomik
ID: 24800335
try getDate()
0
 
LVL 19

Expert Comment

by:erikTsomik
ID: 24800337
and also what is the datatype  you using int your DB
0
 

Author Comment

by:jsoenke
ID: 24800396
Here's what happens:

Variable GETDATE is undefined.

See the code below...

Thanks
	INSERT INTO facilities
	(facility_name,facility_addr,facility_city,facility_state,facility_country,facility_postal,facility_market,facility_type,facility_created)
	VALUES ('#requiredFacilityName#','#requiredFacilityAddress#','#city#','#state#','#country#','#requiredFacilityPostal#','#market#','#getDate()#')

Open in new window

0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 19

Accepted Solution

by:
erikTsomik earned 2000 total points
ID: 24800403
change it to this
  INSERT INTO facilities
        (facility_name,facility_addr,facility_city,facility_state,facility_country,facility_postal,facility_market,facility_type,facility_created)
        VALUES ('#requiredFacilityName#','#requiredFacilityAddress#','#city#','#state#','#country#','#requiredFacilityPostal#','#market#',getDate())
0
 

Author Comment

by:jsoenke
ID: 24800439
Now I get the following error:

FUNCTION elevated.getDate does not exist

I found that GetDate() is for MS-SQL, Now() is for MySQL which is what I'm using. However, when I removed the #'s and used Now(), it worked!

Thank you for the help!
0
 
LVL 27

Expert Comment

by:azadisaryev
ID: 24800443
getdate() is not a cf function - it is MS SQL built-in function. thus do not surround it with ##.

PS: Use <cfqueryparam> when inserting user-defined values into your db!
PPS: you may also want to properly scope your variables, i.e. #FORM.requiredFacilityName#

Azadi
0
 
LVL 19

Expert Comment

by:erikTsomik
ID: 24800452
Also the best practice is to use <cfqueryparam for future references
0
 
LVL 52

Expert Comment

by:_agx_
ID: 24805760
> VALUES (...... '#getDate()#'   )
> Now() is for MySQL which is what I'm using. However, when I removed the #'s
> and used Now(), it worked!

It probably would have worked if you had removed the single quotes.  

As you have discovered both CF and MySQL have a now() function.   To use the CF function, use ## signs, but _no_ single quotes (because the function returns a date/time object, not a string).

INSERT ....
VALUES (   #now()# )

To use the MySQL version of that function, do _not_ use ## signs or single quotes:

INSERT ....
VALUES (   now() )
0

Featured Post

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
The purpose of this video is to demonstrate how to insert an Iframe into WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Open Page or Post…
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question