UPDATING a column to take first letter from another column - sql

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)

Related

BigQuery - concatenate array of strings for each row with `null`s

This is a clarification/follow-up on the earlier question where I didn't specify the requirement for null values.
Given this input:
Row id app_date inventor.name inventor.country
1 id_1 01-15-2022 Steve US
Ashley US
2 id_2 03-16-2011 Pete US
<null> US
Mary FR
I need to extract name from inventor struct and concatenate them for each id, like so:
Row id app_date inventors
1 id_1 01-15-2022 Steve, Ashley
2 id_2 03-16-2011 Pete, ^, Mary
Note custom filling for null value - which, to me, seems like it means I need to use ARRAY_TO_STRING specifically that supports this.
The closest example I found doesn't work with nulls. How can one do this?
Use below
SELECT * EXCEPT(inventor),
(SELECT STRING_AGG(IFNULL(name, '^'), ', ') FROM t.inventor) inventors
FROM sample t
with output

Grouping values and changing values which do not allow the rest of the row to group

Not sure how to describe this, but I want to group a row of values, where one field has two or more different values and set the value of that (but concatenating or changing the values) to give just one single row.
For example:
I have a simple table (all fields are Strings) of people next to their departments. But some people belong to more than one department.
select department_ind, name
from jobs
;
department_ind name
1 Michael
2 Michael
2 Sarah
3 Dave
2 Sally
4 Sally
I want to group by name, and concatenate the department_ind. So the results show look like:
department_ind name
1,2 Michael
2 Sarah
3 Dave
2,4 Sally
Thanks
Use string_agg()
select string_agg(department_ind::text, ',') as departments,
name
from jobs
group by name;

Crystal Reports SQL - comparing data in multiple fields for grouping purposes

I have a database table which contains the following fields (amoung others)
Person 1
Person 2
Person 3
I need to group by Person. Unfortunately the same person can appear in any one of the above three fields. I can't simply concatenate the 3 fields as there may be a name in each field ie 3 names in a particular entry. For example
Record 1 Joe Bloggs (Person 1), Person 2 null, Person 3 null
Record 2 Jane Black (Person 1), Joe Bloggs (Person 2), Person 3 null
Record 3 Jane Black (Person 1), James Blue (person 2), Joe Bloggs (person 3)
Yes I know the database table is set up incorrectly - ideally there should be 1 field for Person's name and multiple entries but this is not the case and this is what I have to work with.
How on earth can I group by name when the answer is in one of several fields ?
Please note I am not looking to pull out a particular name (that would be easy as I would search for the name in either of the 3 fields). I need to group by name to show a report containing everyone. The report would look like:
Group 1: Joe Bloggs
Record 1 fields
Record 2 Fields
Record 3 fields
Group 2: Jane Black
Record 2 Fields
Record 3 Fields
Group 3: James Blue
Record 3 fields
Any help would be much appreciated thank you !

Search SQL table for value, lookup another value in that record and replace with value

I want to run a script on a SQL table that will search the table for the users supervisor and then replace the supervisor value with the fullname value. How can I do this? I am using MSSQL and have one table containing this data.
Before:
fullname,username,supervisor
Timothy Dalton,tdalton,rmoore
Pierce Brosnan,pbrosnan,rmoore
Sean Connery,sconnery,rmoore
Roger Moore,rmoore,dcraig
Daniel Craig,dcraig,
After script:
fullname,username,supervisor
Timothy Dalton,tdalton,Roger Moore
Pierce Brosnan,pbrosnan,Roger Moore
Sean Connery,sconnery,Roger Moore
Roger Moore,rmoore,Daniel Craig
Daniel Craig,dcraig,
Thanks
Try something like this
Update t1
set t1.supervisor = t2.Fullname
from YourTable t1
join YourTable t2 on t1.supervisor = t2.username
This code hasn't been tested ... so make sure to backup table before using it
Try with the below query.
Please note that below query will fail if there are multiple supervisors with same last name and first name starts with same character.
(for example if the supervisor is rmoore and there are Roger Moore and Royal Moore in full name,then the below query will update the supervisor with any of these names)
UPDATE y
SET y.supervisor=y1.fullname
FROM YourTable y
JOIN YourTable y1
ON y1.fullname like LEFT(y.supervisor,1)+'%'
AND LTRIM(RTRIM(SUBSTRING(y1.fullname,CHARINDEX(' ',y1.fullname),LEN(y1.fullname))))=LTRIM(RTRIM(SUBSTRING(y.supervisor,2,LEN(y.supervisor))))

sql combine two columns that might have null values

This should be an easy thing to do but I seem to keep getting an extra space. Basically what I am trying to do is combine multiple columns into one column. BUT every single one of these columns might be null as well. When I combine them, I also want them to be separated by a space (' ').
What I created is the following query:
select 'All'= ISNULL(Name+' ','')+ISNULL(City+' ','')+ISNULL(CAST(Age as varchar(50))+' ','') from zPerson
and the result is:
All
John Rock Hill 23
Munchen 29
Julie London 35
Fort Mill 27
Bob 29
As you can see: there is an extra space when the name is null. I don't want that.
The initial table is :
id Name City Age InStates AllCombined
1 John Rock Hill 23 1 NULL
2 Munchen 29 0 NULL
3 Julie London 35 0 NULL
4 Fort Mill 27 1 NULL
5 Bob 29 1 NULL
Any ideas?
select 'All'= LTRIM(ISNULL(Name+' ','')+ISNULL(City+' ','')+ISNULL(CAST(Age as varchar(50))+' ','') from zPerson)
SEE LTRIM()
In the data you have posted, the Name column contains no NULLs. Instead, it contains empty strings, so ISNULL(Name+' ','') will evalate to a single space.
The simplest resolution is to change the data so that empty-strings are null. This is appropriate in your case since this is clearly your intention.
UPDATE zPerson SET Name=NULL WHERE Name=''
Repeat this for your City and Age fields if necessary.
Use TRIM() arount the ISNULL() function, or LTRIM() around the entire selected term