August 17, 2018 at 7:52 am
I am trying to update a company ID for a table and running into issues. I am wondering if someone can help me with a query. This is what I am using to update values. I have tried multiple methods but nothing is working. update [PMG].[Contactdata]
set CompanyNids = convert(int,LTRIM(RTRIM(s.CompanyNid)))
FROM [PMG].[Contactdata] c
join [Staging].[StgContactdata] s on c.ContactNids = s.ContactNid
where c.CompanyNids is NULL
error
Conversion failed when converting the nvarchar value ' 29840' to data type int.
August 17, 2018 at 7:57 am
It's most likely a leading tab instead of a leading space. LTRIM/RTRIM will only remove spaces.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
August 17, 2018 at 8:03 am
NewBornDBA2017 - Friday, August 17, 2018 7:52 AMI am trying to update a company ID for a table and running into issues. I am wondering if someone can help me with a query. This is what I am using to update values. I have tried multiple methods but nothing is working.update [PMG].[Contactdata]
set CompanyNids = convert(int,LTRIM(RTRIM(s.CompanyNid)))
FROM [PMG].[Contactdata] c
join [Staging].[StgContactdata] s on c.ContactNids = s.ContactNid
where c.CompanyNids is NULLerror
Conversion failed when converting the nvarchar value ' 29840' to data type int.
Try running it as a SELECT:SELECT c.CompanyNids cCompanyNids,
s.CompanyNid sCompanyNid,
convert(int,LTRIM(RTRIM(s.CompanyNid))) TrimsCompanyNid
FROM [PMG].[Contactdata] c
join [Staging].[StgContactdata] s on c.ContactNids = s.ContactNid
where c.CompanyNids is NULL
Then you might see what is wrong.
August 17, 2018 at 8:28 am
Jonathan AC Roberts - Friday, August 17, 2018 8:03 AMNewBornDBA2017 - Friday, August 17, 2018 7:52 AMI am trying to update a company ID for a table and running into issues. I am wondering if someone can help me with a query. This is what I am using to update values. I have tried multiple methods but nothing is working.update [PMG].[Contactdata]
set CompanyNids = convert(int,LTRIM(RTRIM(s.CompanyNid)))
FROM [PMG].[Contactdata] c
join [Staging].[StgContactdata] s on c.ContactNids = s.ContactNid
where c.CompanyNids is NULLerror
Conversion failed when converting the nvarchar value ' 29840' to data type int.
Try running it as a SELECT:
SELECT c.CompanyNids cCompanyNids,
s.CompanyNid sCompanyNid,
convert(int,LTRIM(RTRIM(s.CompanyNid))) TrimsCompanyNid
FROM [PMG].[Contactdata] c
join [Staging].[StgContactdata] s on c.ContactNids = s.ContactNid
where c.CompanyNids is NULL
Then you might see what is wrong.
This is just going to produce the same error. For troubleshooting, you should change the CONVERT to a TRY_CONVERT and/or use some other method to inspect the problem records.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
August 17, 2018 at 8:33 am
Done
update [Staging].[StgContactdata]
set CompanyNid = REPLACE(CompanyNid, char(9),'')
update [PMG].[Contactdata]
set CompanyNids = convert(int,s.CompanyNid)
FROM [PMG].[Contactdata] c
join [Staging].[StgContactdata] s on c.ContactNids = s.ContactNid
where c.CompanyNids is NULL
August 17, 2018 at 8:35 am
drew.allen - Friday, August 17, 2018 8:28 AMJonathan AC Roberts - Friday, August 17, 2018 8:03 AMNewBornDBA2017 - Friday, August 17, 2018 7:52 AMI am trying to update a company ID for a table and running into issues. I am wondering if someone can help me with a query. This is what I am using to update values. I have tried multiple methods but nothing is working.update [PMG].[Contactdata]
set CompanyNids = convert(int,LTRIM(RTRIM(s.CompanyNid)))
FROM [PMG].[Contactdata] c
join [Staging].[StgContactdata] s on c.ContactNids = s.ContactNid
where c.CompanyNids is NULLerror
Conversion failed when converting the nvarchar value ' 29840' to data type int.
Try running it as a SELECT:
SELECT c.CompanyNids cCompanyNids,
s.CompanyNid sCompanyNid,
convert(int,LTRIM(RTRIM(s.CompanyNid))) TrimsCompanyNid
FROM [PMG].[Contactdata] c
join [Staging].[StgContactdata] s on c.ContactNids = s.ContactNid
where c.CompanyNids is NULL
Then you might see what is wrong.This is just going to produce the same error. For troubleshooting, you should change the CONVERT to a TRY_CONVERT and/or use some other method to inspect the problem records.
Drew
Can try this then:SELECT c.CompanyNids cCompanyNids,
s.CompanyNid sCompanyNid,
LTRIM(RTRIM(s.CompanyNid)) TrimsCompanyNid
FROM [PMG].[Contactdata] c
join [Staging].[StgContactdata] s on c.ContactNids = s.ContactNid
where c.CompanyNids is NULL
and s.CompanyNid LIKE '%29840%'
August 17, 2018 at 8:51 am
Personally, my preferred method is to convert the string to VARBINARY, particularly since the grid view won't properly display many of the whitespace characters, so it can be difficult to determine exactly which one it is. Then again, I'm familiar enough with binary encodings of most of the common characters that it's easy for me to pick out which ones might be causing problems. Others might want to use the ASCII function, but that requires first finding where in the string the problem character is located.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
August 17, 2018 at 8:55 am
NewBornDBA2017 - Friday, August 17, 2018 8:33 AMDone
update [Staging].[StgContactdata]
set CompanyNid = REPLACE(CompanyNid, char(9),'')update [PMG].[Contactdata]
set CompanyNids = convert(int,s.CompanyNid)
FROM [PMG].[Contactdata] c
join [Staging].[StgContactdata] s on c.ContactNids = s.ContactNid
where c.CompanyNids is NULL
That takes two passed at the table. Incorporate the REPLACE as the target of the LTRIM/RTRIM in a single pass.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply