Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

SQL Select using parameter

3 views
Skip to first unread message

nos...@meatonconsulting.com

unread,
Sep 12, 2006, 10:29:26 AM9/12/06
to
I have a rather simple select statement that is driving me mad! Here
is what currently works:

SELECT EntiyId, Source, LDAP_DN, EntryType, Message, EventTime
FROM dbo.ONT_LDAP_LOG
WHERE (EntryType = 'Error') AND (CONVERT(varchar(10), EventTime,
101) LIKE '%8/22/2006%')

Here is what I would like to work:

SELECT EntiyId, Source, LDAP_DN, EntryType, Message, EventTime
FROM dbo.ONT_LDAP_LOG
WHERE (EntryType = 'Error') AND (CONVERT(varchar(10), EventTime,
101) LIKE '%' & @Date & '%')

Basically, I have a parameter(@Date), but I need a percent sign before
and after since my date string looks like this: 8/22/2006 11:11:34 AM
I have tried everything, i.e %@Date%, '%@Date%' ...

I am assuming it is a simple answer. Also, the convert function is in
there just as part of my attempt to get this running. If it does not
need to be in there, that is fine. I am using this in SQL 2005 with
SQL Reporting Services

Tracy McKibben

unread,
Sep 12, 2006, 10:38:47 AM9/12/06
to

Instead of converting the date, try this:

SELECT EntiyId, Source, LDAP_DN, EntryType, Message, EventTime
FROM dbo.ONT_LDAP_LOG

WHERE EntryType = 'Error'
AND DATEDIFF(day, EventTime, @Date) = 0

See
http://realsqlguy.com/serendipity/archives/5-No-Time-For-DATETIME-Values.html


--
Tracy McKibben
MCDBA
http://www.realsqlguy.com

nos...@meatonconsulting.com

unread,
Sep 12, 2006, 10:45:36 AM9/12/06
to
Thank you so much!!!! I would have never gone that route, but it makes
perfect sense! Thanks again!

nos...@meatonconsulting.com

unread,
Sep 12, 2006, 11:17:53 AM9/12/06
to
That solution works great for the example I gave, but what if I wanted
to run this:

SELECT * FROM dbo.ONT_LDAP_LOG
WHERE (Message LIKE '%sn has change from%') AND EventTime Between
@Start AND @End

I thought that was working fine, but it will only return a result for
8/31/2006 if @Start = 8/30/2006 and @End = 9/01/2006

Tracy McKibben

unread,
Sep 12, 2006, 11:27:10 AM9/12/06
to

On the page that I linked to, there is an example of using DATEDIFF and
DATEADD to "remove" the time portion of a DATETIME value. You should be
able to utilize that technique, just be sure to factor in the midnight
cutoff when determining your end date.

nos...@meatonconsulting.com

unread,
Sep 14, 2006, 4:40:32 PM9/14/06
to
Thanks for helping me along this far. I have been able to create a ton
of reports. However, I have found a new need for my original question.
I want to search for specific text that a user enters, such as:

SELECT * FROM dbo.ONT_LDAP_LOG
WHERE (Message LIKE '%@SearchString%')

This fails unless I have the hard code the search string within percent
signs, which is useless. Thanks again for all help this far!

0 new messages