SQL PHP HTML ASP JavaScript articles and free scripts to download
 

Formatted date and time variable to store in MSSQL

We can format a date and time value before storing them or inserting them to a date and time field in MSSQL table. Here is a formatted date and time value which can be stored in record.

dtm="8/17/2007 7:44:56 PM"


Here the variable dtm stores the formatted date and time value. Let us try with one invalid date and time value like this

dtm="8/17/2007 7:74:56 PM"


The date and time field will generate an error message saying like this

Error Type:
Microsoft VBScript runtime (0x800A000D)
Type mismatch: '[string: "8/17/2007 7:74:56 PM"]'
/a/dt/insert.asp, line 29


So it is always a good idea to check the date and time by using isDate function and then we can insert the date value to our MSSQL table.

Here is the code to add a date variable to our record.

Dim dtm dtm="8/17/2007 7:54:56 PM"
Response.Write dtm

if isDate(dtm) Then
rs.open "insert into dt(book_id,issue_dt) values( 9,'"& dtm & "')", conn
else
Response.Write "Please enter a valid date and time "
End if

You can read the tutorial on how to collect date values from the user.
Further readings
Inserting present date and time in a field by default
How to format Date & Time variable to insert into MSSQL table
DatePart to get part of a date & time field of MSSQL record
Updating MSSQL date & time field
DateDiff: Getting the Difference in days, month and year between two date fields
 
Scripts
PHP
JavaScript
All ASP Tutorials
Popular Tutorials
Managing two drop downs
ASP Tutorials
Date and time
Declaring array
Form in ASP
Server.MapPath
Date Time & MSSQL
Select Query
File System Object
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.