Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sqlserver datetime conversion error

Posted on 2016-09-07
4
Medium Priority
?
36 Views
Last Modified: 2016-09-07
I am trying to insert into a table but its giving me an error message and I dont know how to deal with it.

the columns in the destination are as per attached

word is a string containing one wordof nvarchar(50)
MyDateTime is the date and time
the primary key is made up of the word and datetime.


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

use Dictionary

insert into TblCurrentWords (Word_ID, Word, MyDateTimeCol)

select Word + GETDATE() AS CurrentDateTime, word, GETDATE() AS MyDateTimeCol
from TblWords
where word is not null and PATINDEX('%[0-9]%',Word)=0
group by word
order by word

Open in new window

ex
0
Comment
Question by:PeterBaileyUk
[X]
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
  • 2
4 Comments
 
LVL 52

Accepted Solution

by:
Vitor Montalvão earned 2000 total points
ID: 41787612
There's no implicit conversion from datetime to string so you'll need to do that explicitly by using CONVERT function:
use Dictionary

insert into TblCurrentWords (Word_ID, Word, MyDateTimeCol)
select Word + CONVERT(CHAR(17),GETDATE(),120) AS CurrentDateTime, word, GETDATE() AS MyDateTimeCol
from TblWords
where word is not null and PATINDEX('%[0-9]%',Word)=0
group by word

Open in new window

NOTE: Being an INSERT you won't need the data to be returned ordered.
0
 

Author Closing Comment

by:PeterBaileyUk
ID: 41787621
Thankyou once again
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 41787623
the primary key is made up of (a concatenation of) the word and datetime.

That is NOT a good way to define the table

You could make the unique key a combination of word and a datetime column, but do not concatenate them
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 41787624
oh no too late...
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

704 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