SQL Server: how to update rows with additional information - sql

I have a situation in which a table has information like:
First Name | Last Name | Email
-----------+-----------+-----------------
John Doe jd#email.com
Jane Dont jnd#email.com
And I have a user who wants their email added on to both row's emails to looks like:
First Name | Last Name | Email
-----------+-----------+-----------------------------
John Doe jd#email.com;a#email.com
Jane Dont jnd#email.com;a#email.com
Thank you in advance for any help.

To add a#email.com to all rows in your table:
UPDATE Table
SET Email = Email + ';a#email.com'
To update certain rows:
UPDATE t
SET t.Email = t.Email + ';a#email.com'
FROM Table t
WHERE t.FirstName = 'John'
Note: the above query will update all records with the first name of John
Edit ******** Per #destination-data comment:
If you are also trying to add the new email to columns that have NULL value
SET Email = ISNULL(Email, '') + ';a#email.com'
"...Because null plus anything is null." This technique will change the value from NULL to an empty string plus the new value.

Related

Using watchlist value without Search Key

I'm new to KQL and Sentinel.
I'm trying to add validation to a query of alerts using only a column of a Watchlist.
My Watchlist are:
Name
LastName
Date
Jhon
Doe
01/01/2000
I would like to validate that the date in column 3 is greater than the current date. But my Search Key is defined using the name (column 1). Is there a possibility of obtaining a column without using the SearchKey and using it in a validation?
Just for remember
Name is my Search Key
For example:
let watchlist = Getwhatchlist(test)
let today = now();
Table_A
| where watchlist.Date > today
| join kind=leftanti watchlist on Name

How can i update email column with appending some text in a particular place in Postgresql

I want to update my email column like this
|id (int) | email(varchar) | is_subscribed(boolean)
---------------------------------------------------------------------
| 1 | abc#gmail.com | true
| 2 | def#gmail.com | false
update email where id=1
after the update , an updated email will be like abc-cancel#gmail.con
cancel is string which i want to append
This what you are describing:
update t
set email = 'abc-cancel#gmail.com'
where id = 1;
Actually, I suspect that you want to insert the -cancel into the email. I strongly discourage this. You should be storing such information in a different column rather than destroying information in your database. But if you really must, you can use replace():
update t
set email = replace(email, '#', '-cancel#')
where id = 1;
After you do this, you will not be able to match emails in different tables, different databases, or provided from third-parties. Seems like a maintenance nightmare.

UPDATING a column to take first letter from another column

I would like to UPDATE my table to replace / insert in the INITIALS column, the first letter in the first name column
Table name: Mano
Title Firstname Lastname Telephone Initial Gender More columns
1 Mr Adam Smith 001
2 Mrs Angela Evans 002 AE
3 Mr Bill Towny 003
4 Miss Dame Beaut 004
I am interested in transforming it as per below
Title Firstname Lastname Telephone Inital Gender More columns
1 Mr Adam Smith 001 A
2 Mrs Angela Evans 002 A
3 Mr Bill Towny 003 B
4 Miss Dame Beaut 004 D
Many thanks
This seems like a simple update:
update t
set initials = left(firstname, 1);
I should point out that you don't even need a column. You can declare this as a computed column:
alter t add initials as (left(firstname, 1));
This would provide a column called initials (assuming that name is not already used) that always has the first letter of the firstname column -- without an update.
You can use substring function,
SELECT SUBSTR(Firstname, 1, 1) from mango;
Hope using this select statement you can update the table.
If you need the update query, let me know will give you
Left is the best and would more better if we use this with TRIM to prevent the extra spaces if any:
UPDATE TABLE table1 SET Initial = LEFT(LTRIM(Firstname), 1)
Add a computed column, will never be inconsistent:
alter table Mano add Initial as SUBSTR(Firstname, 1, 1)

SQL: Select the same column twice in one query?

