New email address convention [duplicate] - sql

This question already has answers here:
SQL UPDATE SET one column to be equal to a value in a related table referenced by a different column?
(12 answers)
Closed 9 years ago.
I have a database with email addresses in it.
My company is changing our email address convention from:
first_initiallast_name#mycompany.com
to
first_name.last_name#contoso.com
I'd like to write a SQL statement to update all the email addresses in one shot in this database. First and last name are columns in the same table (we'll call it MY_TABLE for simplicity's sake).
How could I do this in an Oracle SQL statement?

It seems like you'd just want
UPDATE my_table
SET email_address = first_name || '.' || last_name || '#contoso.com'
That will update every row in the table and assumes that you have no NULL first or last name values.

You juste want to update the email field with two others fields:
UPDATE my_table SET email= first_name || '.' || last_name || '#contoso.com'
WHERE first_name != NULL AND last_name != NULL
Be aware that the transformation might be incorrect if first_name or last_name is empty...
EDIT: In reality what you want is similar to this question: SQL UPDATE SET one column to be equal to a value in a related table referenced by a different column?

Related

SQLITE: Appending string to primary key [duplicate]

This question already has answers here:
String concatenation does not work in SQLite
(4 answers)
Closed 9 months ago.
I'm using this to try and name the values in NAME to ValueOLD
UPDATE PLAYERS SET NAME = NAME + 'OLD';
However I am receiving
Execution finished with errors.
Result: UNIQUE constraint failed: PLAYERS.NAME
At line 1:
UPDATE PLAYERS SET NAME = NAME + 'OLD';
I've seen some questions asked on inserts that look like they work, but haven't had any success with UPDATE or SET
In SQLite, || can be used for concatenation.
So Try:
UPDATE
PLAYERS
SET
NAME = NAME || 'OLD';
SQL Language Expressions
The || operator is "concatenate" - it joins together the two strings
of its operands.

update a column using values from a different column of the same table

Given the DB table:
CREATE TABLE stuff (
id text not null,
other text
);
That has lots of id values but has all other set to NULL, is there an elegant way to update the table so that all other rows get updated to OTHER-{id} (where {id} is the value of the id column)?
(It must work in Postgresql)
Only a simple update statement is needed with some string concatenation (||):
update stuff
set other = 'OTHER-' || id
You'll want to use the following:
UPDATE stuff
SET other = 'OTHER-' || id;
UPDATE is the keyword used to identify which table you'd like to update.
SET is the keyword used to identify which column you'd like to update, and this is where you choose to assign the column to:
'OTHER-' || id
'OTHER-' being a string
|| a shorthand way to concatenate
id the value you want.
Another way of writing this would be
other = concat('OTHER-',id);
I along with many others will find the || method to be much cleaner, but it's worth knowing about the dedicated function as well.

How do I return only duplicate values depending on column name?

Im using Access for sql. I know access is terrible but its the only available resource for database in my work.
I want to return duplicate values for, lets say, the middle name. I want to see all the rows with the same middle names on a certain month.
Here comes the tricky part, the duplicate values i want to look for is not in the table. I wanna see the duplicate values when you concatenate middle name AND last name. )and for some reason, Access dont like aliases)
You should group by on concatenated value:
select middle_name || last_name
from names_table
group by middle_name || last_name
having count(*) > 1

How to get coumn names without getting truncated in SQLPLUS

