I'm trying to pull data from a column in a table I have called Vendors and the column name is VendorContactLName. I also have a column VendorContactFName. I'm trying to make it so as I pull the data, it conditionally changes the output of the string.
For example, I want to add an apostrophe s <'s> to the end of the name which I can do with:
SELECT VendorContactFName + ' ' + VendorContactLName + '''s' AS 'Vendor Full Name' FROM Vendors;
However, I'd like to make it so that if it happens to be that the last name in VendorContactLName already ends with an "s" that I only concatenate an apostrophe to it, not apostrophe and an 's'.
So, I'd like: Karl Allen, Michael Dunlap, and Michelle Higgins to come out as Karl Allen's, Michael Dunlap's, and Michelle Higgins'.
Thanks for the help.
Check this:
SELECT VendorContactFName + ' ' +
(case right(VendorContactLName,1)
when 's' then VendorContactLName + ''''
else VendorContactLName + '''s'
end) as 'Vendor Full Name'
FROM Vendors
demo here
Related
Table in SQLITE, want to simple way to delete everything to a right of a set phrase/character in the Company_name_ column, in this case everything after "LLC":
Company_name_
Example LLC $42
Example llc,klp
Example LLc jim
becomes
Company_name_
Example LLC
Example llc
Example LLc
Tried Set Charindex and Substr but getting syntax errors.
Thank you
You can do it with string functions SUBSTR() and INSTR().
If you want a SELECT query use a CASE expression with the operator LIKE to check if the column value contains 'LLC' or not:
SELECT CASE
WHEN Company_name_ LIKE '%LLC%'
THEN SUBSTR(
Company_name_,
1,
INSTR(UPPER(Company_name_), 'LLC') + LENGTH('LLC') - 1
)
ELSE Company_name_
END Company_name_
FROM tablename;
If you want to update the table:
UPDATE tablename
SET Company_name_ = SUBSTR(
Company_name_,
1,
INSTR(UPPER(Company_name_), 'LLC') + LENGTH('LLC') - 1
)
WHERE Company_name_ LIKE '%LLC%';
See the demo.
I need to concatenate the columns LastName and FirstName into a new column called EmployeeName. The problem is that some first name fields include a middle initial and some do not. We do not want the initial in the EmployeeName column. How do I remove it from those instances?
I have tried trim functions, left functions, right functions and I cannot get it to work quite right. I have tried concatenating the columns then cleaning it up, that does not work either
SELECT LEFT(EmployeeName, LEN(EmployeeName) - 2) FROM myTable
but this removes the last characters even for those with no middle initial. I have it as -2 to account for the space between FirstName and Middle Initial
When the EmployeeName field is Smith, John J it removes the space and J correctly
When the EmployeeName field is Smith, John it removes the 'hn'. I don't want that.
Thank you very much
I figured it out. I count the spaces then use a case statement to concatenate the fields
select LastName, FirstName,
(
case
when len(firstname) - len(replace(firstname, ' ', '')) > 0
then concat(LEFT(firstname, LEN(firstname) - 2),', ',LastName)
ELSE concat(FirstName, ', ', Lastname)
END
) as namesCombined
from myTable
I've had a really hard time tracking down a solution for this--though I'm sure it's out there. Just not sure of the exact wording to get what I'm looking for.
I have a huge data set where some of the data is missing information so it is not uniform. I want to extract just the name into one column and the e-mail in to the next column.
The best way I can narrow this down is there is a space between each unique entry with the name always being in the first box.
Example:
John Doe
John Doe's Company
(555) 555-5555
John.doe#johndoe.com
John Doe
(555) 555-5555
John Doe
Jane Doe's Company
John.doe#johndoe.com
With the results wanted being (in two excel columns):
John Doe | john.doe#johndoe.com
John Doe |
John Doe | john.doe#johndoe.com
Any suggestions on the best way to do this would be appreciated it. To make it complicated if there was no e-mail I would want to ignore that set completely, but I could just manually check.
VBA coding:
1. Indicate in Row1 the initial row where the data begins.
2. Place a flag in this case the word "end" to indicate the end of the information.
3. Create a second sheet
Sub ToList()
Row1 = 1 'Row initial from data
Row2 = 1 'Row initial to put list
Do
Name = False
Do
field = Trim(Sheets(1).Cells(Row1, 1))
If field <> "" And LCase(field) <> "end" And Not Name Then
Sheets(2).Cells(Row2, 1) = field
Name = True
End If
Row1 = Row1 + 1
Loop Until (IIf(field = "" Or LCase(field) = "end", True, False))
fieldprev = Sheets(1).Cells(Row1 - 2, 1)
If InStr(fieldprev, "#") > 0 Then
Sheets(2).Cells(Row2, 2) = fieldprev
End If
Row2 = Row2 + 1
Loop Until (IIf(LCase(field) = "end", True, False))
End Sub
Extracting the e-mail address shouldn't be too difficult as you just need to is search for a string containing the # character. A series of search() and mid() functions can be used to separate out the individual words. Search for each instance of a space and use that value in a mid() function. Then search for # in the results and you should find the e-mail address. Extracting the name will be more difficult if the original data is very messy.
However I second the comment above about using an external script, especially for a large dataset. Excel isn't really designed for the sort of thing you are describing here.
I have a column of data with multiple value types in it. I am trying to separate out out each value type into a separate column. Below an example of the data:
6 - Cutler, Jay (Ovr: 83)
22 - Forte, Matt (Ovr: 88)
86 - Miller, Zach (Ovr: 80)
I tried to separate the data by a) going to data and clicking text to columns; however, the "Ovr: 80" portion of the data does not separate "Ovr" from 80. I also tried b) to convert to .csv file, but again was unable to separate "Ovr" from "80". Is there a formula I can use to separate this portion of the data from the rest?
I would like the data to be separated into different columns as show below:
6 | Cutler, | Jay | Ovr | 83
22 | Forte | Matt | Ovr | 88
86 | Miller | Zach | Ovr | 80
Any insight is much appreciated!
Select the cells you wish to process and run this macro to place results in the cells to the right of the selected cells:
Option Explicit
Sub dural()
Dim r As Range, s As String, ary
Dim i As Long, a
For Each r In Selection
s = r.Value
If s <> "" Then
s = Replace(Replace(s, "-", " "), ",", " ")
s = Replace(Replace(s, "(", " "), ")", " ")
s = Application.WorksheetFunction.Trim(Replace(s, ":", " "))
ary = Split(s, " ")
i = 1
For Each a In ary
r.Offset(0, i).Value = a
i = i + 1
Next a
End If
Next r
End Sub
using the method above your could do something like this...
first clean the text so its more manageable, using this formula and copying in a column you can clean it so it become a space delimited set
=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,"- ",""),",",""),"(",""),")",""),":","")
from there just copy the values the formula give you to a new sheet maybe and then use 'Text To Columns to get it split into columns.
For the record I do not recommend this method if you are willing to do the text to column option.
Functions used for this solution are:
LEFT function
FIND function
MID function
for your first column of text use the following:
=left(A1,find(" ",A1))*1
That will pull out the first number presuming you do not have any leading spaces. The *1 converts from text to a number.
for your second column of last times use the following:
=MID(A1,FIND("-",A1)+2,FIND(",",A1)-(FIND("-",A1)+2))
Provided you have a coma and a dash as indicated in your example data you will not get an error and it should pull the last name without the coma.
For your third column of first names follow the same general technique as last names with the following,
=MID(A1,FIND(",",A1)+2,FIND("(",A1)-2-(FIND(",",A1)+2)+1)
Follow the similar pattern to get you over column
=MID(A1,FIND("(",A1)+1,FIND(":",A1)-1-(FIND("(",A1)+1)+1)
and finally to get your age column use this:
=MID(A1,FIND(":",A1)+2,FIND(")",A1)-1-(FIND(":",A1)+2)+1)
copy the above formulas down as far as you need to go.
I have a spreadsheet where I increment the value/string in a cell by one up to row 25000
="User_"&ROW(A1) or =CONCATENATE("User_",ROW(A1),"#mail.com")
This works fine.
Now, my question is how do I put a conditional in where I want to increase the value for each row by one up to 100 but then want to start at one again?
User_1
User_2
User_3
'
'
User_100
User_1
User_2
'
'
User_200
User_1
'
'
User_25000
Instead of ROW(A1) (or simply ROW()), use IF(MOD(ROW(),100)=0,ROW,MOD(ROW,100))
=IF( MOD(ROW()-1,100)=0,ROW()-1,MOD(ROW()-1,100) )
If you have headers.
And with the email info it should be like:
="User_" & IF(MOD(ROW()-1,100)=0,ROW()-1,MOD(ROW()-1,100)) & "#mail.com"
="User_"&MOD(ROW()-2,100)+1&"#mail.com"
The -2is because you said it starts on row 2, so if it starts in another row, replace the 2 with that number.
Replace the 100with whatever number you want it to go up to before repeating.
The +1is to eliminate the IFlogic needed by the other answers to this question.
Sorry this is well after the fact of the question, but hopefully it'll help someone else!
**bonus tip - if you want it to be "User_001#mail.com" (pad out the number to 3 digits), use the TEXT function like this: ="User_"&TEXT(MOD(ROW()-2,100)+1,"000")&"#mail.com"