Sorry if i put quotes around the numbers i get rid of the error, however it still doesn't work.It Brings back a null Value.
Main Topics
Browse All TopicsHi There,
I had asked a question related to something like this before but never got to the bottom of it. I am hoping someone can help me this time.
I have a text field with in this field there id information on when a bill date was chaged 'from what' , 'to what'
I want to be able to do a substring on this field to pull out the data needed.
At the moment i am doing the folliowing:
SELECT *,SUBSTRING(history_text,2
from customer_history where customer_id = 24829
AND history_text like '%<b>BILLING INFO CHANGE</b>%'
AND history_text like '%Bill Day%'
The text is automatically entered into the database the one problem i am having is if i want to just bring back the date and not the text around it then i want just 2 charcters back, however if the date is from the 1st to the 9th this is throwing the formula out.
For example here are two text fields:
Create table customer_history
(history_id int not null,
customer_id int not null,
date_entered datetime not null,
history_text text not null)
Insert into customer_history values (1004963,24829, '2007-06-07 15:48:42.000','<b>BILLING INFO CHANGE</b><br><table border=1 cellpadding=0 cellspacing=0><tr><td height=3></td></tr><tr><td
Insert into customer_history values (1002583,12345,'2007-06-07
With my syntax i am getting back with on the correct results ie(25) but with the other i am getting back (&n).
can anyone tell me what why to write the syntax to retrieve only the 25 and the 7?
All help appriciated,
Kind Regards,
Elaine
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Let's see:
SUBSTRING(string, start, length) means that you will get whatever is at start position in the string (starting with the "start-th" character) having a length of 2, in your case the 298th character and the subsequent one (length of 2)
but if the string is shifted with only 1 position to the right ro to the left, if the string doesn't always have the exact same length and structure (date starting at exact same position) you will not get the expected result.
For this very reason in the example above you got from substring the "e6" value (it says in the error(, which you try to compare to 1 abd 9. You cannot compare a string, "e6" to numbers hence the error.
You need first to look for a sequence in the string that preceeds your days and is always the same an only than apply the substring. That is you need to parse the string.
A pattern in your string seems to be that the day always occurs after the "Bill Day " sequence and between the start of this sequence to the day value are always the same characters, 29 of them so insead of tf
SUBSTRING(history_text,298
you shoul try:
SELECT substring(history_text, patindex('%Bill Day %' ,history_text)+29, 2)
one more thing, the day number will not always be 2 digits so the 2 parameter is not really goo because if the date is lets say 7 yo'll get error because you'll get bek 7&. We need to parse that day from the position it starts till the & character after it. so this will work:
SELECT substring(history_text,pat
Declare @SubStringSearch VarChar(200)
Set @SubStringSearch = 'Bill Day </td><td> '
Select substring(
substring(history_text,
(charindex(@SubStringSearc
,5)
,0,charindex('&',
substring(history_text,
(charindex(@SubStringSearc
,5)))
FROM customer_history
Also, what happens if i want to retreive the From Days?
I am trying to understand the syntax first to play around but if i want to get 17 and 4 from the histoey_text inserts how do i do this?
I'm sorry i should of asked for this at the start not just the 25 and 7.
By using the same synatx do i just need to change the substringsearch, the problem there is that the first number is included in that and that will be differnt on ever row?
Hi Zberteoc,
I have tried to run your code also, the first one works but as you say it will bring back some unwanted data types, however when i run the second example it brings back an error:
Argument data type text is invalid for argument 1 of len function.
The history_text is of datatype Text, and i tried changing the len to datalength but it still returned an error,sorry about all these qustions, i am only startig out with SQL about 6 months now!
Thanks for you help so far.
Elaine.
No unfortunitly not.
Only in the history table. This goes for a lot of changes such as product price changes or sales reps changes also.
I know this is not the best way of managing the info but i am pulling the information through replication into my data warehouse from a live database created by someone esle, and i tried as best as possiable to follow their table lay out.
Thanks for you help so far, i really appriciate this.
Elaine.
<<Hi Zberteoc,
I have tried to run your code also, the first one works but as you say it will bring back some unwanted data types, however when i run the second example it brings back an error:
Argument data type text is invalid for argument 1 of len function.
>>
Replace len(history_text) with some big number like 10000.
Declare @SubStringSearch VarChar(200)
Declare @SecondaryStringSearch VarChar(200)
Set @SubStringSearch = 'Bill Day </td><td> '
Set @SecondaryStringSearch = ' </td><td> '
Select history_id,customer_id, date_entered, substring(
substring(history_text,
(charindex(@SubStringSearc
,5)
,0,charindex('&',
substring(history_text,
(charindex(@SubStringSearc
,5))),
substring(substring(
substring(history_text,
(charindex(@SubStringSearc
,Len(cast(history_text as VarChar(max)))
),charindex(@SecondaryStri
, substring(history_text,
(charindex(@SubStringSearc
,Len(cast(history_text as VarChar(max))))) + len(@SecondaryStringSearch
),0,
charIndex('&',substring(
substring(history_text,
(charindex(@SubStringSearc
,Len(cast(history_text as VarChar(max)))
),charindex(@SecondaryStri
, substring(history_text,
(charindex(@SubStringSearc
,Len(cast(history_text as VarChar(max))))) + len(@SecondaryStringSearch
)))
FROM customer_history
Sorry so you can see the breaks I added the "AS" to give you the column names. Its messy but it should work regardless of how large the numeric values are.
Declare @SubStringSearch VarChar(200)
Declare @SecondaryStringSearch VarChar(200)
Set @SubStringSearch = 'Bill Day </td><td> '
Set @SecondaryStringSearch = ' </td><td> '
Select history_id,customer_id, date_entered, substring(
substring(history_text,
(charindex(@SubStringSearc
,5)
,0,charindex('&',
substring(history_text,
(charindex(@SubStringSearc
,5))) AS TODATE,
substring(substring(
substring(history_text,
(charindex(@SubStringSearc
,Len(cast(history_text as VarChar(max)))
),charindex(@SecondaryStri
, substring(history_text,
(charindex(@SubStringSearc
,Len(cast(history_text as VarChar(max))))) + len(@SecondaryStringSearch
),0, charIndex('&',substring(
substring(history_text,
(charindex(@SubStringSearc
,Len(cast(history_text as VarChar(max)))
),charindex(@SecondaryStri
, substring(history_text,
(charindex(@SubStringSearc
,Len(cast(history_text as VarChar(max))))) + len(@SecondaryStringSearch
))) AS FROMDATE
FROM customer_history
Please try this:
SELECT *, REPLACE(SUBSTRING(
SUBSTRING(history_text, CHARINDEX('Bill Day', history_text), 100),
PATINDEX('%[123456789]%',
SUBSTRING(history_text, CHARINDEX('Bill Day', history_text), 100)),
2), '&', '') AS [BillDay]
from customer_history
where customer_id = 24829
AND history_text like '%<b>BILLING INFO CHANGE</b>%'
AND history_text like '%Bill Day%'
For example:
SELECT customer_id, REPLACE(SUBSTRING(
SUBSTRING(history_text, CHARINDEX('Bill Day', history_text), 100),
PATINDEX('%[123456789]%',
SUBSTRING(history_text, CHARINDEX('Bill Day', history_text), 100)),
2), '&', '') AS [BillDay],
history_text
from (
select 24829 as customer_id, '<b>BILLING INFO CHANGE</b><br><table border=1 cellpadding=0 cellspacing=0><tr><td height=3></td></tr><tr><td
union all
select 24829, '<b>BILLING INFO CHANGE</b><br><table border=1 cellpadding=0 cellspacing=0><tr><td height=3></td></tr><tr><td
) AS customer_history
where customer_id = 24829
AND history_text like '%<b>BILLING INFO CHANGE</b>%'
AND history_text like '%Bill Day%'
Ok, Elaine, this should work:
SELECT
replace( -- replace & with '' (empty string) if one digit day
substring
(
history_text, -- this is the text
patindex('%Bill Day %',history_text)+
2 -- this is the length
), '&','') AS 'Day'
FROM
#customer_history
I used it with the CREATE/INSERT examples you gave in your question and I got the good results: 25 and 7
Thank you all for your help, i am going to use messen1975 answer as my solution as he was able to show me the From and To dates,however i am unsure what to do about allocation the points Do you all agree if i choose the ' accept muliple solutions' Or because i am usng messen1975's answer to allocate all the points to hm?
I don't want to offend anybody this is why i am asking this.
You have all been brilliant with your answers and times in responce.
Elaine.
Putoch, I am not selfish, but I gave you the explanations how and why you have to do plus the starting ideea, for you and all the subsequent answerers, of how to parse the text, not to mention that my previous post gave you a nice and simple solution that works.
I am only concern about my time I spent on this message. messen1975 was a good assist I would say.
Business Accounts
Answer for Membership
by: PutochPosted on 2007-08-29 at 04:15:21ID: 19790066
I have also tried the following: ,2) between 10 and 31 then SUBSTRING(history_text,298 ,2) ,2) between 1 and 9 then SUBSTRING(history_text,297 ,1) ENd
select *,
case when SUBSTRING(history_text,298
when SUBSTRING(history_text,298
from customer_history
but get this error: ''Conversion failed when converting the varchar value 'e6' to data type int.''
I have been also trying to use patindex, but i can not get it to work correctly.
Any help greatly appriciated
thanks,
Elaine