Solved

sqlserver datetime conversion error

Posted on 2016-09-07
4
32 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 49

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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

679 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