February 17, 2009 at 2:56 am
Hi,
I have a column named code in a table called data with the following sample information:
1
1.1
1.1.1
1.1.10
1.1.11
1.1.12
I need a query that would remove all information after the last '.'
My result table should read:
1
1
1.1
1.1
1.1
1.1
Thanks
February 17, 2009 at 3:09 am
Try this
SELECT SUBSTRING(s, 1, LEN(s) - CHARINDEX('.', REVERSE(s)))
FROM
(
SELECT '1' as s
UNION ALL SELECT '1.1'
UNION ALL SELECT '1.1.1'
UNION ALL SELECT '1.1.10'
UNION ALL SELECT '1.1.11'
UNION ALL SELECT '1.1.12'
) AS TmpTable
Regards,
Nitin
February 17, 2009 at 3:37 am
Thanks for the reply,
Problem is that I have hundreds of rows of data and I cant type every row in.Also the rows aren't only 1.1, there is also 1.2, 1.2.1, 1.3, 1.3.1, 1.3.2 etc.
Thanks again
February 17, 2009 at 3:52 am
🙂
Inner table(with UNION ALL) is for example only. You can use your table inplace of TmpTable..
something like this
Select SELECT SUBSTRING(yourColumnName, 1, LEN(yourColumnName) - CHARINDEX('.', REVERSE(yourColumnName)))
FROM yourTableName
Regards,
Nitin
February 17, 2009 at 3:54 am
Is there a maximum number of dots?
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
February 17, 2009 at 4:04 am
Thanks Alot for the help,works like a dream.
Apologies for the misunderstanding,
Thank you :hehe:
February 17, 2009 at 8:25 am
-- If the number of dots is never greater than 3, then this will work:
DECLARE @String varchar(20)
SET @String = '123.45.678.90'
SELECT REPLACE(@String+'~', PARSENAME(@String+'~',1), '')
Result:
123.45.678.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply