Avatar of PeterBaileyUk
PeterBaileyUk
 asked on

sql server insert query

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
Microsoft SQL Server

Avatar of undefined
Last Comment
Scott Pletcher

8/22/2022 - Mon
Scott Pletcher

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

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

ASKER
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

ASKER CERTIFIED SOLUTION
Scott Pletcher

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes