Is database record is case sensitive? - sql

I have two tables City and CityCommunity in City table one column contain cityName like this
cityName = 'ABC'
And in CityCommunity table we have same column cityName but value of this columnName is something like this
cityName = 'abc'
Will it effect in query when we will run a Join query. In my case when I am joining these two table in the above case when both cityName same but only difference is case sensitive will it effect query to run

By default it is not case sensitive. So it will treat 'ABC' and 'abc' as same. But to make it case sensitive you have to use COLLATE.
You can find the detailed explanation with example HERE . This applys to join also.

In Oracle Yes, the results are case sensitive so if you are looking for
cityName = 'ABC'
the results with
cityName = 'abc' or cityName ='Abc'
won't show!
You need to add UPPER so you can get all results
SELECT UPPER(cityName)
FROM City;
in this case, no matter what case are the results it will convert the whole column data to upper case

Related

SQL Server connecting several Selects

I have the following statement:
Select No, Region = 'Ohio'
FROM table
where PostCode >='0001'
AND PostCode <= '4999'
which updates me the table with the correct state in the field Region. How can I expand that statement with several other WHERE conditions in the same statement?
e.g.
Region = 'NewYork'
Where PostCode >='5000'
AND PostCode <= '7999'
My solution would be to build several Statements, for each Region, but there must be a better way having them all in one.
Two common ways to select/set different values based on multiple criteria in a single query are case statements and doing a join on another table with those values. I should also point out that you can take advantage of the between operator in SQL server for much of this.
CASE statements in a single query
A case statement might be useful if you have a small set of criteria, or if you just need to throw together an adhoc query. Here is an example of using a case statement:
select
No,
Region = case
when (PostCode >= '0001' and PostCode <= '4999')
'Ohio'
when (PostCode between '5000' and '7999')
'NewYork'
else
'Unknown'
end
from [...]
JOIN a table with the values and criteria
This is definitely the better method for something like evaluating 50 states - especially since this data is likely static. The idea is that you will want to have a table that contains the criteria and the value, and then join it to the table.
Here is an example using a temp table - you would likely want to use a real table for something as common as states.
-- Setup a #states table
create table #states (state varchar(20), PostCodeMin char(4), PostCodeMax char(4))
insert into #states values ('Ohio', '0001', '4999')
insert into #states values ('NewYork', '5000', '7999')
-- Now query it
select
t.No,
State = isnull(s.state, 'Unknown')
from
my_table t
left outer join #states s
on (t.PostCode between s.PostCodeMin and s.PostCodeMax)
Note that in the above query, I do a left outer join to #states, in case the state isn't setup. I also select the State using isnull, in case the outer join doesn't return anything for that particular row in my_table.
You can create a calculated field using a case statement on region. If there is going to be many "Unknown" records returned then you may want to tweak the WHERE clause to filter out nonessential records for better performance.
SELECT
*
FROM
(
Select
No,
Region =
CASE
WHEN PostCode >'0001' AND PostCode <='4999' THEN 'Ohio'
WHEN PostCode >'5000' AND PostCode <='7999' THEN 'New York'
ELSE
'Unknown'
END
FROM table
where PostCode >='0001' AND PostCode <= '7999'
)AS X
ORDER BY
Region

Building a SELECT clause with dynamic number of columns

Is it possible to create a SELECT clause with a varying number of columns to be returned depending on joined tables?
For instance.
If I join a table depending on a value in the WHERE-clause I want to return either tbl1.col1, tbl1.col2 if tabl tbl1 is joined or tbl2.col4, tbl2.col5, tbl2.col8 if table tbl2 is joined.
Is this possible? How?
No, you can't write one query that sometimes returns n columns and another time m columns. What you can do is something like this: Use UNION ALL on two queries with conditions that either query 1 or query 2 Returns data. Have columns match, so where one query has no value let it select null in this place.
select tbl1.col1 as firstname, tbl1.col2 as lastname, null as street, tbl1.col3 as job as street from ...
where #variable = 1
UNION ALL
select tbl2.col4 as firstname, tbl2.col5 as lastname, tbl2.col8 as street, null as job from ...
where #variable = 2;
Or you just build your SQL dynamically with whatever language and use completely different SQL, which is what one would normally do.

