Solved

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

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

Accepted Solution

by:
erikTsomik earned 500 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…
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…

911 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

18 Experts available now in Live!

Get 1:1 Help Now