patternsqlModerate
Remove string after second specific character occures from left
Viewed 0 times
afterleftcharacteroccuresremovesecondspecificfromstring
Problem
I have this type of values in table column
etc.
How can i remove everything after second @ character ? I need to display
I can do it only for all @ but not for second
returns 45 but not 45@Tra
Thank you :-)
154646@8@486
45465@6464@654etc.
How can i remove everything after second @ character ? I need to display
154646@8
45465@6464I can do it only for all @ but not for second
SELECT REPLACE(LEFT('45@Tra@lala', CHARINDEX('@','45@Tra@lala')-1),'_',' ')returns 45 but not 45@Tra
Thank you :-)
Solution
You can use the third parameter of
Result
charindex() that is used to specify where in the string the search will start.declare @S varchar(20) = '45465@6464@654';
select left(@S, charindex('@', @S, charindex('@', @S)+1)-1);Result
45465@6464Code Snippets
declare @S varchar(20) = '45465@6464@654';
select left(@S, charindex('@', @S, charindex('@', @S)+1)-1);Context
StackExchange Database Administrators Q#89456, answer score: 17
Revisions (0)
No revisions yet.