Tsql: LOWERCASE using with IN - sql

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

Related

Case sensitive for a specific column

I am using Microsoft SQL Management Studio V18. I have a column where I need to find if it contain any lower case alphabets. See below for desired output:
Material#
Material_Desc
Contain_Lower
123
PRODUCT DESCRIPTION
Yes
213
Product Description
No
Since the default collation is case insensitive I just want to turn it on for that specific column.
I've tried the following however it does not work - it does not identify if row2 contain a lower case alphabet. What can I do to identify if the column has a lower case?
select
table.Material_Desc,
Case when
table.Material_Desc collate SQL_Latin1_General_CP1_CS_AS = table.Material_Desc
Then 'No' else 'Yes' end as Contain_Lower
from table
You are on the right way, but you just forced the column to UPPER CASE. You also need to compare:
SELECT
material_desc,
CASE WHEN material_Desc = UPPER(material_desc) COLLATE SQL_Latin1_General_CP1_CS_AS
THEN 'No' ELSE 'Yes' END AS contain_lower
FROM table;

REPLACE not doing what I need in SQL

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;

SQL computed column based on a special character on another column

I am not good with SQL at all, barely have an idea on how to do basic scripts suck as delete, drop, add.
I have this data with about 12 columns, I want to add a calculated column which will change depending if a special character shows up in another column.
lets say
A C
Money$ YES
Money NO
that is the idea, I want to create a column C where it says yes if there is a $ sign on the column A. Is this possible? I am assuming you can use something similar to an if condition but I have no experience with SQL scripting.
You would use a case expression and like:
select t.*,
(case when a like '%$%' then 'YES' else 'NO' end) as c
from t;
The following is just commentary.
This is very basic syntax for SQL. I would recommend that you spend some time to learn the basics. Learning-as-you-go is an okay approach -- assuming you have some fundamentals to build on. Otherwise, you are likely to spend a lot of time to learn a few things, and you may not learn the best way to do things.
yes, this is possible. you'll have to replace the parts in braces ({}) with the appropriate object names. I also use a bit rather than 'Yes'/'No'; as that seems better suited:
ALTER TABLE {YourTable} ADD {New Column Name} AS CONVERT(bit, CASE WHEN {Column} LIKE '%$%' THEN 1 ELSE 0 END) PERSISTED;
Note that this will return 0 if the column ({Column}) has a value of NULL, not NULL; unsure if this is the correct logic however, this should be more than enough to get the ball rolling. If not, read up on the CASE expression and NULL logic.
Regexp match can help you find out if there is a character you consider as special char in the strings:
SELECT
ColumnA
, SUBSTRING(ColumnA, PATINDEX('%[^ a-zA-Z0-9]%', ColumnA), 1) AS FirstSpecialChar
WHERE
ColumnA LIKE '%[^ a-zA-Z0-9]%'
;
The pattern [^ a-zA-Z0-9] will match on any character which is not a number, a space or an alphabetic character (note the ^ at the beginning of the character group - that mean NOT)
You can use regex to check any special character in column EX:
SQL SERVER
SELECT CASE WHEN 'ABCD$' Like '%[^a-zA-Z0-9]%' 1 THEN 'YES' ELSE 'NO' END as result
MYSQL
SELECT CASE WHEN 'ABCD$' REGEXP '[^a-zA-Z0-9]' = 1 THEN 'YES' ELSE 'NO' END as result
Regex can be changed as per the requirement
REGEXP '[^[:alnum:]]'

Oracle SQL- Update Query with Case Statement Missing Keyword Error

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;

Assign a case value to a column rather than an alias

This should be a simple one, but I have not found any solution:
The normal way is using an alias like this:
CASE WHEN ac_code='T' THEN 'time' ELSE 'purchase' END as alias
When using alias in conjunction with UNION ALL this causes problem because the alias is not treated the same way as the other columns.
Using an alias to assign the value is not working. It is still treated as alias, though it has the column name.
CASE WHEN ac_code='T' THEN 'time' ELSE 'purchase' END as ac_subject
I want to assign a value to a column based on a condition.
CASE WHEN ac_code='T' THEN ac_subject ='time' ELSE ac_subject='purchase' END
Now I get the error message
UNION types character varying and boolean cannot be matched
How can I assign a value to a column in a case statement without using an alias in the column (shared by other columns in UNION)?
Here is the whole (simplified) query:
SELECT hr_id,
CASE WHEN hr_subject='' THEN code_name ELSE hr_subject END
FROM hr
LEFT JOIN code ON code_id=hr_code
WHERE hr_job='123'
UNION ALL
SELECT po_id,
CASE WHEN po_subject='' THEN code_name ELSE po_subject END
FROM po
LEFT JOIN code ON code_id=po_code
WHERE po_job='123'
UNION ALL
SELECT ac_id,
CASE WHEN ac_code='T' THEN ac_subject='time' ELSE ac_subject='purchase' END
FROM ac
WHERE ac_job='123'
There is no alias in your presented query. You are confusing terms. This would be a column alias:
CASE WHEN hr_subject='' THEN code_name ELSE hr_subject END AS ac_subject
In a UNION query, the number of columns, column names and data types in the returned set are determined by the first row. All appended rows have to match the row type. Column names in appended rows (including aliases) are just noise and ignored. Maybe useful for documentation, nothing else.
The = operator does not assign anything in a SELECT query. It's the equality operator that returns a boolean value. TRUE if both operands are equal, etc. This returns a boolean value: ac_subject='time' Hence your error message:
UNION types character varying and boolean cannot be matched
The only way to "assign" a value to a particular output column in this query is to include it at the right position in the SELECT list.
The information in the question is incomplete, but I suspect you are also confusing the empty string ('') with the NULL value. A distinction that you need to understand before doing anything else with relational databases. Maybe start here. In this case you would rather use COALESCE to provide a default for NULL values:
SELECT hr_id, COALESCE(hr_subject, code_name) AS ac_subject
FROM hr
LEFT JOIN code ON code_id=hr_code
WHERE hr_job = '123'
UNION ALL
SELECT po_id, COALESCE(po_subject, code_name)
FROM po
LEFT JOIN code ON code_id=po_code
WHERE po_job = '123'
UNION ALL
SELECT ac_id, CASE WHEN ac_code = 'T' THEN 'time'::varchar ELSE 'purchase' END
FROM ac
WHERE ac_job = '123'
Just an educated guess, assuming type varchar. You should have added table qualification to column names to clarify their origin. Or table definitions to clarify everything.
The CASE expression is supposed to return a value, e.g. 'time'.
Your value is another expression subject ='time' which is a boolean (true or false).
Is this on purpose? Does the other query you glue with UNION have a boolean in that place, too? Probably not, and this is what the DBMS complains about.
I found the problem.
CASE WHEN hr_subject=’’ THEN code_name ELSE hr_subject END
The columns code_name and hr_subject was different length. This caused the unpredictable result. I think that aliases can work now.
Thank you for your support.