Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Removing '.' in SQL server table Expand / Collapse
Author
Message
Posted Friday, October 18, 2013 12:53 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, February 10, 2014 6:04 AM
Points: 52, Visits: 123
Hi All,

We have a table say TableA. It has a column 'Detail' which is varchar(250). I has some values with '.' in end and some without '.' Example (Row 1 : 'ADB', Row2 : 'ADC. DCD.' Row3 : 'ADC.')

We need to remove the '.' which is occurring only in the last of 'Detail' column and load it. Example after removing '.' (Row 1: 'ADB', Row2 : 'ADC. DCD', Row3 : 'ADC')

Database is SQL Server 2008 R2 and we are inserting into the TableA using a INSERT INTO 'SELECT' query..

Please suggest.

Thanks
Jim



Post #1505995
Posted Friday, October 18, 2013 1:44 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Sunday, July 20, 2014 11:20 PM
Points: 13,252, Visits: 10,133
DECLARE @myRow VARCHAR(10) = 'ADC. DCD.';

SELECT ISNULL(REVERSE(STUFF(REVERSE(@myRow),CHARINDEX('.',REVERSE(@myRow),1),1,'')),@myRow);

ps: please read the first link in my signature on how to post questions (table DDL, sample data, desired output). It will help you to get correct answers more quicker.




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1506009
Posted Friday, October 18, 2013 8:36 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, February 10, 2014 6:04 AM
Points: 52, Visits: 123
Thanks Koen!! This is perfect. But now there is a small change, there should not be any special character at the end. Could you please suggest..
Post #1506195
Posted Friday, October 18, 2013 9:02 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 1:27 PM
Points: 3,318, Visits: 7,164
Maybe something like this would help.
WITH SampleData(String) AS(
SELECT 'ADB' UNION ALL SELECT 'ADC. DCD.' UNION ALL SELECT 'ADC.'
)
SELECT CASE WHEN RIGHT( String, 1) LIKE '[^A-Za-z0-9]'
THEN LEFT( String, LEN( String) - 1)
ELSE String END
FROM SampleData

Do you have more than one special character at the end? Could you give some sample data?



Luis C.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1506208
Posted Friday, October 18, 2013 10:10 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, February 10, 2014 6:04 AM
Points: 52, Visits: 123
Hi Luis,

I think this is not working properly when the column is ending with space ' '.

Sample data

'ABC DS.'
'AD.'
'DG@'
'DGC DS '
'fdsf fs/'
'fs fds //'
Post #1506255
Posted Friday, October 18, 2013 10:26 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 1:27 PM
Points: 3,318, Visits: 7,164
Here's a modification.
WITH SampleData(String) AS(
SELECT 'ADB' UNION ALL SELECT
'ADC. DCD.' UNION ALL SELECT
'ADC.' UNION ALL SELECT
'ABC DS.' UNION ALL SELECT
'AD.' UNION ALL SELECT
'DG@' UNION ALL SELECT
'DGC DS ' UNION ALL SELECT
'fdsf fs/' UNION ALL SELECT
'fs fds //'
)
SELECT CASE WHEN RIGHT( RTRIM(String), 1) LIKE '[^A-Za-z0-9]'
THEN LEFT( String, LEN( String) - PATINDEX('%[A-Za-z0-9]%', REVERSE(String)) + 1)
ELSE String END
FROM SampleData




Luis C.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1506262
Posted Saturday, October 19, 2013 5:42 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 10:00 AM
Points: 8,551, Visits: 9,043
If the requirement is to remove trailing spaces and all trailing non-alpha-numerics Luis code is what's needed; but it seems a bit heavyweight if the requirement is just to eliminate trailing spaces and at most one '.'. If the requirement is just spaces and at most one '.' this simpler code will do instead:
FROM SampleData;
WITH SampleData(String) AS(
SELECT 'ADB' UNION ALL SELECT
'ADC. DCD.' UNION ALL SELECT
'ADC.' UNION ALL SELECT
'ABC DS.' UNION ALL SELECT
'AD.' UNION ALL SELECT
'DG@' UNION ALL SELECT
'DGC DS ' UNION ALL SELECT
'fdsf fs/' UNION ALL SELECT
'fs fds //'
)select CASE WHEN right( rtrim(String), 1) ='.'
THEN rtrim(substring(String,1,len(String)-1))
ELSE rtrim(String) END as NewString
FROM SampleData;



Tom
Post #1506386
Posted Wednesday, October 23, 2013 8:59 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, February 10, 2014 6:04 AM
Points: 52, Visits: 123
Thanks Luis. It worked!!
Post #1507683
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse