January 30, 2011 at 9:59 pm
I have to display first 4 letters of a country in a column [country_sub], both country & country_sub have same data the objective is to take a reference from country column and update country_sub column
This is what I am doing
update test set country_sub = (select (substring(country,1,4)) from test where country like '%ENGLAND%')
Subquery returned more than 1 value .This is not permitted when the subquery follows =,
if I run this query using select statement independently it works fine displaying multiple rows which have above pattern but I get above error when I embed select in front of update.
I am new bee and need pointers on how to fix it.
January 30, 2011 at 10:20 pm
Hi,
Please use below query, surely it will work for you... 🙂
update test set country_sub = (substring(country,1,4)) from test t where t.country like '%ENGLAND%' AND t.country = country
January 31, 2011 at 10:53 am
The reason it is giving you the error is because you are trying to assign multiple values to a single row. If you run the SELECT statement by itself you could get more than one row back, if you do the query has no idea which value to assign to the column.
Why do you have the WHERE clause in there? Are you only trying to update certain rows, if so then you need to move the WHERE so it is outside of the subquery, something like this should work:
update test set country_sub = substring(country,1,4) where country like '%ENGLAND%'
That will update the country_sub column of every row whose country value has 'ENGLAND' in it somewhere.
Hope that helps
January 31, 2011 at 8:55 pm
Hi Dave, You are right that "it is giving you the error is because you are trying to assign multiple values to a single row." But if this is the requirement then the where clause should have one more condition which I mentioned in my previous reply. For your reference you can see the below query and definetly it will work.
update test set country_sub = (substring(country,1,4)) from test t where t.country like '%ENGLAND%' AND t.country = country
Hope now its clear:-)
February 1, 2011 at 8:43 am
rsk15 (1/30/2011)
I have to display first 4 letters of a country in a column [country_sub], both country & country_sub have same data the objective is to take a reference from country column and update country_sub columnThis is what I am doing
update test set country_sub = (select (substring(country,1,4)) from test where country like '%ENGLAND%')
Subquery returned more than 1 value .This is not permitted when the subquery follows =,
if I run this query using select statement independently it works fine displaying multiple rows which have above pattern but I get above error when I embed select in front of update.
I am new bee and need pointers on how to fix it.
I have to ask... what are you going to do for countries that begin with words like North, South, East, or West? Multiple countries will appear the same.
--Jeff Moden
Change is inevitable... Change for the better is not.
February 1, 2011 at 10:58 am
Thanks Deepak, I wasn't questioning you answer, just giving the OP another option and explaining to them why they got the error message. Without more info from them on what they are trying to accomplish we can't say for sure exactly which approach is right.
February 1, 2011 at 8:43 pm
Yeah...
I agree.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply