Sql (Not in )operator query getting ignored - sql

I am writing an SQL query to select each row except those which have mentioned city values.
But somehow it's not working and selects rows with these cities.
Select * from Emp
Where City not in ('Suart', 'Vapi');
Upcoming output
Name City
Kris Surat
Joy vapi
Riva Goa
Jeni Mumbai
Maya Sayan
Expected output
Name City
Riva Goa
Jeni Mumbai
Maya Sayan

Maybe your data must be case sensitive or spaces added.
Try this if it works
Select * from Emp
Where lower(ltrim(rtrim(City))) not in ('suart', 'vapi');
Or
Select * from Emp
Where lower(ltrim(rtrim(City))) not like '%suart%' OR
lower(ltrim(rtrim(City))) not like '%vapi%'

Related

SQL Result to multiple array

MY SQL returns the following array...
id
staff
province
1
Ben
Ontario
2
Ben
Quebec
3
John
Manitoba
4
John
Saskatchewan
6
Kitty
Alberta
7
Kitty
Nova Scotia
I would like to have the record displayed like this...
staff
province
Ben
Ontario, Quebec
John
Quebec, Manitoba, Saskatchewan
Kitty
Alberta, Nova Scotia
what approach should I use to approach this?
Would be better to post the tables as well for clearer context.
You can use Aggregate functions and Grouping to help doing this. A GROUP BY to group the rows by staff column, then use GROUP_CONCAT() to concatenate province values in one string.
A reference of how you want it to be, unsure what table you are using or if there are any other factors but you can adapt as needed.
SELECT staff, GROUP_CONCAT(province SEPARATOR ', ') as province
FROM table_name
GROUP BY staff;

DB2: I want to see € or $ in the output of select

This simple query satisfy me
db2 => SELECT city,SUM(sales) as sum from offices group by city;
CITY SUM
---------------------------------------------------------------------------------------------------- ---------------------------------
Rome 14000,
Paris 19000,
But..how to add the $ or € symbol? To get an output like this?
CITY SUM
---------------------------------------------------------------------------------------------------- ---------------------------------
Rome 14000$
Paris 19000$
Normally, this kind of formatting is left to the client to display.
If you really need to have the DB do it, then VARCHAR_FORMAT is likely a better choice.
select city
, varchar_format(sum(sales),'$999G999G990D99' as sales
from offices group by city;
The G and D characters represent the grouping and decimal character for your locale
Solution found using the ||
select city,sum(sales) || '$' as sales from offices group by city;

ORDER BY and TOP statements in SQL works different on listing down the records and why?

I am using table called CITY from my database with below city names.
CITY Names : Delhi, Mumbai, Patna, Vijayawada, Panaji, Tiruvananthapuram, Chennai and Kolkata.
ORDER BY:
Select NAME from CITY ORDER BY LEN(NAME)
Output : Delhi, Patna, Panaji, Mumbai, Kolkata, Chennai, Vijayawada, Tiruvananthapuram
TOP 3 and ORDER BY
Select TOP 3 NAME from CITY ORDER BY LEN(NAME)
Output : Delhi, Patna, Mumbai
My question here is, why not Delhi, Patna and Panaji. Why top 3 is picking the fourth item instead of third one?
This is because Panaji and Mumbai are both 6 characters long and both of them qualify for the third position inside the TOP 3 clause. SQL Server is free to return Panaji or Mumbai without violating the ORDER BY criteria and there is absolutely no guarantee which city it will return at any given time.
To handle ties you can explicitly specify additional criteria in ORDER BY clause e.g.:
ORDER BY LEN(name), name -- order by length
-- if there is a tie then by name
This was one of the reasons WITH TIES keyword was introduced along with TOP clause.
Without using the keyword, you would obtain random results for the results you hit the match with. As you see here, Mumbai and panaji had same length so either of them could have been returned. While using the keyword, you would have seen 4 results return (both of them included).

VBA Access SQL - field within LIKE operator

Can I use a table column within a Like operator? I've created an example,
TableA
Names Location
Albert Smith Senior Aberdeen
John Lee London
Michael Rogers Junior Newcastle
Mary Roberts Edinburgh
TableB
Names
Albert Smith
John Lee
Michael Rogers
I want to do a query such as:
SELECT TableA.Location
into NewTable
FROM TableA
WHERE TableA.Names Like '*[TableB.Names]*';
In this case, there would be no match for Mary Roberts, Edinburgh but the first three locations would be returned.
Is it possible to put a column into a like statement?
If not does anyone have any ideas how I could do this?
Hope you can help
PS I can't use an actual asterisk since this is removed and the text italicised, also I have read about using % instead but this has not worked for me.
You can join the two tables and use LIKE within the JOIN clause:
SELECT TableA.Location
into NewTable
FROM TableA
INNER JOIN TableB ON TableA.Names LIKE TableB.Names & '*';
Honestly, I had no idea that you can do this in Access before I tried it just now :-)

SQL - compare part of word in WHERE clausule

I have problem with sql query.
For example, I have table like this:
ID Name1 Name2 Country
1 Greg Torr Poland
2 John Smith England
3 Tom Jerry USA
I want get all record, which have for example, "la" in Country. In this case:
PoLAnd
EngLAnd
How I can put this in Where clausule?
Greets
Use the LIKE keyword:
SELECT * FROM table
WHERE Country LIKE '%la%'
I think you can use the Like Clause here. The examples are available on
http://www.sql-tutorial.net/SQL-LIKE.asp
http://www.w3schools.com/sql/sql_like.asp
etc.
WHERE CHARINDEX('LA', Country) > 0
alternatively
WHERE Country LIKE '%la%'
If your RDBMS is case-sensitive, convert Country to upper case using the appropriate string function, and compare against the upper case LA with a LIKE:
SELECT *
FROM tbl
WHERE UPPER(Country) LIKE '%LA%'
Enclose your keyword before and after with Percent Symbol
SELECT *
FROM tableName
WHERE Country LIKE '%LA%'