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;
Related
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 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:]]'
I've never posted before, I've usually been able to find my answer, so please bear with me if I exclude anything or format incorrectly. Here is my issue:
I have two columns that include similar data, for example:
Column1 Column2
BASKETBALL BasketBall-CAVS
BASEBALL REDS Baseball-SPORTS
I am not searching for a specific word, just any word that is found in both columns should populate a "no" under a third column. So both of the above records should return a NO. I attempted this:
CASE
WHEN Column1 NOT LIKE
CONCAT('%',Column2,'%') THEN 'Yes'
ELSE 'No'
END AS Needs_Updated;
For some reason, only the first record pictured above returns a "No" in the "Needs_Updated" column. The second record returns a "Yes" (which isn't correct, two words match in that column). The only difference in the second record is there is multiple words in column 1. Can anyone help me out? Thanks in advance!
I think you can do what you want with regular expressions. Not all databases support them, so this is tricky with those databases.
Using MySQL syntax:
select t.*,
(case when column2 regexp replace(column1, ' ', '|') then 'no' else 'yes' end) as needs_updated
from t;
Have you tried Charindex
SELECT CASE WHEN CHARINDEX(Column2,Column1) > 0 ) THEN 'NO' ELSE 'YES' AS Needs_Updated
CHARINDEX ( expressionToFind , expressionToSearch [ , start_location ])
start_location is optional
https://learn.microsoft.com/en-us/sql/t-sql/functions/charindex-transact-sql
I have a column that is of type 'bit'. I want the column to display either the word yes (1) or (0) if no. Is there somewhere I can set this?
If not is there a replacement for boolean I can use? True/false will do fine as a replacement
I am using SQL Server-management 2008 R2
I'm trying to amend the table.
Try using case:
select case
when MyBitField = 1 then
'yes'
else
'no'
end
from MyTable
I have a table Part(No,Desc,Draw).
I want to return a list of all the parts in the table in this form :
Part Number| Description | Is Packed ? (Yes/No)|
------------------------------------------------
In the result set, the Is Packed header should be Yes if the Draw column is not null and No if otherwise.
I'm using PL/SQL.
I don't know how to do this.
SELECT No AS "Part Number", Desc AS "Description",
CASE
WHEN Draw IS NOT NULL THEN 'Yes'
ELSE 'No'
END AS "Is Packed"
FROM Part
You might need to escape Desc in field list since it's a reserved word.
As an alternative to CASE (see other answers) in this case you can use NVL2:
SELECT No AS "Part Number", Desc AS "Description", NVL2 ( DRAW, 'YES', 'NO' ) AS "Is Packed" FROM PART
BTW: you should neve create a DB object (table/column...) with a name which is a reserved word (in you case Desc) - this can lead to really strange things...
Use CASE:
CASE WHEN Draw IS NOT NULL THEN 'Yes'
ELSE 'No'
END AS column_alias