Solved

sqlserver datetime conversion error

Posted on 2016-09-07
4
21 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
  • 2
4 Comments
 
LVL 45

Accepted Solution

by:
Vitor Montalvão earned 500 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 48

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 48

Expert Comment

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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now