Trim characters - sql

All,
I have 2 values as below in a table.
ID Name
1 Justs_S01_3456
2 S01_56788
The output I want is below.
ID Name NewName
1 Justs_S01_3456 S01_3456
2 S01_56788 S01_56788
I tried using CHARINDEX and other functions,but I am not getting the desired output.
Can some one please help me?
Thanks,
John

In SQL Server, you could use:
select t.*, stuff(name, 1, charindex('S01_', name) - 1, '') as newname
Here is a db<>fiddle.

You can use String functions.
The PATINDEX() function returns the position of a pattern in a string (beginning position 'S01' in the Name)
AND using SUBSTRING function you can extract the string from S01 position to the end of string:
SELECT *, SUBSTRING(Name,PATINDEX('%S01%',Name),len(Name)) As NewName from Table

Related

get last _ position values in sql server

Hi I have one doubt in sql server .
how to get first position to right side specific character position.
table : empfiles
filename:
ab_re_uk_u_20101001
ax_by_us_19991001
abc_20181002
I want output like below:
filename
ab_re_uk_u
ax_by_us
abc
I tried like below :
select SUBSTRING(filename,1,CHARINDEX('2',filename) - 1) as filename from empfiles
above query is not given expected result please tell me how to write query to achive this task in sql server .
If last position has always numeric values then you can use patindex():
select *, substring(filename, 1, patindex('%[0-9]%', filename)-2) as NewFile
from empfiles e;
If you want to get characters after than _ to right sight of string then you can use combo to reverse() and substring()
select *,
reverse(substring(reverse(filename),charindex('_', reverse(filename))+1, len(filename)))
from empfiles e;
Another way is to use reverse in combination with STUFF.
create table f(filename nvarchar(100));
insert into f values
('ab_re_uk_u_20101001')
,('ax_by_us_19991001')
,('abc_20181002');
select
filename=reverse(stuff(reverse(filename),1,charindex('_',reverse(filename)),''))
from f
Try This
CREATE TABLE #DATA([FILENAME] NVARCHAR(100));
INSERT INTO #DATA VALUES
('ab_re_uk_u_20101001')
,('ax_by_us_19991001')
,('abc_20181002');
SELECT [filename],
SUBSTRING([filename],0,PATINDEX('%[0-9]%',[filename])-1) AS ExpectedResult
FROM #Data
Result
filename ExpectedResult
--------------------------------------
ab_re_uk_u_20101001 ab_re_uk_u
ax_by_us_19991001 ax_by_us
abc_20181002 abc
Well, obviously the last position value is a date, and the format is YYYYMMDD so its 8 characters, plus, added by underscore character, so that makes its 9 character.
Assumed by the above statement applied, the following logic of the query should work
SELECT SUBSTRING(ColumnText, 1, LEN(ColumnText) - 9)
Which means, only display characters from character position 1, to character position LEN - 9, which LEN is the length of characters, and 9 is the last 9 digit of number to be removed
Try with this ..
select [filename],SUBSTRING([filename],1,PATINDEX('%_[0-9]%',[filename])-1) from empfiles
Individual Select records
SELECT SUBSTRING('ab_re_uk_u_20101001',1,PATINDEX('%_[0-9]%','ab_re_uk_u_20101001')-1)
SELECT SUBSTRING('ax_by_us_19991001',1,PATINDEX('%_[0-9]%','ax_by_us_19991001')-1)
SELECT SUBSTRING('abc_20181002',1,PATINDEX('%_[0-9]%','abc_20181002')-1)

Use of substring in SQL

My query is the following:
SELECT id, category FROM table1
This returns the following rows:
ID|category
1 |{IN, SP}
2 |
3 |{VO}
Does anyone know how i can remove the first char and last char of the string in PostgreSQL, so it removes: {}?
Not sure, what you mean with "foreign column", but as the column is an array, the best way to deal with that is to use array_to_string()
SELECT id, array_to_string(category, ',') as category
FROM table1;
The curly braces are not part of the stored value. This is just the string representation of an array that is used to display it.
Either using multiple REPLACE functions.
SELECT id, REPLACE(REPLACE(category, '{', ''), '}', '')
FROM table1
Or using a combination of the SUBSTRING, LEFT & LENGTH functions
SELECT id, LEFT(SUBSTRING(category, 2, 999),LENGTH(SUBSTRING(category, 2, 999)) - 1)
FROM table1
Or just SUBSTRING and LENGTH
SELECT id, SUBSTRING(category, 2, LENGTH(category)-2)
FROM table1
You could replace the {} with an empty string
SELECT id, replace(replace(category, '{', ''), '}', '') FROM table1
select id,
substring(category,charindex('{',category,len(category))+2,len(category)-2)
from table1;
select id
,left(right(category,length(category)-1),length(category)-2) category
from boo
select id
,trim(both '{}' from category)
from boo
Trim():
Remove the longest string containing only the characters (a space by
default) from the start/end/both ends of the string
The syntax for the replace function in PostgreSQL is:
replace( string, from_substring, to_substring )
Parameters or Arguments
string
The source string.
from_substring
The substring to find. All occurrences of from_substring found within string are replaced with to_substring.
to_substring
The replacement substring. All occurrences of from_substring found within string are replaced with to_substring.
UPDATE dbo.table1
SET category = REPLACE(category, '{', '')
WHERE ID <=3
UPDATE dbo.table1
SET category = REPLACE(category, '}', '')
WHERE ID <=3

