How to get length of string using Oracle query - sql

In a table I have columns FIRST_NAME and LAST_NAME. I have to concatenate the two names and retrieve names containing more than 12 characters.
I tried the following query:
select *
from (select first_name, last_name
from customer as name
)
where length(NAME) = 12

Select *
from (
select first_name||last_name as name
from customer
)
where length(name)>12

You need only one SELECT
SELECT first_name || last_name AS name
FROM customer
WHERE LENGTH(first_name||last_name) > 12
Optionally TRIM name and last_name from spaces.

As an alternative, if you rewrite your question to check if the total length of first name and last name is more than 12, you probably get a different response from people with a more efficient code. It is not necessary to make Oracle actually concatenate the columns.
Programmers are too literal these days. :)

You are not concatenating the first name and last name.
Also no need to write an inline view, below is the query.
select FIRST_NAME || LAST_NAME as NAME
from CUSTOMER
where length(FIRST_NAME || LAST_NAME) > 12;

Related

Why do I get an error querying from column alias? [duplicate]

This question already has answers here:
Using an Alias column in the where clause in Postgresql
(6 answers)
access a column aliases in the where clause in postgresql
(2 answers)
How to use new created column in where column in sql?
(2 answers)
Closed 1 year ago.
I'm getting an error querying from the column alias and don't understand why. In the example below, if I run a query from the actual column, no problem. I concatenate first_name and last_name columns into a fullname column alias and then get the output.
SELECT first_name ||' '|| last_name AS fullname
FROM actor;
Output:
Now, if I create a column alias, I get the error. In this example, I'm concatenating first_name and last_name into a fullname column alias, and then query the names between value1 and value2.
SELECT first_name ||' '|| last_name AS fullname
FROM actor;
WHERE fullname BETWEEN 'Zero Cage' AND 'Fred Costner';
Output:
Thanks in advance for your taking the time to help!
In postgres document:
An output column's name can be used to refer to the column's value in ORDER BY and GROUP BY clauses, but not in the WHERE or HAVING clauses; there you must write out the expression instead.
That's according to the SQL standard and may not be very intuitive. The (historic) reason behind this is the sequence of events in a SELECT query. WHERE and HAVING are resolved before column aliases are considered, while GROUP BY and ORDER BY happen later, after column aliases have been applied.
Also note that conflicts between input and output names are resolved differently in ORDER BY and GROUP BY - another historic oddity (with a reason behind it, but potentially confusing nonetheless).
You can use one of the below manners:
Use full both column name
SELECT first_name || ' ' || last_name AS fullname
FROM actor
WHERE first_name || ' ' || last_name BETWEEN :conditio1 AND :conditio2
Use CTE
WITH data s (
SELECT first_name || ' ' || last_name AS fullname
FROM actor
)
SELECT *
FROM data
WHERE fullname BETWEEN :conditio1 AND :conditio2
Use subquery
SELECT *
FROM (
SELECT first_name || ' ' || last_name AS fullname
FROM actor
) tmp
WHERE tmp.fullname BETWEEN :conditio1 AND :conditio2

CREATE VIEW with concatenated fields

I have a table with Last Names, First Names, Hours and GPA's.
How do I create a view that displays a concatenated first name and last name, the StudentID and the GPA of the students who have passed at least 90 hours.
The concatenated names should be separated with one space.
The three column headings should be FullName, StudentID and GPA.
The rows should be sorted by last names, then first names.
Please help. I am lost as to how to approach this.
Use the operator || for concatenation (so you don't have to do nested CONCAT()).
Example:
create view v as
select (firstname || ' ' || lastname) "FullName", GPA, StudentId
from table
where Hours>90
order by lastname, firstname

SQL checking the last letter in string / AKA LIKE strange behaviour

I know it was already answered but it doesn't work for me.
So quick introduce:
I have table called swimmers. I'll insert new record to it which fulfils all columns:
INSERT INTO swimmers(id, first_name, last_name, age, gender)
VALUES(9,'Maria','Spolsky',34,'Female');
Now I want to find records in swimmers table which first_name DOES NOT end with letter a and where gender is Female
So I wrote SQL query:
SELECT first_name, last_name
FROM swimmers
WHERE first_name NOT LIKE '%a' AND gender = 'Female'
But it does return Maria which we just added. It basically returns every female
I'm using ORACLE iSQL plus.
EDIT:
I tried to use substr(first_name, -1) = 'a' but it turned out that -1 is empty, because I use varchar(20) and names are usually smaller than 20 characters
EDIT2:
I tried to find issue in data type. I used char(20) for first_name.
I changed it into varchar2(20):
ALTER TABLE swimmers
MODIFY first_name varchar2(20);
but didn't solve the issue
EDIT 3:
Changing NOT LIKE to WHERE first_name LIKE '%a' returns no rows. I believe issues lies inside data type and empty spaces from the end of the first_name to the end of reserved space for string (names has ~10 characters and I use `varchar2(20))
Appearantly oracles is adding spaces behind the name and that's why '%a' doesn't work, either change your column definition to a varchar (so oracle doesn't add the extra spaces) or trim the spaces out of your name like this:
SELECT first_name, last_name
FROM swimmers
WHERE NOT trim(first_name) LIKE '%a' AND gender = 'Female';

SQL prompt to enter a letter that the last name starts with

The whole question is:
Rewrite the query so that the user is prompted to
enter a letter that the last name starts with. For example, if the user enters “H" (capitalized) when prompted for a letter, then the output should show all employees whose last name starts with the letter “H.”
SELECT last_name
FROM employees
WHERE last_name like '%' = &Start_Letter
This wont work :(
This query should work:
SELECT last_name FROM employees WHERE last_name like '&Start_Letter%';
I have tested it in Oracle11g.
SELECT last_name FROM employees WHERE last_name like &Start_Letter + '%';
Depending on the requirements of your particular application, a more efficient solution might be to create a function based index:
CREATE INDEX emp_start_letter_idx
ON employees ( substr( last_name, 1, 1 ) );
Then your query would be:
SELECT last_name
FROM employees
WHERE substr(last_name, 1, 1) = '&Start_Letter';
Likewise, depending on your requirements, this may be a lot of unnecessary fuss!

How Do I SELECT when needing to use LIKE and change column Name

Here is the problem I am working on:
Select all the Oracle database employees whose last names end with “s” Change the
heading of the column to read Possible Candidates.
I tried
SELECT last_name AS possible_candidates FROM * WHERE last_name LIKE '%s';
That returned the error :
ORA-00903: invalid table name
Unless I am reading the question wrong how do I check the entire database for something like this?
To answer the question which is asking for employee names not just last names you would select all employees with a last name that ends in S.
SELECT *
FROM employees
WHERE last_name LIKE '%s'
If the table has more than a first_name and last_name column you can do
SELECT first_name, last_name
FROM employees
WHERE last_name LIKE '%s'
Now to combine your two parts (Select employees and rename column)
Run one of the two queries above but add an AS statement as show in the example below.
SELECT *
AS Possible_Candidates
FROM employees
WHERE last_name LIKE '%s'
Based on feedback: this appears to be your answer...
Is there a table called employees? if that I would interpret the question as from the employees table. Otherwise, you could select table_name from All_TAB_COLS where column_name like = 'LAST_NAME' and then build a union from the resulting tables all of which have a field called 'LAST_NAME'