Wildcards in sql

How does wildcards works in sql. If I do select * from table it give all the fields. But if I do select a* from table it gives error. Shouldn't it give all fields which begins with a?
I am little confused.
SELECT * FROM tableName literally means "select all columns from tableName".
Philip Graham is right about his answer where he asked to use a.*
Wildcards help you search for strings about which you are not sure. These are almost always used with the LIKE keyword and put in WHERE clauses or searched CASE statements.
There are two wildcard characters - % and _.
% is used to find any string of 0 or more length.
E.g.,
SELECT firstName
FROM persons
WHERE UPPER(firstName) LIKE 'J%'
This will return all the firstName from the persons table where firstname starts with letter J. This would return "Jason", "James", "Josh", "Jessica" and much more.
Note that UPPER function was used to eliminate case sensitivity.
Next, you can have an _ character that looks for the presence of one single character.
SELECT firstName
FROM persons
WHERE UPPER(firstName) LIKE 'J_M__'
This would return "James", "Jimmy", "Jamos", "Jxmx" and filter away any "Jason", "Jaguar", etc.
For more info click here
You can use a.* where a is the name of the table. For instance in
select a.* from a left join b on a.id = b.id
You would return only the fields from a but not from b
If want to use a wild card in SQL, You need to key on the column that you want to filter using LIKE.
SELECT *
FROM table
WHERE column_name LIKE 'a%';
This will give you everything that begins with 'a' on that column.
If you don't want all the columns, you must explicitly give the name of each column that you want in the query.
SELECT LastName, FirstName, Address
FROM table
So if you want all the fields that begin with 'a' you must name all the fields that begin with 'a' in the SELECT statement.
Hope this helps.

Getting records from two tables using Intersect

I want to get List of students from a particular section and with other search criteria. I am doing this:-
declare #sectionId int, #name varchar
select #sectionId=23
select #name='a'
select UserId, FirstName from StudentMaster
Where FirstName Like #name and UserId IN
(
select UserId from StudentMaster
Intersect
select StudentId from StudentInSections where SectionId=#sectionId
)
but it is not giving right answer. If I only write Userid condition it works properly but I have to get list with whole search criteria.
is there anybody to help me?
The problem is the LIKE operand. If #name is 'a' this will only return students whose name is 'a' or 'A'. If you want student names starting with "a" you must add a wildcard "%"
FirstName LIKE 'a%'
(Some SQL dialects like MS Access use the wildcard "*" instead of "%".)
Note on case sensitive/insensitive search. Depending on the SQL dialect and the collation used for the column the search will be case sensitive or not. If you do not want to make a case sensitive search (i.e. you want to find students whose name starts with "a" or "A"), you can do this:
UPPER(FirstName) LIKE 'A%'
Or on the SQL-Server
FirstName COLLATE UTF8_GENERAL_CI LIKE '%a'
Where CI means case insensitive.
select sm.UserId, sm.FirstName from StudentMaster sm
inner join StudentInSections ss on ss.StudentId = sm.UserId
Where sm.FirstName Like #name
and ss.SectionId = #sectionId
Something like that should work. You just need to learn how to use inner joins.

SQL case sensitive string comparison with like and "="

i have one table named category.
in this table i have two columns 1.) cat_id, 2.) cat_name
in this table i have added record with this value : cat_id = 1, cat_name = test
Now when i am going to run select query
SELECT * FROM category WHERE cat_name = 'Test'
this query returns me null result because in database record string is test and in query it's a Test mark the differance of capital string here. Instade of null result i want the result with 1st record.
also i want same result in vice versa small and capital string.
i am using sqlite as database.
Use upper function.
SELECT * FROM category WHERE upper(cat_name) = upper('Test')
SELECT * FROM category WHERE cat_name = 'Test' COLLATE NOCASE;
use LOWER:
SELECT * FROM category WHERE LOWER(cat_name) = 'test'
will get you all combinations of TeSt