Solved

sqlserver datetime conversion error

Posted on 2016-09-07
4
34 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 51

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 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

636 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