Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to get the characters before and after a dot value in sql server ?

For Eg : If my string is [NTP-228].Test i want to get [NTP-228] & Test as separate values.

What I have tried:

SQL
SET @extension = SUBSTRING(@originalstring, CHARINDEX('.', @originalstring-1), 999)

SELECT @extension

bUT NOT WORKING. I want to get the string before and after.
Posted
Updated 27-Apr-16 3:45am
v2
Comments
ZurdoDev 27-Apr-16 9:35am    
You are on the right track.
PIEBALDconsult 27-Apr-16 9:47am    
Move your first closing parenthesis.
And you should always include the error messages you receive.

SQL is not good at such text manipulation; you may be better off doing that outside the database; perhaps _before_ you store the values.

1 solution

Please run this query:

SQL
declare @extension varchar(100),
		@Code varchar(500),
		@originalstring varchar(100)='[NTP-228].Test'
SET @extension = SUBSTRING(@originalstring, CHARINDEX('.', @originalstring)+1, 999)
set @Code=SUBSTRING(@originalstring,0, CHARINDEX('.', @originalstring))
select @extension,@Code


Ashish
 
Share this answer
 
v3
Comments
ZurdoDev 27-Apr-16 9:54am    
This is easier to read but the problem is I am sure the OP wants to use it in a Select statement for all rows in a table. This approach only works for 1 row at a time.
Nigam,Ashish 27-Apr-16 9:56am    
If he want to use this in a table then he can use alias in place of variables with select statement.
ranio 27-Apr-16 23:41pm    
I just want this for a single case.
Another case is if the OriginatorString could come as NTP-228 or [NTP-228]
On all occasions it must come as [NTP-228] always.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900