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

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

Related

Append & populate first name, last name columns using full name column in same table?

I'm using SQL Server and I have a column called "Full Name" that I pulled from a separate table. I have a column for First Name & Last name (among many other columns).
Here is the code that I found and used and it worked:
SELECT Full_Name,
LEFT(Full_Name,CHARINDEX(' ',Full_Name )-1) AS First_Name,
RIGHT(Full_Name, LEN(Full_Name)- CHARINDEX(' ', Full_Name)) AS Last_Name
FROM table_name
That extracted the name, but upon viewing the entire table using this command:
SELECT * FROM table_name
I don't see it? Is there a method that I can use to insert the data into the First_name and Last_name columns without adding any additional Rows
Thank you !
Perhaps creating a view wil be sufficient:
CREATE VIEW table_name_v
SELECT Full_Name,
LEFT(Full_Name,CHARINDEX(' ',Full_Name )-1) AS First_Name,
RIGHT(Full_Name, LEN(Full_Name)- CHARINDEX(' ', Full_Name)) AS Last_Name
FROM table_name
Then instead of using the table name in subsequent queries you use the view instead
SELECT * FROM table_name_v
The problem you face otherwise is that if you add first and last name columns to this table that you now end-up with 3 columns to maintain, or that you may need to replace the data entry screen(s) that use full_name to start using first and last name columns instead.
If you really do want to proceed by adding the columns, you could try using "computed columns" which would avoid the necessity to change data entry screens etc.
CREATE TABLE mytable(
full_name VARCHAR(15) NOT NULL
);
INSERT INTO mytable(full_name) VALUES ('fred flintstone');
select * from mytable
full_name
fred flintstone
alter table mytable
add
first_name as LEFT(Full_Name,CHARINDEX(' ',Full_Name )-1)
, Last_Name as RIGHT(Full_Name, LEN(Full_Name)- CHARINDEX(' ', Full_Name))
select * from mytable
full_name
first_name
Last_Name
fred flintstone
fred
flintstone
db<>fiddle for computed columns here
If you realy do want to store these 2 separate columns (rather than computing them) then you need to add the columns to your table and run an update statement to populate them as follows:
alter table mytable
add
first_name varchar(100)
, last_name varchar(100)
update mytable
set
first_name = LEFT(Full_Name,CHARINDEX(' ',Full_Name )-1)
, Last_Name = RIGHT(Full_Name, LEN(Full_Name)- CHARINDEX(' ', Full_Name))
db<>fiddle for adding columns and updating here

How to get length of string using Oracle query

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;

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

Filling a new SQL column using two other columns

I am trying to combine to rows in a table into one new row in a table. Specifically columns First_Name and Last_name into First_Last. I am having trouble getting my query to run due to the fact I am trying to do this for all entries in the table and not just 1 row. Any suggestions?
Current Code:
update Name
set First_Last = (select First_Name + ' ' + Last_Name from Name)
Thanks,
Justin
Assuming you intend updating all rows on the same table using the First_Name and Last_Name values for each row, it is as simple as (in Sql Server):
update MyTable
set First_Last = First_Name + ' ' + Last_Name;
In most other RBDMS, you use the pipe || operator or the CONCAT function to combine text.
Note that most RDBMS also have the concept of a computed (sometimes called generated) column, which prevents the redundancy and synchronisation issues with storing a derived field:
CREATE TABLE MyTable...
(
First_Last AS first_name + ' ' + last_name
);
You did not specify your RDBMS but if you are using MySQL you can use the CONCAT() function
update Name
set First_Last = concat(First_Name, ' ', Last_Name)

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'