﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 7,2000 / T-SQL  / SUBSTRING And CHARINDEX to find start/end of string / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Thu, 20 Jun 2013 03:38:00 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: SUBSTRING And CHARINDEX to find start/end of string</title><link>http://www.sqlservercentral.com/Forums/Topic1366255-8-1.aspx</link><description>[quote][b]Eliza (10/7/2012)[/b][hr]Hi All,Thanks for some of the solutions, I've tried a few.SELECT SUBSTRING(@var,28+CHARINDEX ('CustomerFornm: Changed from ',@var),CHARINDEX('CustomerDOB',@var)- CHARINDEX ('CustomerFornm: Changed from ',@var)-28) AS 'Is this really what you want?'Doesn't work as it requires me to feed in a variable, @var.  My SQL has to be in a view.  I don't have a choice on the matter due to how the 3rd party software works.   CustomerDOB will not always follow, it may not even exist and there are a whole load of other 'Changed From' categories that can be present.  For example 'NI: Changed from 'Essentially it is variable all the time. The datatype is varchar and we have SQL server 2008, applologies as this may be a SQL server 2000 forum I had not realised.I have created a function that loops through each character in the string but this seems a very inefficient way of trying to achieve my result.Thanks for the help and replies so far.Eliza[/quote]This should work so long as the forname change appears only once in the string:[code="sql"]SELECT d.MyString, a.p1, b.p2, x.p1, y.p2, 	OldForename = SUBSTRING(d.MyString,a.p1,b.p2-a.p1),	NewForename = SUBSTRING(d.MyString,x.p1,y.p2-x.p1)	FROM (	SELECT MyString = 'CustomerSurnm: Changed From ''Smith'' to ''John-Smith'' CustomerFornm: Changed From ''Harry'' to ''Ben'' CustomerDOB Changed from ''12/12/1978'' to ''12/12/1979''') d -- sample table sourceCROSS APPLY (SELECT p1 = 1 + LEN('CustomerFornm: Changed From ') + CHARINDEX('CustomerFornm: Changed From',d.MyString,1)) aCROSS APPLY (SELECT p2 = CHARINDEX(' ',d.MyString,a.p1+1)) bCROSS APPLY (SELECT p1 = b.p2+1+LEN(' to ')) xCROSS APPLY (SELECT p2 = CHARINDEX(' ',d.MyString,x.p1+1)) y[/code]</description><pubDate>Sun, 07 Oct 2012 13:31:11 GMT</pubDate><dc:creator>ChrisM@Work</dc:creator></item><item><title>RE: SUBSTRING And CHARINDEX to find start/end of string</title><link>http://www.sqlservercentral.com/Forums/Topic1366255-8-1.aspx</link><description>Hi All,Thanks for some of the solutions, I've tried a few.SELECT SUBSTRING(@var,28+CHARINDEX ('CustomerFornm: Changed from ',@var),CHARINDEX('CustomerDOB',@var)- CHARINDEX ('CustomerFornm: Changed from ',@var)-28) AS 'Is this really what you want?'Doesn't work as it requires me to feed in a variable, @var.  My SQL has to be in a view.  I don't have a choice on the matter due to how the 3rd party software works.   CustomerDOB will not always follow, it may not even exist and there are a whole load of other 'Changed From' categories that can be present.  For example 'NI: Changed from 'Essentially it is variable all the time. The datatype is varchar and we have SQL server 2008, applologies as this may be a SQL server 2000 forum I had not realised.I have created a function that loops through each character in the string but this seems a very inefficient way of trying to achieve my result.Thanks for the help and replies so far.Eliza</description><pubDate>Sun, 07 Oct 2012 12:09:02 GMT</pubDate><dc:creator>Eliza</dc:creator></item><item><title>RE: SUBSTRING And CHARINDEX to find start/end of string</title><link>http://www.sqlservercentral.com/Forums/Topic1366255-8-1.aspx</link><description>[quote][b]ACinKC (10/2/2012)[/b][hr]Hmmm....these solutions would work if "CustomerFomm" is [i]always[/i] followed by "CustomerDOB", but I don't think we should take that as a constant (without input from the OP).  Once you've found the beginning of the "CustomerFomm" segment, you'd have to parse through it to find the bits you need: Between first and second tick/quote is the old data, and between third and fourth ticks/quotes is the new data.  Work through that pattern and you should be able to get your solution.[/quote]On the same line even we can not assume quote pattern in example as constant pattern ;-)Only OP can tell the requirement and pattern.</description><pubDate>Tue, 02 Oct 2012 21:57:51 GMT</pubDate><dc:creator>rhythmk</dc:creator></item><item><title>RE: SUBSTRING And CHARINDEX to find start/end of string</title><link>http://www.sqlservercentral.com/Forums/Topic1366255-8-1.aspx</link><description>[quote][b]Eliza (9/30/2012)[/b][hr]Hi all,I have a big long text string that can vary in lengh and contrain audit information about data that has changed in the database...ThanksEliza[/quote]Which version of SQL Server are you using?What datatype is the string? Is it a column value?</description><pubDate>Tue, 02 Oct 2012 08:40:10 GMT</pubDate><dc:creator>ChrisM@Work</dc:creator></item><item><title>RE: SUBSTRING And CHARINDEX to find start/end of string</title><link>http://www.sqlservercentral.com/Forums/Topic1366255-8-1.aspx</link><description>Hmmm....these solutions would work if "CustomerFomm" is [i]always[/i] followed by "CustomerDOB", but I don't think we should take that as a constant (without input from the OP).  Once you've found the beginning of the "CustomerFomm" segment, you'd have to parse through it to find the bits you need: Between first and second tick/quote is the old data, and between third and fourth ticks/quotes is the new data.  Work through that pattern and you should be able to get your solution.</description><pubDate>Tue, 02 Oct 2012 08:23:25 GMT</pubDate><dc:creator>ACinKC</dc:creator></item><item><title>RE: SUBSTRING And CHARINDEX to find start/end of string</title><link>http://www.sqlservercentral.com/Forums/Topic1366255-8-1.aspx</link><description>[quote][b]bitbucket-25253 (9/30/2012)[/b][hr]Is this what you want / need ?[code="sql"]SELECT SUBSTRING(@x,28+CHARINDEX ('CustomerFornm: Changed from ',@x),CHARINDEX('CustomerDOB',@X,1)-77) AS 'Is this really what you want?'Result:Is this really what you want?Harry to Ben [/code][/quote]Actually little correction in Ron's script.[code="sql"]SELECT SUBSTRING(@var,28+CHARINDEX ('CustomerFornm: Changed from ',@var),CHARINDEX('CustomerDOB',@var)- CHARINDEX ('CustomerFornm: Changed from ',@var)-28) AS 'Is this really what you want?'[/code]</description><pubDate>Mon, 01 Oct 2012 00:15:46 GMT</pubDate><dc:creator>rhythmk</dc:creator></item><item><title>RE: SUBSTRING And CHARINDEX to find start/end of string</title><link>http://www.sqlservercentral.com/Forums/Topic1366255-8-1.aspx</link><description>Is this what you want / need ?[code="sql"]SELECT SUBSTRING(@x,28+CHARINDEX ('CustomerFornm: Changed from ',@x),CHARINDEX('CustomerDOB',@X,1)-77) AS 'Is this really what you want?'Result:Is this really what you want?Harry to Ben [/code]</description><pubDate>Sun, 30 Sep 2012 19:41:44 GMT</pubDate><dc:creator>bitbucket-25253</dc:creator></item><item><title>SUBSTRING And CHARINDEX to find start/end of string</title><link>http://www.sqlservercentral.com/Forums/Topic1366255-8-1.aspx</link><description>Hi all,I have a big long text string that can vary in lengh and contrain audit information about data that has changed in the database.An example string would beCustomerSurnm: Changed From 'Smith' to 'John-Smith' CustomerFornm: Changed From 'Harry' to 'Ben' CustomerDOB Changed from '12/12/1978' to '12/12/1979'What I need to do is pick out the forename changes in the case I want to pull out the stringCustomerFornm: Changed From 'Harry' to 'Ben'The rest of the text disregarded.The lengh of the string will vary due to the name size however because the audit trail is generated by the computer program is will always start with CustomerFornm: Changed From Any ideas on how I can achieve this please, I have played with SUBSTRING and CHARIndex.  I can get the beginning bit working using . . . .SUBSTRING(ChangeInfo, (29+CHARINDEX ('CustomerFornm: Changed from ',ChangeInfo)),LEN(ChangeInfo))however this returns something likeCustomerFornm: Changed From 'Harry' to 'Ben' CustomerDOB Changed from '12/12/1978' to '12/12/1979'I can't figure out how to cut out the end part of the string to get my results.Any ideas?ThanksEliza</description><pubDate>Sun, 30 Sep 2012 08:27:07 GMT</pubDate><dc:creator>Eliza</dc:creator></item></channel></rss>