Solved

MS SQL Update datefield from a form

Posted on 2014-03-31
12
222 Views
Last Modified: 2014-04-01
I'm trying to update a datetime field from a form, I could update the date in the following format 04/01/2014 When I update the field to 04/01/2014 12:30:00 it errors out It only works if I enter 04/01/2014 and  doesn't work if I enter in the time. Database value 2014-04-01 00:00:00.000
0
Comment
Question by:smares323
12 Comments
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39968096
You might be in the wrong topic. The issue you are experiencing is almost certainly related to the form not SQL Server.

What have you built your form in, and where it the code?

If I am correct please change the topic to the technology which related to your form.
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39968185
What is the field type set to?  Date or datetime or text or something else?

<<edit>>

Field type of the table.field
0
 

Author Comment

by:smares323
ID: 39969535
datetime field
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39969741
What serverside language are you using?
0
 

Author Comment

by:smares323
ID: 39969771
MS SQL 2000
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39969843
>I'm trying to update a datetime field from a form

Is this from a website? What server side language?  ASP.NET, ASP classic, PHP, something else?  Or is this a desktop app?
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39969855
If using a web form, did you try entering  "4/1/2014  3:25pm".   Before I add or update data to my db, I first check that the data is a date.     For example in  ASP classic you can use

if isdate("4/1/2014  3:25pm") then
    ' update the database

   else
   ' send an error message
end if  

Open in new window

0
 

Author Comment

by:smares323
ID: 39969883
ColdFusion mx 7
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39969952
I do not know CF, but it looks like there is an isDate function.  Try passing your variables through that before sending to the server.  http://livedocs.adobe.com/coldfusion/7/htmldocs/wwhelp/wwhimpl/js/html/wwhelp.htm


<h3>IsDate Example</h3>
<cfif IsDefined("FORM.theTestValue")>
   <cfif IsDate(FORM.theTestValue)>
   <h3>The string <cfoutput>#DE(FORM.theTestValue)#</cfoutput> 
    is a valid date</h3>
   <cfelse>
   <h3>The string <cfoutput>#DE(FORM.theTestValue)#</cfoutput> 
    is not a valid date</h3>
   </cfif>
</cfif>
<form action = "isDate.cfm" method="post">
<p>Enter a string, find whether it can be evaluated to a date value.
<p><input type = "Text" name = "TheTestValue" value = "<cfoutput>#Now()#
</cfoutput>">
<input type = "Submit" value = "Is it a Date?" name = "">
</form>

Open in new window

0
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 39969975
What does your cfquery code look like and what is the exact error message?  

It should work fine as long as
a) the input is a valid date string (your example is valid) AND
b) the target column is type "datetime"

Try using cfqueryparam to ensure you're passing a date object and protect against sql injection. Always a good idea w/SQL Server anyway. Be sure to validate the input string first, as Scott mentioned.  

(EDIT) Just keep in mind IsDate() is .. overly generous in what it considers a valid "date".  So if the source strings can vary, you may want to roll your own.

<!--- demo value --->
<cfset FORM.someDateString = "04/01/2014 12:30:00">

...  validate date string ...

<cfquery ....>
   UPDATE someTable
   SET      YourDateTimeCol = <cfqueryparam value="#FORM.someDateString#"
                                                 cfsqltype="cf_sql_timestamp">
   WHERE  .....
</cfquery>

Open in new window

0
 

Author Closing Comment

by:smares323
ID: 39970596
Great advice
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

Suggested Solutions

Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.

831 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