HI I have data in one column as this:
in SQL Server 2008
Aetna (AETNA)
I need to update this so that it will be Aetna that is to remove everything after the first parens in an update mode.
You can use CHARINDEX() to find the position of the (, then select everything to the LEFT() of that:
SELECT RTRIM(LEFT('Aetna (AETNA)',CHARINDEX('(','Aetna (AETNA)')-1))
Need to subtract 1 from the length to also remove the (, and RTRIM() removes any extra blank space from the right side.
Just replace the hardcoded string with your column name.
The update would be:
UPDATE table
SET col = RTRIM(LEFT(col,CHARINDEX('(',col)-1))
WHERE col like '%(%'
If you need to do this for all records:
update t
set col = left(col, charindex(' (', col))
where col like '% (%';
Related
Here is my column :
column
abc1234
abc5678
abc4567
Now I need to remove the abc only from the column. Please help me write a query.
You might want to use REGEXP_REPLACE here:
UPDATE yourTable
SET col = REGEXP_REPLACE(col, '^abc', '')
WHERE col LIKE 'abc%';
If you don't care about the particular position of abc, and accept removing all occurrences of it anywhere, then we can do without regex:
UPDATE yourTable
SET col = OREPLACE(col, 'abc', '')
WHERE col LIKE 'abc%';
I have column with data like:
'2020193'
'3208391'
'1038291'
'9349203'
The data type is varchar and I can't change it to int (data managed in this datatype always).
I have some rows with trailing spaces like:
' 2222928'
' 3213331'
I need to remove that trailing space from start. I have tried SUBSTRING() or TRIM()/RTRIM()/LTRIM(), but didn't worked any of those.
select (rtrim(ltrim(doc_id))) from bpm.sales where len(doc_id) = 8
select left(doc_id,2) from bpm.sales where len(doc_id) = 8
select charindex(' ',doc_id) from bpm.sales where len(doc_id) = 8
Also, when I am trying to search the data like:
select doc_id from bpm.sales where doc_id = ' 2269203'
I am geting nothing where it exist in the column. With CHARINDEX() I got 0.
Can someone explain me this behaviour and suggest a solution?
You can get rid of everything up to the first character you do want:
select stuff(doc_id, 1, patindex('%[^0-9a-zA-Z]%', doc_id) - 1, '')
I want to update only the values in Column1 that have characters of a string, then a comma, then (without a space) more characters, like so: abc,def or abc,123,def. I want a space to be added between the comma and the next character, like so: abc, def or abc, 123, def. It shouldn't add an extra space if there already is one.
Sorry, I don't have any existing SQL to show-- I'm not sure where to even start on this.
I'd first replace all instances of , (comma + space) with , (comma, NO space). That way everything is uniform. Then go the other way; replace commas with comma + space
Perhaps a little overkill, but this will support any combination of space(s) before and after a comma
Example
Declare #S varchar(max) = 'abc , 123 , def, ddd'
Select replace(replace(replace(replace(#S,',','<>'),' ','<>'),'><',''),'<>',', ')
Returns
abc, 123, def, ddd
I should add, this is a little trick from Gordon several months ago. I don't have the original link
This would do what you are wanting
UPDATE TABLE
SET Col1 = REPLACE(REPLACE(Col1,', ',','),',',', ')
WHERE Col1 LIKE '%,%'
I think you are need to first remove space and then add space to column1 near
UPDATE yt set yt.Column1 =replace(replace(yt.Column1 ,' ',''),',',', ')
from yourtable yt
where yt.Column1 like '%,%'
If one needs exactly one space after the comma and the data is not consistent there is also the option of a string_split and then string_agg:
DECLARE #test varchar(max) = 'Test, Test 12,Test34,Test56 , Test78'
select STRING_AGG(Trim(value),', ') as Test from string_split(#test,',')
Result:
Test, Test 12, Test34, Test56, Test78
If possible you should use the new "enable_ordinal" option of string_split, but i actually never had problems with the order.
I have a column with data zeros after 6th column
i want to remove the leading zero after the 6th pipe in the data.
Please let me know if there is any way to do it. I tried to use substr with Trim but its not working.
Let's say your column is called COL something like the following should work:
CONCAT(SUBSTR(1,INSTR(COL,'|', 1,5)), LTRIM(SUBSTR(INSTR(COL,'|', 1,5)+1)),'0'))
Assuming from your current data that you need to only replace every occurrence of |00 with |, You can achieve thing using REPLACE function.
SELECT 'TMB|MLE020828585|74384911WA3S|="''07300058"|74384911|0013' AS Current_String,
replace('TMB|MLE020828585|74384911WA3S|="''07300058"|74384911|0013', '|00', '|') AS result_String
You can replace hard-coded value in above with your column name.
The above query generate result as below.
Current_String | result_String
------------------------------------------------------------------------------------------------------------------------
TMB|MLE020828585|74384911WA3S|="'07300058"|74384911|0013 | TMB|MLE020828585|74384911WA3S|="'07300058"|74384911|13
Hope this will help.
Oleg was correct in utilizing INSTR(), that is how I would do it. He was missing some arguments, though. Also, I wasn't able to get ltrim to work, so I cast it to an int instead. I tested and updated my table with your data using:
UPDATE mytable
SET mycolumn = Substr(mycolumn , 1, Instr(mycolumn , '|', 1, 5))
|| CAST(Substr(mycolumn , Instr(mycolumn , '|', 1, 5) + 1) AS INT)
I have a column in my SQL Server database and it has white spaces from left and right site of the record. Basically it's a nvarchar(250) column.
I have tried removing white spaces completely like this:
UPDATE MyTable
SET whitespacecolumn = LTRIM(RTRIM(whitespacecolumn))
But this didn't work out at all, the whitespace is still there. What am I doing wrong here?
Check the below;
Find any special characters like char(10), char(13) etc in the field value.
Check the status of ANSI_PADDING ON. Refer this MSDN article.
I think replace is the way as you are looking to update
UPDATE MyTable SET whitespacecolumn = Replace(whitespacecolumn, ' ', '')
you can try doing select first and then prefer to update
SELECT *, Replace(whitespacecolumn, ' ', '') from MyTable
LTRIM, RTRIM will remove spaces in front and rear of column. In 2016 you can use TRIM function as below to trim special characters as well:
SELECT TRIM( '.,! ' FROM '# test .') AS Result;
Output:
# test