MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 30th, 2012, 06:28 PM
eclypz00 eclypz00 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 3 eclypz00 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 3 m 2 sec
Reputation Power: 0
[Solved] Microsoft SQL Server - Conversion Failed When Converting Dates

Hello everyone. I'm currently taking an Intro to Database Systems and am attempting to populate tables as apart of my lab.

The following was used to create the table:

CREATE TABLE customers(
customer_id int IDENTITY(1,1) NOT NULL,
membership_yn char(1) NOT NULL,
membership_number int NOT NULL,
date_became_member date NOT NULL,
customer_first_name varchar(32) NOT NULL,
customer_last_name varchar(32) NOT NULL,
customer_address varchar (128) NOT NULL,
customer_phone varchar(9) NOT NULL,
customer_email varchar(32) NOT NULL,
customer_dob date NOT NULL);

When I run:
INSERT INTO customers VALUES ('Y',1,'07302012','John','Smith','1235 Main Street, Dunmore, PA 18512','1234567','jsmith@hotmail.com','07301980');

I recieve this:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.

I'm not sure where to go from here. Any help would be greatly appreciated.

Reply With Quote
  #2  
Old July 30th, 2012, 07:28 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 43rd Plane (26000 - 26499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,442 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 14 h 15 m 59 sec
Reputation Power: 4141
you forgot the list of columns

INSERT
INTO customers ( list,of,columns )
VALUES ( list,of,values )

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #3  
Old July 30th, 2012, 07:41 PM
eclypz00 eclypz00 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 3 eclypz00 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 3 m 2 sec
Reputation Power: 0
I've added the list of columns as well via another Q&A site.

INSERT INTO customers (member_yn,membership_number,date_became_member,customer_first_name,customer_last_name,customer_addr ess,customer_phone,customer_email,customer_dob) VALUES etc etc

It returned the same error as having no columns identified.

Reply With Quote
  #4  
Old July 30th, 2012, 07:57 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 43rd Plane (26000 - 26499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,442 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 14 h 15 m 59 sec
Reputation Power: 4141
Quote:
Originally Posted by eclypz00
It returned the same error as having no columns identified.
astonishing

the first query (without the list of columns) was trying to insert 'Y' into customer_id, 1 into membership_yn, '07302012' into membership_number, 'John' into date_became_member, and so on

so by sheer coincidence it was a date value that triggered the first error

now it looks like you have your values lined up with the proper columns, and again it's a date value that's triggering an error

which is the first date value that it encounters? what do you suppose might be wrong with that date format?

Reply With Quote
  #5  
Old July 30th, 2012, 08:18 PM
eclypz00 eclypz00 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 3 eclypz00 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 3 m 2 sec
Reputation Power: 0
Quote:
Originally Posted by r937
which is the first date value that it encounters? what do you suppose might be wrong with that date format?


I looked at your questions and tried again with the recommendation of listing the columns, as well as altering my date format to YYYY-MM-DD and it worked flawlessly.

I'm still rather confused on everything that has happened but I can say it works now and thank you very much for your help.

Here is my new input:

INSERT INTO customers(membership_yn,membership_number,date_became_member,customer_first_name,customer_last_name, customer_address,customer_phone,customer_email,customer_dob) VALUES ('Y',1,'2012-07-08','John','Smith','TempAddress','1234567890','jsmith@hotmail.com','1980-01-07');

Reply With Quote
  #6  
Old August 31st, 2012, 03:53 AM
Fear Naught Fear Naught is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Location: Folkestone, Kent, UK
Posts: 3 Fear Naught User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 30 sec
Reputation Power: 0
Your first mistake was not explicitly listing the columns that you wanted your data to go into. By default, as explained by others, the data will try to be inserted into the table from the first column et al.

Your second mistake was the format of your date. It needs to be in the format 'yyyy-mm-dd'. I also add times otherwise the time will automatically be set at the time the data was entered. I therefore use the format 'yyyy-mm-dd hh:mm'.

In your example I would have entered '2012-07-08 00:00'

HTH.

Reply With Quote
  #7  
Old August 31st, 2012, 06:20 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 43rd Plane (26000 - 26499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,442 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 14 h 15 m 59 sec
Reputation Power: 4141
Quote:
Originally Posted by Fear Naught
I also add times otherwise the time will automatically be set at the time the data was entered.
this is incorrect

Reply With Quote
  #8  
Old August 31st, 2012, 06:29 AM
Fear Naught Fear Naught is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Location: Folkestone, Kent, UK
Posts: 3 Fear Naught User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 30 sec
Reputation Power: 0
Quote:
Originally Posted by r937
this is incorrect


You are quite right. Many apologies for the error. However I still think it would be good practice to be explicit about times.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Microsoft SQL Server - Conversion Failed When Converting Dates

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap