Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

sql server insert query

Posted on 2016-09-08
3
Medium Priority
?
86 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 70

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 70

Accepted Solution

by:
Scott Pletcher earned 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

782 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