PeterBaileyUk
asked on
odd characters inserted at end of string during sql server update
I have this query, it was part of another ee question but I cannot find the id now.
the query is working but ive spotted some odd additions at the end of the string output which shouldnt be there
the query is below. ive highlighted the attachment to show whats added
ive attached an xml output, i suspect its that space at the end causing this, can i get rid of that during the update sql, if indeed thats what it is.
the query is working but ive spotted some odd additions at the end of the string output which shouldnt be there
the query is below. ive highlighted the attachment to show whats added
ive attached an xml output, i suspect its that space at the end causing this, can i get rid of that during the update sql, if indeed thats what it is.
<TblWords ClientCodeWordPosition="07130219__5" StrFull="320 D GT LUXURY " StrShort="320 D GT LUXURY &#x20;" />
<TblWords ClientCodeWordPosition="07130219_320_1" StrFull="320 D GT LUXURY " StrShort="320 D GT LUXURY &#x20;" />
<TblWords ClientCodeWordPosition="07130219_D_2" StrFull="320 D GT LUXURY " StrShort="320 D GT LUXURY &#x20;" />
<TblWords ClientCodeWordPosition="07130219_GT_3" StrFull="320 D GT LUXURY " StrShort="320 D GT LUXURY &#x20;" />
<TblWords ClientCodeWordPosition="07130219_LUXURY_4" StrFull="320 D GT LUXURY " StrShort="320 D GT LUXURY &#x20;" />
use Dictionary
go
WITH Filtered
AS ( SELECT W.Word, w.WordPosition, w.clientcode, w.ClientCodeWordPosition, w.StrShort
FROM tblwords w
LEFT JOIN tblwordtags wt ON W.clientcodeWordPosition = wt.clientcodeWordPosition
WHERE wt.word IS NULL)
UPDATE tblwords
SET StrShort = RTRIM((SELECT I.Word + ' '
FROM Filtered I
WHERE I.clientcode = tblwords.clientcode
ORDER BY i.WordPosition
FOR XML PATH('')))
ee.JPG
ASKER
the output on this one had a where so as to produce 1 client code but what if that six vary in the other rows, anyway i will try it now see what happens.... it failed with this output message:
Msg 537, Level 16, State 3, Line 4
Invalid length parameter passed to the LEFT or SUBSTRING function.
Msg 537, Level 16, State 3, Line 4
Invalid length parameter passed to the LEFT or SUBSTRING function.
ASKER
I believe its doing this where there are no rows in the join table.
ASKER
so the sql is working fine, I just need to ensure that it doesnt update strshort on an unjoined row or maybe just look for the 6 characters and remove them after the event
Sorry, I just assumed by your example that all records have those 6 characters. So you might need to test it first:
use Dictionary
go
WITH Filtered
AS ( SELECT W.Word, w.WordPosition, w.clientcode, w.ClientCodeWordPosition, w.StrShort
FROM tblwords w
LEFT JOIN tblwordtags wt ON W.clientcodeWordPosition = wt.clientcodeWordPosition
WHERE wt.word IS NULL)
UPDATE tblwords
SET StrShort = RTRIM((SELECT CASE CHARINDEX(' ',I.Word)
WHEN 0 THEN I.Word + ' '
ELSE LEFT(I.Word, CHARINDEX(' ',I.Word)-1) + ' '
END Word
FROM Filtered I
WHERE I.clientcode = tblwords.clientcode
ORDER BY i.WordPosition
FOR XML PATH('')))
ASKER
Those odd characters are still returned and it returned tags which is not right it may be better for me to update the row after the function and remove those specific odd characters
heres a line its got tags of <word> and the offending characters as well.
heres a line its got tags of <word> and the offending characters as well.
ClientCodeWordPosition StrFull StrShort StrShortTag
13414B__4 VN1700 CLASSIC TOURER <Word>VN1700 </Word><Word>CLASSIC </Word><Word>TOURER </Word><Word> </Word> NULL
ASKER
i found a different type  A8 HYBRID  
so it can appear anywhere but always the same characters.
i am still reluctant to alter the working function and just have another update process to remove those
  without leaving duplicates spaces after
so it can appear anywhere but always the same characters.
i am still reluctant to alter the working function and just have another update process to remove those
  without leaving duplicates spaces after
ASKER
heres a xml for the one before.
<TblWords ClientCodeWordPosition="04134932__1" StrFull=" A8 HYBRID" StrShort="&#x20;A8 HYBRID &#x20;" />
<TblWords ClientCodeWordPosition="04134932__4" StrFull=" A8 HYBRID" StrShort="&#x20;A8 HYBRID &#x20;" />
<TblWords ClientCodeWordPosition="04134932_A8_2" StrFull=" A8 HYBRID" StrShort="&#x20;A8 HYBRID &#x20;" />
<TblWords ClientCodeWordPosition="04134932_HYBRID_3" StrFull=" A8 HYBRID" StrShort="&#x20;A8 HYBRID &#x20;" />
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
as a trial i did an update on strfull with a left and right trim and the characters still appear so your right its not space related
ASKER
it didnt do the replace heres some output, i think its the right track though just a question as weather it can be done in function
ClientCodeWordPosition StrFull StrShort StrShortTag
04134932__1 A8 HYBRID  A8 HYBRID   NULL
04134932__4 A8 HYBRID  A8 HYBRID   NULL
04134932_A8_2 A8 HYBRID  A8 HYBRID   NULL
04134932_HYBRID_3 A8 HYBRID  A8 HYBRID   NULL
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok I took your approach and did it post update of strshort, the strings are now fine, its a pain but for now i think its ok, if it starts producing other strings then i will post back.
Strange that it didn't work in the original query but glad that you sorted it out.
Cheers
Cheers
I don't think the issue is with the space since RTRIM is there to get rid of it. The issue might be with the last 6 chars so try this:
Open in new window