I'm trying to remove the last two characters from my results - sql

I am running into a problem when I pull in the city name from my database that includes the 2 character state abbreviation at the very end.
Examples
CITY
WELLSBURG WV
FRANKFORT NY
ORANGE TX
I thought I could fix the problem by using the following SUBSTR function, but the table holds 27 characters which adds spaces at the end of each string to fill the 27 character requirement. Therefore, I'm not able to use the function because the spaces at the end of each result varies.
SUBSTR(S.CITY_NAME, 1, LENGTH(S.CITY_NAME)-2) AS "CITY"
I appreciate any suggestions to get around this issue.

You should check the RTRIM, LTRIM and TRIM functions and use it to remove the spaces at at the end each word. Something like this:
SUBSTR(TRIM(S.CITY_NAME), 1, LENGTH(TRIM(S.CITY_NAME))-2) AS "CITY"
Check this link for more details.

Related

Remove space between number and character - PostgreSQL/REGEXP_REPLACE

I have a table with medication_product_amount column where there are spaces between numbers and characteres like below:
medication_product_amount
1 UN DE 50 ML
20 UN
1 UN DE 600 G
What I want is to remove the single space ONLY between numbers and characters, something like this:
new_medication_product_amount
1UN DE 50ML
20UN
1UN DE 600G
To do this, I am looking for a regular expression to use in the function REGEXP_REPLACE. I tried using the pattern below, indicating to replace the single space after the numbers, but the output remained the same as the input:
select REGEXP_REPLACE(medication_product_amount, '(^[0-9])( )', '\1') as new_medication_product_amount
from medications
Can anyone help me come up with the right way to do this? Thanks!
Your regex is a little off. First what yours does. '(^[0-9])( )', '\1')
(^[0-9]) Start Capture (field 1) at the beginning of the string for 1 digit
followed by Start Capture (field 2) for 1 space.
Replace the string by field1.
The problems and correction:
What you want to capture does not necessary the first character of the string. So eliminate the anchor ^.
What you want to capture may be more that 1 digit in length. So replace [0-9] by [0-9]+. I.E any number of digits.
Not actually a problem but a space holds no special meaning in a regexp, it is just a space so no need to capture it unless user later. Replace ( ) with just .
END of Pattern. But there may be other occurrences. Tell Postgres to continue with the above pattern until end of string. (see flag 'g').
Resulting Expression/Query: (demo here)
select regexp_replace(medication_product_posology, '([0-9]+) ', '\1','g') as new_medication_product_posology
from medications;
Match "digit space letter", capturing and the digit and letter using '([0-9]) ([A-Z])', then put them back using back references.
select REGEXP_REPLACE(medication_product_amount, '([0-9]) ([A-Z])', '\1\2') as new_medication_product_amount
from medications

How can I return Special Characters in SQL?

I've been searching and I couldnt found exactly what I need.
I have and SQL Server 2008
I have some strings in a table with special characters such as "!,;:-()" and I'm trying to make a script that could return this characters, BUT ONLY THOSE CHARACTERS (do you get it?)
For example, I have "Borges, Ricardo" and I need to return only the ","
Another example, "Calle 13 Nº 34, Mercedes Bs As ( 6600)", and I only need the "º,()"
I dont want to get the letters or numbers.
I wrote this simple script:
SELECT name FROM table WHERE name LIKE '%[^A-Za-z0-9 ]%'
SO I could get all the rows where there is a Special Character like a (,;.:-()&%$... but I need to RETURN ONLY the special Characters. Get it? No letters, no numbers, only the special characters in the row.
THank you very much for your help!

LIKE Wild Card Search in Teradata

I have text in a column like RAPP 01. RAPP 02 upto RAPP 45 and RAPP 99.I included all these values manually in my IN statement in my WHERE clause but it slows the query as the data set in huge. I tried WHERE SUBSTR(REMARK_TXT,1,7) LIKE 'RIPA [01-45,99]' and it did not return any data. Can you please help?
Thanks!
You could use REGEXP functionality here:
WHERE REGEXP_SIMILAR(REMARK_TXT, '^RAPP [0-9]{2}$') = 1;
That regex matches with a string that starts with RAPP followed by a space then followed by 2 numbers and the end of the string.
Updating to deal with two number ranges (01-49) and (99). This isn't the best thing to do with regex, but it's still possible:
WHERE REGEXP_SIMILAR(REMARK_TXT, '^RAPP ([0-4][0-9]|99)$') = 1;
This is saying a string that starts with RAPP and then ends in either a two digit number that starts with 0 through 4 OR the number 99
You could use the following:
where column like ('RAPP %')
Which would return anything beginning with the string RAPP and a whitespace. Notice the '%' sign, this will be your wildcard.
Careful on using like, especially not like and putting the wildcard at the beginning of your condition, it would have much bigger performance issues.

SQLite regular Expressions regex get exact word by number

I have a string like the following in a sqlite 1 column:
1a 2B 3c 354 AfS 151 31s2fef 1fs31 3F1e2s 84f64e 45fs
space separated, x amount of characters 0-9, a-z, A-Z, there might be punctuation I'm not sure, but it is definitely space separated.
I'm trying to make a regular expression so I can query the database by number of words. basically if I wanted to get the 6th "word" in the example I'd be looking for:
151
so I tried to make a regular expression that says if the Nth word = 151, return me that row.
Here's what I've got so far.
SELECT * FROM table1 WHERE column1 REGEXP ^((?:\S+\s+){1}){6}
That unfortunately gives me the first through sixth words, but I really want to pinpoint the 6th word like the example above.
Also, I was thinking so save room in the database I could get rid of the white space, I'd just need to know how to count a specific number of characters in to the string which I couldn't figure out either.
Thanks for the help, never written a regular expression before.
If all you need is a match, there is no need for the non-capturing group. Just match a non-space + space group 5 times and follow with a 151.
^(\S+\s+){5}151
Use following regex
^([^\s]+\s){6}(.*?)(\s|$)
this must return 151, you can change {6} with your number to match the string.

Postgresql query to update fields using a regular expression

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.