I'm extremely new to SQL (started learning 3 days ago), so I'm assuming I have some horrendous syntax errors in my code.
I've been at it since yesterday, trying to create an update query that uses cases to modify the value column based on the following conditions:
When value contains the letter 'a', make value uppercase.
When value contains the letter 'd', but doesn't contain the letter 'a', make value lowercase.
When value doesn't meet the conditions above, change the first and last letters from value to 1 and 2.
I have tried to do this a bunch of different ways, but I always end up with some kind of error. This code returns Missing Keyword Error:
UPDATE t1
SET value =
CASE WHEN value LIKE '%a%' THEN SET value = UPPER(value)
WHEN value LIKE '%d%' AND value NOT LIKE '%a' THEN SET value = LOWER(value)
ELSE REPLACE(value,'_%','1') AND REPLACE(value,'%_','2')
END
Where value IS NOT NULL;
CASE returns a value so you don't need the SET there. AND is a conditional operator, it's not for running commands together.
REPLACE() does not work the way you're trying to use it. It takes literals not wildcards. So unless value starts and finishes with underscores, '_%' the command won't change anything. Instead of REPLACE() I suggest you use SUBSTR() and concatenate the replacement characters.
UPDATE t1
SET value =
CASE WHEN value LIKE '%a%' THEN UPPER(value)
WHEN value LIKE '%d%' AND value NOT LIKE '%a' THEN LOWER(value)
ELSE '1'||substr(value, 2, length(value)-2) ||'2'
END
Where value IS NOT NULL;
Alternatively, you could use the regular expression replace function
ELSE regexp_replace(value, '^(.)(.*)(.)$', '1\22'
However, this has slightly different output when the length of value is 1.
there are many syntex error in Case statment you cant use again SET operation and you cant use separated replace
change
1.SET value = UPPER(value) to UPPER(value)
2.SET value = LOWER(value) to LOWER(value) and
3. remove secondreplace like this
UPDATE t1
SET value =
CASE WHEN value LIKE '%a%' THEN SET UPPER(value)
WHEN value LIKE '%d%' AND value NOT LIKE '%a' THEN LOWER(value)
ELSE REAPLACE(REPLACE(value,'_%','1'),'%_',2)
END
Where value IS NOT NULL;
Related
I've got a query pulling data from a table. In one particular field, there are several cases where it is a zero, but I need the four digit location number. Here is where I'm running into a problem. I've got
SELECT REPLACE(locationNbr, '0', '1035') AS LOCATION...
Two issues -
Whoever put the table together made all fields VARCHAR, hence the single quotes.
In the cases where there already is the number 1035, I get 1103535 as the location number because it's replacing the zero in the middle of 1035.
How do I select the locationNbr field and leave it alone if it's anything other than zero (as a VARCHAR), but if it is zero, change it to 1035? Is there a way to somehow use TO_NUMBER within the REPLACE?
SELECT CASE WHEN locationNbr='0' THEN '1035' ELSE locationNbr END AS LOCATION...
REPLACE( string, string_to_replace , replacement_string )
REPLACE looks for a string_to_replace inside a string and replaces it with a replacent_string. That is why you get the undesired behaviour - you are using the wrong function.
CASE WHEN condition THEN result1 ELSE result2 END
CASE checks a condition and if it is true it returns result1 and if it is not it will return result2. This is a simple example, you can write a case statement with more than one condition check.
Don't use replace(). Use case:
(case when locationNbr = '0' then '1035' else locationNbr end)
You can make use of length in Oracle:
select case when length(loacation) = 1 then REPLACE(loacation, '0', '1035') else loacation end as location
from location_test;
I have a question concerning the lower function.
,case when lower(replace(producttitle, ‘ ‘, ‘‘)) in (‘microphone‘) then ‘YES‘ else ‘NO‘ end
The statement above does select only YES, when column value = value in the IN function.
I want to get a YES when the value such as it is written in the IN function. When the column value is ‘Microphone‘, i want to get it lowercase —> microphone and because the IN containts ‘microphone‘, I should get a YES, right?
When the column value is Microphone (upper B), why do I not get it lower? Such as in the IN function.
Example: column value: ‘Microphone‘
I want to get a YES by making the column value lower and because the IN function contains the value (‘microphone‘)
Thank you
You need to change the collation to make it case sensitive, by default it is case insensitive :
CASE WHEN REPLACE(producttitle, ' ', '') = 'microphone' COLLATE SQL_Latin1_General_CP1_CS_AS
THEN 'YES' ELSE 'NO'
END
I am attempting to update several values in a table based on several conditions. If a column (let's call it "name") contains some set of strings, I would like to change the name to something else. For example, if name contains two consecutive a's, like "aa", I would like to change it to just "A".
Here is my query that is currently not working:
UPDATE table
SET name = CASE name
WHEN name like "%aa%" then "A"
WHEN name like "%er%" then "E"
ELSE "Z"
END
WHERE name is not null
I know it is a non-sensical example, but I'm not great with coming up with those on the spot. The error I am currently getting is:
"No matching signature for operator CASE for argument types: STRING, BOOL, STRING, BOOL, STRING, STRING at [2:18].
Any thoughts?
Remove name from CASE name and replace double quotes " with single quotes ':
UPDATE table
SET name = CASE
WHEN name like '%aa%' then 'A'
WHEN name like '%er%' then 'E'
ELSE 'Z'
END
WHERE name is not null
P.S. in this example and your question query the word table is the name of the table not the keyword table. That means that in some databases you will have problems and you will have to put this word in quotes.
In this DEMO you can see that the query from my answer will work in MySQL database when you put word table in quotes.
In this DEMO you can see that the query from my answer will work in Oracle database when you put word table in double quotes. Also you will see that the strings(values of columns) can not be between double quotes in Oracle.
Note: Some of the databases will not cause errors if the string(column value) is between double quotes but it is better to use single quotes for this and because you have not tagged a database that you use this are some guidelines that will help you.
Remove the first name in the CASE clause:
UPDATE table
SET name = CASE WHEN name like "%aa%" then "A"
WHEN name like "%er%" then "E"
ELSE "Z"
END
WHERE name is not null
Exactly what The Impaler said,remove the first name in the CASE clause. SQL syntax does not call for the column name directly after case. For example (From W3 schools):
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN resultN
ELSE result
END;
https://www.w3schools.com/sql/sql_case.asp
Good luck!
Remove the first instance of the word name
UPDATE table
SET name = CASE
WHEN name like "%aa%" then "A"
WHEN name like "%er%" then "E"
ELSE "Z"
END
WHERE name is not null
There are two ways to use case:
1) CASE WHEN name=1 THEN WHEN name=2 THEN...
2) CASE name WHEN 1 THEN WHEN 2 THEN...
In the first case it looks for the first TRUE expression, in the second for the first expression that is equal to name.
You made a mix of the two, but for the interpreter it is the second form.
Therefore, it was looking for the first expression equal to name. When it reads the first WHEN, it checks whether name is equal to the argument (name like "%aa%")
But (name like "%aa%") is a boolean, name is a string, hence the error.
I am trying to compare a variable in db 2 sp using like, however it always goes to the else part of the statement, can someone correct the syntax here..here is the part of the code
do
IF(#variable like '%abc') THEN
set #anotherVariable='abc';
ELSEIF (#variable like '%def') THEN
set #anotherVariable='def';
ELSEIF (#variable like '%def') THEN
set #anotherVariable='def';
ELSE
set #anotherVariable='xyz';
END IF;
END FOR;
This code is part of a cursor, query always returns 1 value, however my comparison is not working(incorrect syntax?), it always goes to the last else as if it never was able to match. I know that value is there but its not comparing in this manner...Thanks
Are you writing SQL-PL code? Because variables are referenced directly by its name, not preceded by the '#' sign.
Another thing is that you are using the like operator outside a query. 'Like' is not a function, it is a predicate: http://pic.dhe.ibm.com/infocenter/db2luw/v10r5/topic/com.ibm.db2.luw.sql.ref.doc/doc/r0000751.html
Instead, you can use a 'case' in the select and then return the corresponding value. In that way you do not need to do an 'if-else': http://pic.dhe.ibm.com/infocenter/db2luw/v10r5/topic/com.ibm.db2.luw.sql.ref.doc/doc/r0023458.html
I have a query that returns a result set which contains a certain column that needs some tweaking. Basically, in the result set, there are certain rows that contain a blank value for the applicable column. What I need to do is set all instances of that blank value to a specific string. I have tried declaring a variable and setting the variable equal to the column name (using a SELECT statement) and then using an IF statement to set the value to a specific string if it is blank (' '). My code thus far is as follows:
declare #sourceNode varchar(30)
set #sourceNode = (select sn_name from pt_cust)
if #sourceNode = '' begin
set #sourceNode = 'None'
end
This code returns an error stating that the sub-query returns more than 1 value. This seems like an easy task but I am stuck at the moment. How can this be accomplished?
This is a case (heh) for CASE:
SELECT CASE WHEN sn_name = '' THEN 'None' ELSE sn_name END
FROM pt_cust
You are getting the error message you mentioned because your SELECT statement can return more than one row, in which case it can not be assigned to a variable.