Solved

sql server insert query

Posted on 2016-09-08
3
31 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:ScottPletcher
Comment Utility
String concatenation in SQL Server uses "+" not "&".  & is only for bit-related operations.  For example:

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

Author Comment

by:PeterBaileyUk
Comment Utility
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:
ScottPletcher earned 500 total points
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

762 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

11 Experts available now in Live!

Get 1:1 Help Now