0

I have a query, I want to Trim a column and get the only Right side values:

enter image description here

Here in the pic the result is like SubAccountCode and SubaccountName but user has entered code and name in SubaccountName. I want to trim the codes from Subaccountname and update the table. T

The query I have tried is mentioned below, but I think it's not so working:

 Select Substring(Subaccountname,8,20)as Name from #temp
2
  • Welcome to StackOverflow! Could you please edit your question and add a bit more information. Could you provide us with the query you've tried already yourself. Commented Sep 19, 2018 at 7:28
  • are you trying to trim the varchar at first space ? Commented Sep 19, 2018 at 9:01

2 Answers 2

1

There are several ways to do this. When looking at your example data the easiest way would be using an replace() in the update statement.

Syntax: REPLACE ( string_expression , string_pattern , string_replacement )

Example:

UPDATE table_name
SET column2 = replace([column2], [column1], '')

What this does it updates the column2 with the value from column2 where the value from column1 is replaced with '' nothing. In your example this does leave you with an unwanted space in front. You could trim this or try as followed:

UPDATE Test
SET [SubAccountName] = replace([SubAccountName], [SubAccountCode] + ' ', '')

If the SubAccountCode could be different from the code in the SubAccountName and you only want to remove the first 8 characters (if you are sure it is always the first 8) you can use:

UPDATE YourTable SET SubAccountName = RIGHT(SubAccountName, LEN(SubAccountName) - 7)

Example script:

create table test (
    SubAccountName varchar(100),
    SubAccountCode varchar(100)
    )

insert into test (SubAccountCode, SubAccountName) VALUES
(1234567, '1234567 AUBC' ),
(1234467, '1234467 AUBC' ),
(1235567, '1235567 AUBC' )

select * from test -- Check that the data is like your example.

UPDATE Test SET SubAccountName = RIGHT(SubAccountName, LEN(SubAccountName) - 8)

select * from test -- Check that the result is like your wanted result.

drop table test -- Cleanup the test table.
Sign up to request clarification or add additional context in comments.

5 Comments

This answer is partially correct. It is working fine when subccountcode is matching in subaccountname column but when subaccouncode series is different say 'IMP178' and subaccountname is 'KGW0076 VINEET KUMAR'. This query is not updating the value correctly.
When reading your question that is exactly what you were asking. You said users entered their code + username, you want to remove that code.
I want a query in which i want to trim first seven characters from string. eg. The string is "KGW0074 VIJAY KUMAR JHA", Output should be "VIJAY KUMAR JHA" only.
Invalid length parameter passed to the RIGHT function. The statement has been terminated.
Please edit your own question with the updated information.
1

This code will work for you.

UPDATE TableName SET SubaccountName = REPLACE(LTRIM(RTRIM(SubaccountName)),LTRIM(RTRIM(SubAccountCode)),'')

4 Comments

Thank you so much for your answer. It worked for me, Can check if my below answer is correct. Update #temp set SubAccountName=Ltrim(Rtrim(Substring(Subaccountname,8,20))) from #temp
Update #temp set SubAccountName=Ltrim(Rtrim(Substring(Subaccountname,8,20))) -- From Keyword is not required
@AjayKumar do take note that if an answer worked for you, mark it so that other readers can learn what will work for this particular scenario.
This answer is partially correct. It is working fine when subccountcode is matching in subaccountname column but when subaccouncode series is different say 'IMP178' and subaccountname is 'KGW0076 VINEET KUMAR'. This query is not updating the value correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.