Advertisement

08.24.2007 at 02:23PM PDT, ID: 22786066
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.0

SQL Server 2005 Query with convert string to date and test for whether it's between two dates.

Asked by jonnietexas in SQL Server 2005, Databases Miscellaneous, MS SQL Server

Tags: , , , ,

I 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(notes, CHARINDEX (' on', notes, len(notes)-25)+4, 10),101)
from table1
where notes <> ''
and substring(notes, CHARINDEX (' on', notes, len(notes)-25)+4, 1) in ('0','1')
and convert(datetime,substring(notes, CHARINDEX (' on', notes, len(notes)-25)+4, 10),101)
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(notes, CHARINDEX (' on', notes, len(notes)-25)+4, 10),101)
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)Start Free Trial
[+][-]08.24.2007 at 02:48PM PDT, ID: 19765724

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: SQL Server 2005, Databases Miscellaneous, MS SQL Server
Tags: sql, convert, server, date, string
Sign Up Now!
Solution Provided By: ScottPletcher
Participating Experts: 2
Solution Grade: A
 
 
[+][-]08.25.2007 at 11:41AM PDT, ID: 19768667

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.27.2007 at 09:56AM PDT, ID: 19776692

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08.27.2007 at 10:09AM PDT, ID: 19776802

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20081112-EE-VQP-42 / EE_QW_1_20070628