I'm currently working on a query where I need to pull a list of items, as well as the date they were entered into the system and the name and user ID of the person who entered them.
My Items table contains the columns CreateDate, CreatedBy (this is the person's user ID), LastChanged (this is a date), and LastChangedBy (also a user ID, being as the person who entered the item isn't always the same person who created it).
I have the Items table joined with a User table so I can pull the first and last name of the users who match the CreatedBy and LastChangedBy user IDs. The way I have the query written now, I've concatenated the two fields so they end up displayed as "LastName, FirstName" in a single column. I also want to only pull results for a certain user, and this user just needs to be the last person who changed the item - it doesn't matter whether or not they created it.
My query looks something like this:
SELECT
i.ItemName, i.CreateDate,
(u.LastName + ', ' + u.FirstName) as [Created By Name],
i.CreatedBy, i.LastChanged,
(u.LastName + ', ' + u.FirstName) as [Last Changed By Name],
i.LastChangedBy
FROM
Items i
JOIN
Users u ON i.CreatedBy = u.UserID
WHERE
i.LastChangedBy = 0001;
My issue though is that since I have the exact same piece of code to pull the user's name both times, it's pulling the same name regardless of whether the user IDs are different in the CreatedBy and LastChangedBy columns. So I end up with results that look like this:
ItemName CreateDate Created By Name CreatedBy LastChanged Last Changed By Name LastChangedBy
Item A 12/01/2015 Smith, John 0001 12/03/2015 Smith, John 0001
Item B 12/02/2015 Smith, John 0001 12/04/2015 Smith, John 0001
Item C 12/02/2015 Doe, Jane 1002 12/05/2015 Doe, Jane 0001
So even though John Smith was the last person to change Item C, it's still displaying Jane Doe's name there because (I assume) the strings of code to display the user's name is the same in both instances and it's just pulling the same name both times.
Is there a way to pull the first and last names from the User table twice in the same query but make sure they correspond with the correct user IDs?
I think you are missing a join in your query:
SELECT i.ItemName, i.CreateDate,
(uc.LastName + ', ' + uc.FirstName) as [Created By Name],
i.CreatedBy, i.LastChanged,
(ucb.LastName + ', ' + ucb.FirstName) as [Last Changed By Name],
i.LastChangedBy
FROM Items i LEFT JOIN
Users uc
ON i.CreatedBy = uc.UserID LEFT JOIN
Users ucb
ON i.LastChangedBy = ucb.UserId
WHERE i.LastChangedBy = 0001;
You need to join twice to get both users.

Tough SQL Update

2 databases QF AND TK
QF has the following:
Imagine you have a table called FunctionalGroup with this data:
FunctionalGroupID | FunctionalGroup
1 Engineering
2 Purchasing
And a table that was a set of login's with a functionalgroupID to reference the group the person is in...
LoginID | FunctionalGroupID | Login
1 1 Jon
2 1 Joe
3 2 Jane
So Jon and Joe are engineering while Jane is purchasing..simple enough
Now there is another database TK.
TK has the following table Login with something to this effect:
Login | FunctionalGroupID
Jon Purchasing
Joe Purchasing
Jane Purchasing
Notice how Jon and Joe in this database are now part of the purchasing group...But notice how this field is the text field and no ID. So what I want to do is use this table as the master data source and update the QF table such that the logins table from the QF now looks like this:
LoginID | FunctionalGroupID | Login
1 2 Jon
2 2 Joe
3 2 Jane
That is update this table to make Jon and Joe part of the purchasing group by setting their functionalgroupid = 2. Because 2 means purchasing.
I tried this:
UPDATE
Login
SET Login.FunctionalGroupID = FunctionalGroup.FunctionalGroupID
FROM Login INNER JOIN
TKKCommonData.dbo.Login lz
ON lz.Login = Login.Login
AND lz.FunctionalGroupID = FunctionalGroup.FunctionalGroup
But I get an error:
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "FunctionalGroup.FunctionalGroup" could not be bound.
This seems so easy but Im just not sure how to write the update statement. Im just looking to join the tables by the Login (which is the users name) and then by the Functionalgroup names.
I even tried this EDIT per Jay's answer with same error message
UPDATE
QuikFix.dbo.Login
SET QuikFix.dbo.Login.FunctionalGroupID = QuikFix.dbo.FunctionalGroup.FunctionalGroupID
FROM QuikFix.dbo.Login INNER JOIN
TKKCommonData.dbo.Login
ON TKKCommonData.dbo.Login.Login = QuikFix.dbo.Login.Login
AND TKKCommonData.dbo.Login.FunctionalGroupID = QuikFix.dbo.FunctionalGroup.FunctionalGroup
WHERE TKKCommonData.dbo.Login.LoginID= 101
You need an additional INNER JOIN:
UPDATE Login
SET
Login.FunctionalGroupID = FunctionalGroup.FunctionalGroupID
FROM Login
INNER JOIN TKKCommonData.dbo.Login lz
ON lz.Login = Login.Login
INNER JOIN FunctionalGroup
ON lz.FunctionalGroupID = FunctionalGroup.FunctionalGroup
Specify the database name for all of the tables in your query instead of just TKKCommonData.dbo.Login, seems like it can't find the FunctionalGroup table in the database the query is running against.