I need to clean up some records in an SQL table. Somehow the middle initial was placed in front of some last names in the last name field.
The table looks like this:
EmplyID last_name_field
123 A Smith
456 Jones
789 J Gillum
Not all of the records have a middle initial and space in front of them. I can't use TRIM to take away the first two characters in each of the records because it would mess up the ones whose last names where correctly imported. Is there a way to remove the first character and space for the only the records that have the middle initial?
Thanks in advance
Very simple solution. Just remove everything before the space. I was on the right track with TRIM instead use RIGHT and LEN
update TABLE_NAME
Set COLUMN_NAME = RIGHT(COLUMN_NAME, LEN(COLUMN_NAME) - CHARINDEX(' ', COLUMN_NAME))
where CHARINDEX(' ', COLUMN_NAME) > 0
Related
I have a table which have columns and they have data as comma separated
i used replace to replace that comma with space and updated the table, now due to some incompatibility i had to revert the change and i missed to take backup
so my columns which were replaced have spaces like this
The S.T.A.B.L.E.Manual 6th Ed
see the space between Manual and 6th
i want it should go back to this
The S.T.A.B.L.E. Manual, 6th Ed
You should be careful, but you can do:
update t
set col = replace(col, ' ', ', ')
where col like '% %';
This does not handle the space before "Manual". That is not in your description of the problem.
that will sounds stupid, but I have a table with names, those names may finish with white space or may not. E.g. I have name ' dummy ', but even if in the query I write only ' dummy' it will find the record ' dummy '. Can I fix it somehow?
SELECT *
FROM MYTABLE where NAME=' dummy'
Thanks
This is how SQL works (except Oracle), when you compare two strings the shorter one will be padded with blanks to the length of th 2nd string.
If you really need to consider trailings blanks you can switch to LIKE which doesn't follow that rule:
SELECT *
FROM MYTABLE where NAME LIKE ' dummy'
Of course, you better clean your data during load.
There's only one thing which is worse than trailing spaces, leading spaces (oh, wait a minute, you got them, too).
I don't recall ever seeing a field like this before, but it combines the city, state, and zipcode into a single string of varchar2. Fortunately, I believe most of the fields are in the same city space state, space zipcode format, but I started finding a few that deviated from that norm.
Right now I'm trying to identify all these distinct conditions
in the database with over 5 million rows and my queries aren't working for what I wanted.
I started with:
SELECT PROJECT_CTY_ST_ZIP FROM PAYMENT WHERE PROJECT_CTY_ST_ZIP LIKE '%' || CHR(32) || '%';
Then tried:
SELECT PROJECT_CTY_ST_ZIP FROM PAYMENT WHERE PROJECT_CTY_ST_ZIP LIKE '% %' AND PROJECT_CTY_ST_ZIP LIKE '% %' AND PROJECT_CTY_ST_ZIP LIKE '% %';
but they are both pulling based on leading and trailing spaces and I was really wanted to find spaces in the inside of the text. I don't want to remove them, just identify them with a query so I can parse them properly in my java code and then do an insert later to put them into city, state, and zipcode fields in another table.
While it doesn't show it here, I found this field in IA with no leading spaces, then one leading space and then two leading spaces. I fixed the leading spaces with trim.
WEST LIBERTY, IA 52776
This last one I wasn't expecting and I wanted to see if there are other conditions that might be unusual, but my query doesn't find them as the spaces are in the middle of the text:
TRUTH OR CONSEQUENCE, NM 87901
How would I go about a query to find these kinds of distinct records?
This query replaces each of the spaces with a dot (.) so you can see them
SELECT
REGEXP_REPLACE(PROJECT_CTY_ST_ZIP,
'([[:space:]])',
'.') spaces_or_now_dots
FROM PAYMENT
This query finds the ones that have one or more spaces.
SELECT PROJECT_CTY_ST_ZIP
FROM PAYMENT
where REGEXP_LIKE(PROJECT_CTY_ST_ZIP,
'[[:space:]]'
)
I have not considered the cases of spaces in the beginning and end, because you have already taken care of them.
I have a big bunch of cells in Excel that look like the following:
FName LName, Loc JB
Abbreviations are bad, so: First Name, Last Name, Location, Job.
I need to move that so it looks like this:
FName LName || LOC || JB
Caveats:
Must remove the , after the name.
Must capitalize the location (it's 2 or 3 letters, inconsistently capitalized. I want to make them all caps).
JB is anywhere from 1 to 4 characters on the end. I just need to take that last bit and dump it in.
They're all separated by at least a space (the first has a comma and a space).
I'd like a macro to do this, because I have to do it with relative frequency, and doing 200 rows of this by hand is a pain. Any help?
Sounds like all you need is a formula in the next column. If your values are in column A starting with cell A1, try:
=LEFT(A1,FIND(",",A1)-1)&" || "&UPPER(MID(A1,FIND(",",A1)+2,FIND(" ",A1,FIND(",",A1)+2)-FIND(",",A1)-2))&" || "&RIGHT(A1,LEN(A1)-FIND(" ",A1,FIND(",",A1)+2))
This formula takes everything to the left of the comma and adds " || ". Then it finds the next space starting its search two characters after the comma. Using that index it then can extract the location and make it upper case. Then again we add " || ". Then knowing the index of that space we can grab everything to the right to grab the job. This same logic can be applied in VBA but this is probably a quicker solution and easier to pass between computers.
You don't necessarily need a macro. You can do it with a series of right()'s left()'s mid()'s and find()'s. Also need to use Upper() for the loc.
For instance, if your data is in column A, to get a column with first and last name in column B, in B1 you could use:
=LEFT(A1,FIND(",",A1)-1)
That'll return everything up to but not including the comma. For Loc, assuming there's a comma between Loc and JB in C1 you'd use:
=UPPER(MID(A1,FIND(",",A1)+2,FIND(",",A1,FIND(" ",A1)+1)-FIND(" ",A1)-3))
That'll return the uppercase version of the middle of the string, starting 2 chars after the first comma (so you don't get the space), and ending 2 less than the difference between the first and second commas. If there's no comma, you could do a similar set of searches to find where that last space is.
The last IN d1 is:
=RIGHT(A1,LEN(A1)-FIND(" ",A1,FIND(",",A1)+2))
edited after the clarification of commas and spaces.
I have a Company table in SQL Server and I would like to retrieve list of data related to particular companies and list of companies is very huge of around 200 company names and I am trying to use IN clause of T-SQL which is complicating the retrieval as few the companies have special characters in their name like O'Brien and so its throwing up an error as it is obvious.
SELECT *
FROM COMPANY
WHERE COMPANYNAME IN
('Archer Daniels Midland'
'Shell Trading (US) Company - Financial'
'Redwood Fund, LLC'
'Bunge Global Agribusiness - Matt Thibodeaux'
'PTG, LLC'
'Morgan Stanley Capital Group'
'Vitol Inc.'..
.....
....
.....)
Above is the script that is not working for obvious reasons, is there any way I can input those company names from an excel file and retrieve the data?
The easiest way would be to make a table and join it:
CREATE TABLE dbo.IncludedCompanies (CompanyName varchar(1000)
INSERT INTO dbo.IncludedCompanies
VALUES
('Archer Daniels Midland'),
('PTG, LLC')
...
SELECT *
FROM Company C
JOIN IncludedCompanies IC
ON C.CompanyName = IC.CompanyName
I do not think that mysql knows how to handle excel format, but you can fix your query.
Check how complicated names are stored in database (check if they have escape characters in them or anything else".
Replace all ' with \' in your query and it will take care of the ' characters
mysql> select now() as 'O\'Brian'; returns
O'Brian
2014-03-17 15:06:39
So i'm guessing you have a excel sheet with a column containing these names, and you want to use this in your where clause. In addition, some of the values have special characters in them, which needs to be escaped.
First thing you do is to escape the '-characters. You do this in excel, with a search replace for all occurences of ' with '' (the escaped version in sqlserver (\' in MySQL.)) Then, create a new column on each side side of your companies column, and in the first row input a ' on the left hand side, and ', on the right. Then use the copy cell functionality (the little square in the bottom right of the cell when you select it) to copy the cells to the left and right to all the rows, as far as the company list goes (just grab the square and pull it downwards..)
Then, take your list, now containing three columns and x rows and paste it into your favorite text editor. It should look something like this:
' Company#1 ',
' Company with special '' char ',
[...]
' Last company ',
Now, you will have some whitespace to get rid of. Use search replace and replace two space characters with nothing, and repeat (or take the space from the first ' to the start of the text and replace this with nothing.
Now, you should have a list of:
'Company#1',
'Company with special '' char',
[...]
'Last company',
Remove the last comma, and you'll have a valid list of parameters to your in-clause (or a (temporary) table if you want to keep your query a bit cleaner.)