Postgresql query to update fields using a regular expression - sql

I have the following data in my "Street_Address_1" column:
123 Main Street
Using Postgresql, how would I write a query to update the "Street_Name" column in my Address table? In other words, "Street_Name" is blank and I'd like to populate it with the street name value contained in the "Street_Address_1" column.
From what I can tell, I would want to use the "regexp_matches" string method. Unfortunately, I haven't had much luck.
NOTE: You can assume that all addresses are in a "StreetNumber StreetName StreetType" format.

If you just want to take Street_Address_1 and strip out any leading numbers, you can do this:
UPDATE table
SET street_name = regexp_replace(street_address_1, '^[0-9]* ','','');
This takes the value in street_address_1 and replaces any leading string of numbers (plus a single space) with an empty string (the fourth parameter is for optional regex flags like "g" (global) and "i" (case-insensitive)).
This version allows things like "1212 15th Street" to work properly.

Something like...:
UPDATE table
SET Street_Name = substring(Street_Address_1 FROM '^[0-9]+ ([a-zAZ]+) ')
See relevant section from PGSQL 8.3.7 docs, the substring form is detailed shortly after the start of the section.

Related

How to filter String in where clause

I would like to extract the string using where clause in SAP HANA.For an example,these are 3 strings for name column.
123._SYS_BIC.meag.app.qthor.cidwh_eingangsschicht.backend.dblayer.l2.checks/MasterData_Holdings.
153._SYS_BIC.meag.app.qthor.centralAdministration.backend.dblayer.l2.checks/AuditAndSecurities.
meag.app.qthor.centralAdministration.backend.dblayer.l2.checks/GeneralLedger
After filter the name column using where clause, output in the name column would be shown only the last portion of the string. So, output will be like this. That means whatever we have, just remove from the beginning till '/'.
"MasterData_Holdings"
"AuditAndSecurities"
"GeneralLedger"
You can try using the REPLACE_REGEXPR
I'm not familiar myself with Hana but the function is pretty straight forward and it should be:
select REPLACE_REGEXPR('.+/(.+)' IN fieldName WITH '\1' OCCURRENCE ALL) as field
...
where
... -- your filter
Be aware that this regex '.+/(.+)' will eat everything until the last / so for instance if you have ....checks/MasterData_Holdings/Something it will return only Something

Comma inside like query fails to return any result

Using Oracle db,
Select name from name_table where name like 'abc%';
returns one row with value "abc, cd" but when I do a select query with a comma before % in my like query, it fails to return any value.
Select name from name_table where name like 'abc,%';
returns no row. How can I handle a comma before % in the like query?
Example:
Database has "Sam, Smith" in the name column when the like has "Sam%" it returns one row, when i do "Sam,%" it doesn't return any row
NOT AN ANSWER but posting it as one since I can't format in a comment.
Look at this and use DUMP() on your own machine... see if this helps.
SQL> select dump('Smith, Stan') from dual;
DUMP('SMITH,STAN')
-----------------------------------------------------
Typ=96 Len=11: 83,109,105,116,104,44,32,83,116,97,110
If you count, the string is 11 characters (including the comma and the space). The comma is character 44, and the space is character 32. If you look at YOUR string and you don't see 44 where the comma should be, you will know that's the problem. You could then let us know what you see there (just for that character, I understand posting "Leno, Jay" would be a violation of privacy).
Also, make sure you don't have any extra characters (perhaps non-printable ones!) right before the comma. Just compare the two strings you are using as inputs and see where the differences may be.

Get a Count of a Field Including Similar Entries MS Access

