SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Removing '.' in SQL server table


Removing '.' in SQL server table

Author
Message
Jim1234
Jim1234
SSC-Enthusiastic
SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)

Group: General Forum Members
Points: 119 Visits: 139
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
Koen Verbeeck
Koen Verbeeck
SSC Guru
SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)

Group: General Forum Members
Points: 64320 Visits: 13298

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?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Jim1234
Jim1234
SSC-Enthusiastic
SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)

Group: General Forum Members
Points: 119 Visits: 139
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..
Luis Cazares
Luis Cazares
SSC-Forever
SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)

Group: General Forum Members
Points: 42979 Visits: 19851
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.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Jim1234
Jim1234
SSC-Enthusiastic
SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)

Group: General Forum Members
Points: 119 Visits: 139
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 //'
Luis Cazares
Luis Cazares
SSC-Forever
SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)

Group: General Forum Members
Points: 42979 Visits: 19851
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.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Tom Thomson
Tom Thomson
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26357 Visits: 12506
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

Jim1234
Jim1234
SSC-Enthusiastic
SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)

Group: General Forum Members
Points: 119 Visits: 139
Thanks Luis. It worked!!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search