Solved

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

Posted on 2009-07-07
8
729 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
8 Comments
 
LVL 19

Expert Comment

by:erikTsomik
Comment Utility
try getDate()
0
 
LVL 19

Expert Comment

by:erikTsomik
Comment Utility
and also what is the datatype  you using int your DB
0
 

Author Comment

by:jsoenke
Comment Utility
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
 
LVL 19

Accepted Solution

by:
erikTsomik earned 500 total points
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:jsoenke
Comment Utility
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
Comment Utility
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
Comment Utility
Also the best practice is to use <cfqueryparam for future references
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
> 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The purpose of this video is to demonstrate how to make a WordPress Site faster and smaller in size by cleaning up the database. This will be demonstrated using a Windows 8 PC. Plugin WP Optimize will be used. Go to your WordPress login page. T…
The purpose of this video is to demonstrate how to properly insert a Vimeo Video into a WordPress site or Blog. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now