Solved

MS SQL Update datefield from a form

Posted on 2014-03-31
12
217 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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

705 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

21 Experts available now in Live!

Get 1:1 Help Now