MS SQL Update datefield from a form

Posted on 2014-03-31
Medium Priority
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
Question by:smares323
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
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.
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?


Field type of the table.field

Author Comment

ID: 39969535
datetime field
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

LVL 53

Expert Comment

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

Author Comment

ID: 39969771
MS SQL 2000
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?
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

   ' send an error message
end if  

Open in new window


Author Comment

ID: 39969883
ColdFusion mx 7
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>
   <h3>The string <cfoutput>#DE(FORM.theTestValue)#</cfoutput> 
    is not a valid date</h3>
<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()#
<input type = "Submit" value = "Is it a Date?" name = "">

Open in new window

LVL 52

Accepted Solution

_agx_ earned 2000 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#"
   WHERE  .....

Open in new window


Author Closing Comment

ID: 39970596
Great advice

Featured Post

Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

765 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