Fetching a part of field name by searching for a character pattern - sql

I have a field that has values like:
ABCD3 100MG
EFGHI 0.5 UNITS/ML
JKL MNO PQR
STU V 100-1.5 MCG ABC
W-X/Y Z 100-750
...
These values are just a sample (there could be more patterns)
I am looking to write a query to fetch only the first character portion of the values, in this example:
ABCD3
EFGHI
JKL MNO PQR
STU V
W-X/Y Z
What is the best way to do this?
Thought - Use a substring type function to capture all text before '%%'? I could get to this because there is no set number of characters or position to look for, and could not generalize numbers (there may be a different way to do this).
***Using SQL Server 2008 R2
Examples are appreciated!
Thank you!

Related

Delete number of string characters from left in Column B depending on length of string in column A of pandas dataframe

As described in the title, I have the following problem:
Data is prepared as a pandas dataframe incoming as follows:
Article
Title
A0
A00183
BB2
BB2725
C2C3
C2C3945
As you can see, the "Title" column is repeating the string value of the Article column.
I want this to be deleted, so that the table looks as follows:
Article
Title
A0
0183
BB2
725
C2C3
945
I want to do this with Pandas.
I already found out how to read the length of the string row in column Article, so that I already know the amount of characters to be deducted with this:
df1['Length of Article string:'] = df1['Article:'].apply(len)
But now I am to stupid to figure out how to delete the strings, that can change in amount for every row, in the Title column.
Thanks for your help!
Kind regards
Tried Pandas Documentation, found some hints regarding split and strip, but I do not have enough know-how to implement...
You can replace from list derived from Article column.
df["Title"] = df["Title"].replace(df["Article"].tolist(), "", regex=True)
print(df)
Article Title
0 AA 0123
1 BBB 234
2 CCCC 345
you can use replace() with a lambda function.
dfx = df[['Article','Title']].apply(lambda x : x['Title'].replace((x['Article']), ''), axis=1)

Retrieve only alphabets from a specific column Postgres

I have a field in a table where I have to get only alphabets and ignore numbers and special characters. How can I get that output?
Example:
abc (234)454-4546
232-454-565 xyz
Ell # 83493
Expected Output:
abc
xys
ell
Try
SELECT REGEXP_REPLACE('ABC12345xyz','[^[:alpha:]]','','g');

Need a way to split string pandas to colums with numbers

hi i have string in one column :
s='123. 125. 200.'
i want to split it to 3 columns(or as many numbers i have ends with .)
To separate columns and that it will be number not string !, in every column .
From what I understand, you can use:
s='123. 125. 200.'
pd.Series(s).str.rstrip('.').str.split('.',expand=True).apply(pd.to_numeric,errors='coerce')
0 1 2
0 123 125 200

Change the values in the DF column pandas, Pythonic

I want change all strings in the pandas DF to the corresponding* integers.
I can use this routine:
k=0
for item in df['someColumn'].unique():
df.replace(item, k)
k += 1
or even I can do list comprehension... Anyway I want to ask, does somebody know about probably some specific method in Pandas, which could replace ALL strings in some column with the corresponding (=different) integer (or float) values..? Please, propose only pythonic way.
Probably some additional aspect. My column doesn't have some numeric values in the string format, just really strings (= concatenation of the chars):
C
asd
bbd
ksl
asd
asd
ksl
I want
C
1
2
3
1
1
3
*Probably the word corresponding is a little bit confusing here, I'm very sorry

tsql coalesce with float and varchar

I have a float field which shows data as such:
1
1.00
3.12
3.00
I also have a varchar field that shows as such:
NA
ND
I
Data is as such: Fld_N is a float and Fld_S is varchar
Fld_N Fld_S
----- ------
1
ND
1.00
3.12
3
NA
I
Notice that a row can have a value for either the Fld_N or the Fld_S but not both.
What I am doing is using the coalesce as such:
COALESCE(STR(Fld_N,9,2), Fld_S) Fld
This doesn't quite work well as I have the decimal points always be upto 2 decimal points whereas I need it to support showing 1 as well as 1.00. Is there a way to not specify the decimal points and still accomomdate for showing 1 and 1.00 in my example?
try the convert function:
coalesce(convert(varchar,Fld_N),Fld_S) Fdl
Instead of STR, use
CAST(fld_N AS VARCHAR(9))
The VARCHAR will only use as many decimal places as necessary to show the value you provide.
Putting that into your COALESCE will yield:
COALESCE(CAST(fld_N AS VARCHAR(9)), Fld_S) AS Fld
In a float type column, there is absolutely no difference between 1 and 1.00. Therefore, what you suggest is actually impossible. The data stored in your database for rows 1 and 3 are, in reality, identical.
However, you can cast to VARCHAR instead of using STR, which will use the least number of decimal places necessary:
COALESCE(CAST(fld_N AS VARCHAR(12)), Fld_S) AS Fld
This should produce:
Fld
-----
1
ND
1
3.12
3
NA
I