How to do I get column names without getting truncated in SQLPLUS in Unix for select statement. This might look like duplicate question, but I have been searching for hours but couldn't find a convenient solution.
So far what I have found is
COLUMN COLUMN_NAME FORMAT SIZE;
Or
SELECT COLUMN1|| ',' || COLUMN2 || ',' || COLUMN3 FROM TABLE;
Both involves hardcoding,Is there any simpler solution without hardcoding.
Sorry for making it hard
Query: select * from Employee;
It has column names as Name,Salary,Age
What I get is:
Name Sala Ag
Steve 1000 30
John 2000 25
What I want is:
Name Salary Age
Steve 1000 30
John 2000 25
Setting size (width) of a column in SQL*Plus output.
SQL> column sex format a5
Seeing the current settings in effect.
SQL> column
Getting further help on usage.
SQL> help column
UPDATE
Setting format for all columns (in an awkward way). Assuming my users table defined as follows.
create table users(
id number
, username varchar2(20)
, credentials varchar2(90)
, lastname varchar2(20) not null
, firstname varchar2(20)
, emailaddress varchar2(42)
, emailisvalid number(1)
, sex char(1)
, created date default sysdate
);
We could issue this command putting the output into the file login.sql which is automatically executed every time you start SQL*Plus.
SQL> spool login.sql
SQL> select 'column ' || column_name || ' format a' || length(column_name) || ';'
from user_tab_cols where table_name = 'USERS';
column ID format a2;
column USERNAME format a8;
column PASSWORD format a8;
column LASTNAME format a8;
column FIRSTNAME format a9;
column EMAILADDRESS format a12;
column EMAILISVALID format a12;
column SEX format a3;
column CREATED format a7;
This problem:
Name Sala Ag
Steve 1000 30
John 2000 25
Can be solved like this:
SELECT Name,
CAST(Salary) AS VARCHAR(6) AS Salary,
CAST(Age) AS VARCHAR(3) AS Age
FROM Employee;
Note, once again... this is not what the query is returning, but how the client is displaying the results. Use a different client it would work differently. In this case the client is formatting the columns based on the column data type. When it sees a varchar (like name) it goes to the max data size. So we give it a bigger string data it will look ok.
This is NOT part of what is happening on the Server -- this goes to how the client displays. So if the query is going to be used by a different client (eg web page or application call) this won't matter when it is actually used by those clients.

Add a Column that Represents a Concatenation of Two Other Varchar Columns

I have an employees table and I want to add a third column valued as the concatenation of the first and last name called "FullName". How can I accomplish that without losing any data from either of the first two columns?
Quick preface: this answer was based on the originally incorrect tag that this question was relating to SQL Server. I'm no longer aware of its validity on Oracle SQL Developer.
ALTER TABLE Employees ADD FullName AS (FirstName + ' ' + LastName)
Although in practice I'd advise that you do that operation in your SELECT. That's somewhat personal preference, but I tend to think doing things in your end queries is a bit cleaner, more readable, and easier to maintain than storing extra, calculated columns.
Edit:
This was eventually found as the answer, and listed by the OP as a comment on this post. The following is appropriate syntax for Oracle Sql Database.
ALTER TABLE emps MODIFY (FULL_NAME VARCHAR2(50) GENERATED ALWAYS AS (first_name || ' ' || last_name) VIRTUAL);
If you need fullname column all time when you select from database then you can create computed column at the time of creation of your table employee.
for example:
CREATE TABLE Employee
(
FirstName VARCHAR(20),
LastName VARCHAR(20),
FullName AS CONCAT(FirstName,' ',LastName)
)
INSERT INTO Employee VALUES ('Rocky','Jeo')
SELECT * FROM Employee
Output:
FirstName LastName FullName
Rocky Jeo Rocky Jeo
It depends on your purpose, whether you really need to add a new column to your database, or you just need to query out the "full name" on an as-needed basis.
To view it on the fly, just run the query
SELECT firstname + ' ' + lastname AS FullName FROM employees
Beyond that, you also can create a simple Stored Procedure to store it.
(For single result use equal to in the where condition)
select *
from TABLE_name
where (Column1+Column2) in (11361+280,11365+250)
In addition to #Jacky 's answer, if you are trying to add this to a query and not the table, there is also the CONCAT() function that you can use in the select statement
SELECT CONCAT(FirstName, ' ', LastName) as FullName
FROM table_name