If that is the format (mm-dd-yy), wrap the 'string' in a convert statement.
e.g.
INSERT INTO MyTable (MyDateColumn)
VALUES (CONVERT(datetime,'12-04-0
Main Topics
Browse All TopicsI need to insert a date variable that contains formating like so:
12-04-07 13:22:11
Into a SQL Server 2005 table.
Note it is not always that date, that is a sample of the variable as it is formatted.
What would be the proper way to insert that?
When I try it as is, I get a date of 7/2/2007 12:00:00
I am using vbscript if that matters.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
When inserting the dates you will want to make sure of a couple of things:
1) use ' to surround the date.
2) be aware of the Locale that exists in Windows as well as the Collation and datatype.
-- using smalldatetime or datetime datatype
INSERT INTO dbo.table (column1) VALUES ( '12-04-2007 13:22:11' )
should produce '2007-12-04 13:22:11' when selected out.
Hi Guys
The date is a variable that will change.
I cannot code the actual date into the SQL command, I can only code the variable.
The variable name is: OrderPOSDate
The Variable is originally formatted like so: 12-04-07 13:22:11
The database column datatype is datetime
dbaduck:
INSERT INTO dbo.table (column1) VALUES ( '12-04-2007 13:22:11' )
I understand that, and I thought that would work, but it doesn't and I am currently using this:
INSERT INTO dbo.table (column1) VALUES ( '"&OrderPOSDate&"')
the inserted value ends up being:
7/2/2007 12:00:00
If I change it to:
INSERT INTO dbo.table (column1) VALUES ( "&OrderPOSDate&")
which is the 'date' you are suggesting, the inserted value ends up being: :
12/30/1899 12:00:00 AM
???
It should be
INSERT INTO dbo.table (column1) VALUES ( '"&OrderPOSDate&"')"
and for clarity it should be
( apostrophe quote &OrderPOSDate& quote apostrophe ) quote
' " &OrderPOSDate& " ' ) " without the spaces. Make sense? What looks like is happening is that your string is not formatted as you may expect and you also may look at your Locale settings (Regional Settings) in Windows on that SQL Server to make sure that you have different regional settings than US.
The other thing that you should look at is doing a
SELECT CONVERT(datetime, '" &OrderPOSDate&"')
and seeing what you get back.
dbaduck:
That is what I was doing originally and I was getting the 7/2/2007 12:00:00 value
I just did a copy/paste just to make sure and again, with your code I get: 7/2/2007 12:00:00
My regional settings are correct.
The convert statement you gave me gives me errors..
I pasted it into a SQL Query window and I get this:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting datetime from character string.
But I really have no clue as to what I am doing there.
I may have the code wrong...
I am doing a SELECT CONVERT(datetime, '" &OrderPOSDate&"') from [dbo].[orders] directly from the query window in SQL Studio Management Studio
Business Accounts
Answer for Membership
by: matthewspatrickPosted on 2007-12-07 at 08:25:29ID: 20428596
1) What is the data type for the database column in question?
2) Please post the relevant code