inserting multiples values in one column - sql

I have a question about SQL. I have created a table in SQL with only one column containing the name of two people (say John and Matt). Later I added a new column into the table with ALTER TABLE. This column will contain the surname of these people.
My question is, in case mmy table contained several people already is there a command to enter the surnames for all the people at once rather than writing one command for each person as in:
INSERT INTO table (Surname) VALUE (John's surname) and
INSERT INTO table (Surname) VALUE (Matt's surname) ?
Thanks in advance
P.D.
I tried something like:
UPDATE foo set Surname=("Parker","Walker") where Name =("John","Matt") but does not work

You want an update. Something like this:
update t
set surname = 'John'' surname'
where firstname = 'John';
You can do this separately for each name. Or use a case expression for multiple ones:
UPDATE foo
SET Surname = (CASE WHEN Name = 'John' THEN 'Parker'
WHEN Name = 'Matt' THEN 'Walker'
END)
WHERE Name IN ('John', 'Matt');

Related

Insert multiple columns into a mapped 1-1 single column

I have the following setup in SQL Server 2016. I want to insert into table B name, surname, address into there respective columns, but from the 3 columns in View A for employment, I want to insert a single row for each person based on their Employment into Employment Status column in Table B, What is the best way to do this?
View A
Name (String)
Surname (Sring)
Address (String)
Employed (boolean)
Non-Employed (boolean)
Retired (boolean)
Table B
Name (String)
Surname (Sring)
Address (String)
Employed Status (String)
Although you have three flags that seem exclusive, I see no reason to think that they actually are (unless you have some sort of validation).
So, I would concatenate the statuses together:
insert into b (name, surname, address, employed_status)
select name, surname, address,
stuff(concat(case when employeed = 1 then ', employed' else '' end,
case when non_employeed = 1 then ',non_employed' else '' end,
case when retired = 1 then ',retired' else '' end
), 1, 1, ''
)
from a;
Note: SQL Server does not have a boolean type, so I assume you are using 0/1 encoding for the columns.
I would take a step back and create another table called EmploymentState, into which I would add three rows "1 - Employed", "2 - Unemployed", "3 - Retired". Then add a foreign key to that table from Table A and Table B.
Unless of course, a single person can be in more than one state...in which case you need an intersection table. See https://www.bing.com/search?q=sql+intersection+table&qs=n&form=QBRE&sp=-1&ghc=1&pq=sql+intersection+tab&sc=0-20&sk=&cvid=153E23C3D25F461E89F71E106FBF7D66

Replace string while updating sql

I have table called Empdetails. Here one of column name is loginid. I want to replace loginid if it contain #s.com, in few case it has.
Loginid
abc#s.com
sdf
ghj
adfgh#j.com
fghjku#s.com
pinky#s.com
update Empdetails
set loginid = REPLACE(loginid, '#s.com', '')
where id in (1,6,8,9)
If I mistakenly mention id whose loginid does not contain any '#s.com', will throw an error. Is above query is fine
To answer your question, your query will not end in Error.
The below will only update records which end with '#s.com'
update Empdetails
set loginid = REPLACE(loginid, '#s.com', '')
where loginid like '%#s.com'
You don't need to specify ID, you can just use your logic to filter records which you want to update

selecting with a column being one of two possible values

I have a table called "people" with a column named "name". I would like to select all rows where the name is "bob" or "john". I have tried the following and many variants of it, none of which work. How can I do this correctly?
select * from people where name is bob or john;
Thanks
To compare a column with a value you need to use = not IS
select *
from people
where name = 'bob'
or name = 'john';
Alternatively you can use the IN operator.
select *
from people
where name IN ('bob','john');
Note that string comparison is case-sensitive in SQL. So the above will not return rows where the name is Bob or John

SQL select specific letter from concatenated string

This may have been answered similarly somewhere but I am still kind of confused. I need to create a view named A7T7 that will display the concatenated first name and last name of the students who have at least three letter Os or three letter Ts in their concatenated name (e.g., John Olson, Richard Hooton, Tina Trout). The column heading should be Student and the rows should be sorted by the length of the concatenated names from long to short.
Not really sure how to make my WHERE statement for this restriction.
You can use a LIKE query - for example (using a table variable for SQL Server):
CREATE TABLE #students (firstname varchar(20), lastname varchar(20));
INSERT ALL
INTO #students VALUES ('John','Olson')
INTO #students VALUES ('Richard','Hooton')
INTO #students VALUES ('Tina','Trout')
SELECT 1 FROM dual;
SELECT *
FROM #students s
WHERE CONCAT(s.firstname, s.lastname) LIKE '%o%o%o%';
DROP TABLE #students;
Which roughly translates to "select all the rows from students where the concatenation of firstname and lastname contains o three times".

How Could I Update a table with conditions from other table

I Have the Table "Person" and the table "Fisica" which is the extention of "Person". Both tables are related by the field name Id, and I want to update the tables based on conditions that include both tables.
For example:
Tables:
Persona(Id, Name, Money)
Fisica(Id, LastName, Year)
With data:
Persona(1, X, 5)
Persona(2, A, 10)
Fisica(1, Y, 1990)
Fisica(2, B, 2000)
I want to set Persona.Name=some_Value, Fisica.LastName=other_Value and Fisica.Year=number when Persona.Name='X', so it results
Persona(1, some_Value, 5)
Persona(2, A, 10)
Fisica(1, other_Value, number)
Fisica(2, B, 2000)
I am working in Oracle
You can't update two tables with one statement. So you need two UPDATE statements.
So you have to update one, then the other. The question is, which one first?
It doesn't really matter, but if you update Persona first and change the name, then you have to use the new name when updating Fisica. Like this:
update Persona
set name = 'some_Value'
where name = 'current_Name'
;
update Fisica
set lastname = 'other_Value',
year = number
where id = (
select id
from Persona
where name = 'some_Value'
)
;
If you update Fisica first, you use the old value of name both times, like this:
update Fisica
set lastname = 'other_Value',
year = number
where id = (
select id
from Persona
where name = 'current_Value'
)
;
update Persona
set name = 'some_Value'
where name = 'current_Value'
;
note number has to be replaced with an actual numeric value.
Good coding practice would be to put both statements in the same transaction and commit only if they are both successful.
Though Oracle don't provide such functionality as compare to mysql where we can write query like this:
Update table1 t1, table2 t2 set t2.field2="ABC" where t1.id=t2.id
But you can update multiple tables using single query in Oracle for Views.
So create the view for the above two tables Persona, Fisica and write query as follows:
CREATE OR REPLACE FORCE VIEW "Persona_Fisica" ("PersonaID", "Name", "Money", "FisicaID", "Lastname", "Year") AS
select P.Id as PersonaID,
Name as Name,
Money as Money,
F.Id as FisicaID,
LastName as Lastname,
year as Year
from Persona P,
Fisica F
where P.ID=F.ID
/
Update Persona_Fisica set Name=some_Value, LastName=other_Value,Year=number were Name='X',