Use this example to see the core idea, then work through the complete steps below. Code snippets are Classic ASP / VBScript or SQL Server T-SQL as labelled, not PHP.
VBScript example
Set cmd = Server.CreateObject("ADODB.Command")
Set cmd.ActiveConnection = cn
cmd.CommandType = 1 ' adCmdText
cmd.CommandText = "UPDATE dbo.dt SET issue_dt = ? WHERE book_id = ?"
cmd.Parameters.Append cmd.CreateParameter("pDate", 135, 1, , CDate(dateText))
cmd.Parameters.Append cmd.CreateParameter("pId", 3, 1, , 5)
cmd.ExecuteValidate dateText with IsDate() first. Prefer unambiguous form data and typed ADO parameters instead of inserting locale-formatted strings into SQL text.
Date/time values should be stored in SQL Server date/time columns as typed values, not as preformatted display strings. In Classic ASP, validate user-entered dates and send them through an ADO parameter so SQL Server receives a date value rather than locale-dependent SQL text.
dtm = "8/17/2007 7:44:56 PM"This may parse under a U.S.-style server locale, but slash-formatted strings can be interpreted differently on servers using another locale.
dtm = "8/17/2007 7:74:56 PM"The minute value 74 is invalid. Passing this to CDate() or another date operation can raise a type mismatch.
Microsoft VBScript runtime error '800a000d'
Type mismatch
<%
Dim dtm, cmd
dtm = Request.Form("issue_dt")
If IsDate(dtm) Then
Set cmd = Server.CreateObject("ADODB.Command")
Set cmd.ActiveConnection = conn
cmd.CommandType = adCmdText
cmd.CommandText = "INSERT INTO dbo.dt (book_id, issue_dt) VALUES (?, ?)"
cmd.Parameters.Append cmd.CreateParameter("pBook", adInteger, adParamInput, , 9)
cmd.Parameters.Append cmd.CreateParameter("pIssue", adDBTimeStamp, adParamInput, , CDate(dtm))
cmd.Execute
Set cmd = Nothing
Else
Response.Write "Please enter a valid date and time."
End If
%>This assumes ADO constants are available. Keep the value typed in the database; apply presentation formatting only when displaying it. For browser forms, an ISO-like input format or separate validated components can reduce locale ambiguity.
Strings such as 8/17/2007 depend on regional interpretation. Parse inputs deliberately and send an ADO date/time parameter; for SQL literals in controlled scripts, use unambiguous ISO-compatible formats.
Store values as date, datetime2, or another appropriate date/time type, not as text. Convert them to display strings with CONVERT() at output time.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| Harsh Sehgal | 02-04-2013 |
| i want date in numeric format without /(slash or back slash) .for example 3 0 0 4 2 0 1 3 (i.e 30/04/2013) | |