Solved

MS SQL Update datefield from a form

Posted on 2014-03-31
12
225 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
[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
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 53

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
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 
LVL 53

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 53

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 53

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 53

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

690 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