Link to home
Start Free TrialLog in
Avatar of PeterBaileyUk
PeterBaileyUk

asked on

Sql server query

I have this
SELECT  smmt.[MVRIS CODE] + '_' + s.value + '_' AS ClientCodeWordPosition,SMMT.[MVRIS CODE], 'SMMT' AS ClientName,   s.value AS [Word],LEN(s.VALUE) AS WordLength, CONCAT(ISNULL(RTRIM(LTRIM(SMMT.[Model Range])),''), CHAR(32), ISNULL(RTRIM(LTRIM(SMMT.[RANGE SERIES])),''), CHAR(32), ISNULL(RTRIM(LTRIM(SMMT.[VARIANT])),'')) AS ConcatDesc, 
       row_number()  over (partition by SMMT.[MVRIS CODE] order by LEN(s.VALUE)) WordPosition,
	   count(*)  over (partition by SMMT.[MVRIS CODE]) NoOfWords,SMMT.MARQUE
       
FROM ClientData.[dbo].SMMT SMMT
CROSS APPLY STRING_SPLIT(CONCAT(ISNULL(RTRIM(LTRIM(SMMT.[Model Range])),''), CHAR(32), ISNULL(RTRIM(LTRIM(SMMT.[RANGE SERIES])),''), CHAR(32), ISNULL(RTRIM(LTRIM(SMMT.[VARIANT])),'')), CHAR(32)) s
WHERE smmt.MARQUE='PEUGEOT' AND SMMT.[MODEL RANGE]='ElDDIS' AND SMMT.[MVRIS CODE]='D39GA' AND s.VALUE <>' '

Open in new window


How can I use the result of wordPosition and add it to the end of [ClientCodeWodePosition]

ive attached a small spreadsheet to show

I am using the most recent version of sql server express
ee.xlsx
ASKER CERTIFIED SOLUTION
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PeterBaileyUk
PeterBaileyUk

ASKER

it squeeled on conversion so I guerst it needs a cast somewhere the clientcodewordposition field is an nvarchar

the error

Msg 8114, Level 16, State 5, Line 4
Error converting data type nvarchar to bigint.
clientcodewordposition is the concatenation of the clientcode + _ + s.value +_ + wordposition
i hadnt completed that part of the clientcodewordposition as I didnt know how
At line 1, try:

Select ClientCodeWordPosition + '_' + cast(WordPosition As Varchar(5))

brb
got it
Select ClientCodeWordPosition + '_' + CAST(WordPosition AS NVARCHAR(50))

Open in new window

aha we crossed
thank you, I shall try in future to follow your layout too