Hey all I'm trying to parse out any duplicates in an access database. I want the database to be usable for the access illiterate and therefore I am trying to set up queries that can be run without any understanding of the program.
My database is setup where there are occasionally special characters attached to the entries in the Name field. I am interested in checking for duplicate entries based of the fields field1 and name. How can I include the counts for entries with special characters with their non-special character counterparts? Is this possible in a single step or do I need to add a step where I clean the data first?
Currently my code (shown below) only returns counts for entries not including special characters.
SELECT
table.[field1],
table.[Name],
Count(table.[Name]) AS [CountOfName]
FROM
table
GROUP BY
table.[field1],
table.[Name]
HAVING
(((table.[Name]) Like "*") AND ((Count(table.[Name]))>1));
I have tried adding a leading space to the Like statement (Like " *"), but that returns zero results.
P.S. I have also tried the Replace statement to replace the special characters, but that did not work.
field1 ##
1234567
1234567
4567890
4567890
name ##
brian
brian
ted
ted‡
Results
field1
1234567
name
brian
countofname
2
GROUP BY works by placing rows into groups where values are the same. So, when you run your query on your data and it groups by field1 and name, you are saying "Put these records into groups where they share a common field1 and name value". If you want 4567890, ted and 4567890, ted† to show in the same group, and thus have a count of 2, both the field1 and name have to be the same.
If you only have one or two possible special characters on the end of the names, you could potentially use Replace() or Substring() to remove all the special chars from the end of the names, but remember you must also GROUP BY the new expression you create; you can't GROUP BY the original name field or you won't get your desired count. You could also create another column that contains a sanitized name, one without any special character on the end.
I don't have Access installed, but something like this should do it:
SELECT
table.[field1],
Replace(table.[Name], "†", "") AS Name,
Count(table.[Name]) AS [CountOfName]
FROM
table
GROUP BY
table.[field1],
Replace(table.[Name], "†", "")

Using SQL to make specific changes in a database.

I am trying to figure out some commands/code in SQL.
I have database with names, addresses IDs etc, but I have to convert firstname values ending in “jnr” to “(Jnr)” and those ending in “snr” to “(Snr)”.
How do I do this?
update table TABLE_NAME set NAMES = '*xyz*Jnr' where NAMES like '%jnr'
Update or select:
PASTE(column, CHAR_LENGTH(column)-3, 1, UPPER(SUBSTRING(column FROM CHAR_LENGTH(column)-3 FOR 1)
WHERE column LIKE '%jnr' OR column LIKE '%snr'
PASTE is used to put in one character at position 3 from end,
CHAR_LENGTH to get length of column value,
UPPER converts character to upper case,
SUBSTRING is used to pick one character here (j or s),
LIKE is used to find values ending with jnr, or snr.
All ANSI SQL (no dbms specified!)

copy part of one column to another column in the same table

I need to copy part of one column into another column. The delimiter is "-". I don't want to remove that part from the first column.
Example:
ItemDesc Part#
Glowear_black-1234
So it needs to look like this:
ItemDesc Part#
Glowear_black-1234 1234
The only SQL query I can find cuts the information from the ItemDesc column and pastes it into Part#. I still need the "1234" in the first column. Also not all of the ItemDesc have a "-" (which is fine).
In Access SQL, you can use InStr() to find the position of "-" within your field. Then you can use Mid() to return the substring which starts one character after that position.
Note you must bracket Part# in your SQL statement because of the # character in its name.
UPDATE tblIOT1
SET [Part#] =
Mid(ItemDesc, InStr(1, ItemDesc, "-") + 1)
If ItemDesc could be Null or contain a string without a "-", add a WHERE clause to ignore those rows in the UPDATE.
WHERE ItemDesc ALike '%-%'
It is not explicitly clear what to do for the case where ItemDesc lacks the hyphen symbol as a delimiter, but here is my suggestion. Use the substring function to grab everything to the right of the hyphen, where everything to the right is bound by the index of the hyphen character plus one, and the length of the string:
update user.table
set Part# = substring(ItemDesc, (CHARINDEX('-', ItemDesc)+1), LEN(ItemDesc) )
Taking your statement from the comments:
UPDATE tblIOT1
SET Part#= Trim(Mid(ItemDesc, InStr(ItemDesc, '-') +1))
where ItemDesc like "*-*"
Note that I am using the star character and the other respondent uses the percent character, either appear to be valid to MS Access as specified here