Changing LastName,FirstName to LastName,FirstInitial

I'm sure this is super easy, but how would I go about converting LastName,FirstName to LastName,FirstInitial?
For example changing Smith,John to Smith,J or Johnson,John to Johnson,J etc.
Thank You!
In case of LastName and FirstName columns:
select LastName,substr(FirstName,1,1)
from mytable
;
In case of a fullname saved in a single column:
select substr(fullname,1,instr(fullname || ',',',')-1) || substr(fullname,instr(fullname || ',',','),2)
from mytable
;
or
select regexp_replace (fullname,'([^,]*,?)(.).*','\1\2')
from mytable
;
Here is one way, using just "standard" instr and substr. Assuming your input is a single string in the format 'Smith,John':
select substr(fullname, 1, instr(fullname, ',')+1) from yourtable;
yourtable is the name of the table, and fullname is the name of the column.
instr(fullname, ',') finds the position of the comma within the input string (it would be 6 in 'Smith,John'); thensubstrtakes the substring that begins at the first position (the1in the function call) and ends at the position calculated byinstr`, PLUS 1 (to get the first initial as well).

String manipulation with SQL Server

I have the following table with (man-made) IDs.
ID Name
AB12345 John
12346 Charles
...
How do I write a SELECT that returns only the number segment of the ID column? Like this:
ID Name
12345 John
12346 Charles
...
SELECT SUBSTRING(string, PATINDEX('%[0-9]%', string), PATINDEX('%[0-9][^0-9]%', string + 't') - PATINDEX('%[0-9]%',
string) + 1) AS Number
FROM table
here string is equal to id
You could write a regex to extract the numeric values that you are looking for.
This might help
Query to get only numbers from a string

replace data in a column after some characters in oracle

I have a column with values like:
1492966EMAIL1ABCDEFGHIJK12/22/2012 04:20:35
I want to replace the whole part after EMAIL1 in the column and this has to be done for more than 500000 rows. The problem is that the number of digits before EMAIL1 is not common in all the rows, but the value EMAIL1 is there in all the rows. I am not able to find the right function to go about this as I have tried using substr and trim, but I am not able to get the right query for this.
Can someone please tell me how this can be achieved in Oracle SQL? Let me know if more details are needed on the same.
This will exactly do your purpose,
Select SUBSTR(val,1,instr(val,'EMAIL1')+5) from table1
fiddle_demo
Get the string after 'EMAIL1' and replace it required string,
Select replace(SUBSTR(val,instr(val,'EMAIL1')+5),
'String you want to replace','string that replaces')
from table1
demo
update table1 set val=(Select replace(SUBSTR(val,instr(val,'EMAIL1')+5),
'String you want to replace','string that replaces') from table1)
where lower(val) like '%email1%';
update_demo
i hope your requirement is to concatenate a string after email1..for this sub-string position till email1 and concatenation with your required string ..
select SUBSTR(column_name,1,INSTR(column_name,'EMAIL1')+5)||'string' from table1
It seems like you can use the functions REPLACE(), SUBSTR() and, INSTR() in the following:
select
replace(yourcol, substr(yourcol, instr(yourcol, 'EMAIL1')+6), '') newCol
from yourtable
See SQL Fiddle with Demo
Your final value will be:
| NEWCOL |
-----------------
| 1492966EMAIL1 |
Then if you were to use this in a UPDATE statement, the query would be:
update yourtable
set yourcol = replace(yourcol, substr(yourcol, instr(yourcol, 'EMAIL1')+6), '');
See SQL Fiddle with Demo
Use INSTR function to find position of occurence of string EMAIL1 .
Select substring starting from beginning of original string upto
starting position of EMAIL1 plus 6 characters . 6 is length of EMAIL1 substring.
If you want to select in this manner do
SELECT substr( column_name, 1, instr(column_name,'EMAIL1')+6)
If you want to update column values
UPDATE <table>
SET column_name = substr( column_name, 1, instr(column_name,'EMAIL1')+6)