Solved

sql server insert query

Posted on 2016-09-08
3
43 Views
Last Modified: 2016-09-08
Ive created a query but have a syntax error with the underscores.

I wonder if someone could assist, I dont think I am far out.

use dictionary


insert into dbo.TblCurrentWordCounts (WordStat_ID,  Word, NoOfWords, WordLength, WordPosition, ClientCode, MyDateTimeCol,ClientCodeWordPosition, ClientCodeWordWordCountWordLenPosition)

select Word + CONVERT(CHAR(17),GETDATE(),120) AS CurrentDateTime, GETDATE() AS MyDateTimeColWord, Word, NoOfWords, wordlen, WordPosition, ClientCode, ClientCode & '_'& word &'_'& WordPosition,  ClientCode & '_ '& word &'_ '& NoOfWords &'_ '& WordLen &'_ '& WordPosition
from TblWords
where word <>'' and clientcode='210904C'
--where word <>''
group by word, NoOfWords, ClientCode, wordlen, WordPosition
order by clientcode

Open in new window

ee2.JPG
0
Comment
Question by:PeterBaileyUk
  • 2
3 Comments
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 41789853
String concatenation in SQL Server uses "+" not "&".  & is only for bit-related operations.  For example:

...ClientCode + '_'+ word +'_'+ WordPosition,...
0
 

Author Comment

by:PeterBaileyUk
ID: 41789871
i tried that first and it failed with
Msg 245, Level 16, State 1, Line 4
Conversion failed when converting the varchar value '_ ' to data type int.

use dictionary


insert into dbo.TblCurrentWordCounts (WordStat_ID,  Word, NoOfWords, WordLength, WordPosition, ClientCode, MyDateTimeCol,ClientCodeWordPosition, ClientCodeWordWordCountWordLenPosition)

select Word + CONVERT(CHAR(17),GETDATE(),120) AS CurrentDateTime, GETDATE() AS MyDateTimeColWord, Word, NoOfWords, wordlen, WordPosition, ClientCode, ClientCode + '_'+ word +'_'+ WordPosition,  ClientCode + '_ '+ word +'_ '+ NoOfWords +'_ '+ WordLen +'_ '+ WordPosition
from TblWords
where word <>'' and clientcode='210904C'
--where word <>''
group by word, NoOfWords, ClientCode, wordlen, WordPosition
order by clientcode

Open in new window

0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 41789878
You must CAST any non-char values to char in order to concatenate a char string.  For example:

...CAST(ClientCode AS varchar(10)) + '_'+ word +'_'+ CAST(WordPosition AS varchar(10)),...

Otherwise, SQL tries to convert the strings to numbers in order to add them.

That's logical given the use of the over-loaded "+" operator for both addition and concatenation.  SQL must have rules to provide consistent results.

For example, what is the result of:
SELECT '1' + 4
Is it 5 or '14'?  It's 5, because by default any numeric value forces all values to be converted to numeric.  Note that:
SELECT '1' + '4'
does indeed result in '14'.  Based on SQL's conversion rules, this:
SELECT 1 + 'D'
fails, because "D" can't be converted to a numeric value.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
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…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

863 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

28 Experts available now in Live!

Get 1:1 Help Now