T-SQL is notoriously slow for these types of operations. Why don't you write .NET CLR user defined function to parse the string?
Main Topics
Browse All TopicsI have a column (notes) that is a string. This field is periodically updated and more information is appended to it. (Not my design). What I need to do is be able to look for the last date that is put into the string and then compare that date to see if it is between two other dates.
For the purposes of this question we can assume 1 table - table1 with 1 column - Notes.
A sample piece of data for the notes column would be...
blurb on 03/23/2004 by Someone blurb on 03/23/2004 by Someone blurb on 09/27/2004 by Someone Blurb on 09/27/2004 by Someone
Of course, I took the actual data out and replaced it with blurb to protect the innocent.
Here's the query...
DECLARE @StartDate datetime
DECLARE @EndDate datetime
SET @StartDate = convert(datetime, '10/01/2006',101)
SET @EndDate = convert(datetime, '06/30/2007',101)
select convert(datetime,substring
from table1
where notes <> ''
and substring(notes, CHARINDEX (' on', notes, len(notes)-25)+4, 1) in ('0','1')
and convert(datetime,substring
BETWEEN @StartDate AND @EndDate
I get...
Msg 241, Level 16, State 1, Line 8
Conversion failed when converting datetime from character string.
If I remove the between line...
DECLARE @StartDate datetime
DECLARE @EndDate datetime
SET @StartDate = convert(datetime, '10/01/2006',101)
SET @EndDate = convert(datetime, '06/30/2007',101)
select convert(datetime,substring
from table1
where notes <> ''
and substring(notes, CHARINDEX (' on', notes, len(notes)-25)+4, 1) in ('0','1')
I get...
2004-09-27 00:00:00.000
For the one row example I gave above...
So, why can't I do that "between" statement? Also, the formatting doesn't work on the convert but I don't really care. (unless that's the problem)
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.
scott, let me try it and I'll get back with you.
ted, I'm a dba\network admin so my plate is kind of full. I'm plodding through a programming manual now but not enough time for this. My DB is relatively small so it returns pretty quick right now.
also, the and substring(notes, CHARINDEX (' on', notes, len(notes)-25)+4, 1) in ('0','1')
was just a way to get rid of nulls and results with character since this the date is always the only number in the field and begins with 0 or 1. Not eloquent but it works.
Thanks!
I'll let you know how it goes..
Business Accounts
Answer for Membership
by: ScottPletcherPosted on 2007-08-24 at 14:48:32ID: 19765724
Hmm, I think that is the first date not the last (?). Please try this:
][0-9]/2[0 12][0-9][0 -9]%', notes) > 0 --verify that a date pattern is present ]2/[0-9][0 123]/[0-9] [01]%', reverse(notes)) + 8), 10), 101)
]2/[0-9][0 123]/[0-9] [01]%', reverse(@notes)) + 8), 10), 101) ][0-9]/2[0 12][0-9][0 -9]%', @notes) > 0 ]2/[0-9][0 123]/[0-9] [01]%', reverse(@notes)) + 8), 10), 101) between @startdate and @enddate
SELECT ...
WHERE patindex('%[01][0-9]/[0123
/* not sure what this does, it may still be needed
and substring(notes, CHARINDEX (' on', notes, len(notes)-25)+4, 1) in ('0','1')
*/
-- find the *last* date pattern, convert it to a date, ...
AND convert(datetime, substring(notes, len(notes) - (patindex('%[0-9][0-9][012
-- ... and compare it to the requested start and end dates
BETWEEN @StartDate AND @EndDate
A stand-alone example:
DECLARE @StartDate datetime
DECLARE @EndDate datetime
DECLARE @notes varchar(200)
SET @StartDate = convert(datetime, '10/01/2006',101)
SET @EndDate = convert(datetime, '06/30/2007',101)
set @notes = 'blurb on 03/23/2004 by Someone blurb on 03/23/2004 by Someone blurb on 09/27/2004 by Someone Blurb on 09/27/2004 by Someone'
select convert(datetime, substring(@notes, len(@notes) - (patindex('%[0-9][0-9][012
where patindex('%[01][0-9]/[0123
--and convert(datetime, substring(@notes, len(@notes) - (patindex('%[0-9][0-9][012