//  home   //  advanced search   //  news   //  categories   //  sql build chart   //  downloads   //  statistics
 ASP FAQ 
Home
ASP FAQ Tutorials

   8000XXXX Errors
   ASP.NET 2.0
   Classic ASP 1.0
   Databases
      Access DB & ADO
      General SQL Server & Access Articles
      MySQL
      Other Articles
      Schema Tutorials
      Sql Server 2000
      Sql Server 2005
   General Concepts
   Search Engine Optimization (SEO)

Contact Us
Site Map

Search

Web
aspfaq.com
tutorials.aspfaq.com
sqlserver2000.databases.aspfaq.com

ASP FAQ Tutorials :: Databases :: Sql Server 2000 :: Why can't I use LIKE '%datepart%' queries?


Why can't I use LIKE '%datepart%' queries?

Several people want to offer the ability for users to enter any portion of the date (e.g. only the month, or only the year), and leave wildcards for the rest. So, for all rows in 2002, they would do something like this: 
 
SELECT columns FROM table WHERE dateColumn LIKE '%/2002 %'
 
Unfortunately, SQL Server does not internally store dates in the character format you see them represented as in client tools like Enterprise Manager or Query Analyzer. 
 
While for some odd reason Books Online recommends LIKE queries against datetime values, date range searches are much more effective because they don't rely on specific formatting of dates, regional settings on a server, etc. They will also have the ability to use an index, if it exists. Here is the above query in a much more efficient and logical format: 
 
SELECT columns FROM table WHERE 
    dateColumn >= '20020101' 
    AND dateColumn < '20030101'
 
Another interesting approach, albeit not as efficient as the above, is to use the shorthand datepart functions in SQL Server to match one or more criteria passed in. The following stored procedure will work for any combination of day, month and year: 
 
CREATE PROCEDURE dbo.ReturnRowsForAnyPartOfDate 
    @year INT = 0, 
    @month INT = 0, 
    @day INT = 0 
AS 
BEGIN 
    SELECT columns FROM table 
        WHERE 
        YEAR(dateColumn) = CASE 
            WHEN @year > 0 THEN 
                @year 
            ELSE 
                YEAR(dateColumn) 
            END 
        AND 
        MONTH(dateColumn) = CASE 
            WHEN @month > 0 THEN 
                @month 
            ELSE      
                MONTH(dateColumn) 
            END 
        AND 
        DAY(dateColumn) = CASE 
            WHEN @day > 0 THEN 
                @day 
            ELSE 
                DAY(dateColumn) 
            END 
END
 
And you could call this from ASP as follows: 
 
<% 
    if request.form("y") <> "" then 
        y = clng(request.form("y")) 
        m = clng(request.form("m")) 
        d = clng(request.form("d")) 
    end if 
%> 
 
<form method=post name=dateSearch> 
 
    Year: 
     
    <select name=y> 
    <option value=0>Any 
    <% 
        for i = 2002 to 2000 step -1 
            response.write "<option value=" & i 
            if i = y then response.write " SELECTED" 
            response.write ">" & i 
        next 
    %> 
    </select> 
 
    Month: 
     
    <select name=m> 
    <option value=0>Any 
    <% 
        for i = 1 to 12 
            response.write "<option value=" & i 
            if i = m then response.write " SELECTED" 
            response.write ">" & monthname(i) 
        next 
    %> 
    </select> 
 
    Day: 
 
    <select name=d> 
    <option value=0>Any 
    <% 
        for i = 1 to 31 
            response.write "<option value=" & i 
            if i = d then response.write " SELECTED" 
            response.write ">" & i 
        next 
    %> 
    </select> 
 
    <input type=submit> 
 
</form> 
 
<hr size=1> 
 
<% 
    if request.form("y") <> "" then 
        set Conn = CreateObject("ADODB.Connection") 
        Conn.open "<connection_string>" 
        sql = "EXEC dbo.ReturnRowsForAnyPartOfDate " & _ 
            y & ", " & m & ", " & d 
        set rs = conn.execute(sql) 
        if not rs.eof then 
            do while not rs.eof 
                response.write rs(0) & "<br>" 
                rs.movenext 
            loop 
        else 
            response.write "No records." 
        end if 
        rs.close: set rs = nothing 
        conn.close: set conn = nothing 
    end if 
%>

Related Articles

Are there tools available for auditing changes to SQL Server data?
Can I create an index on a BIT column?
Can I have optional parameters to my stored procedures?
Can I implement an input mask in SQL Server?
Can I make SQL Server format dates and times for me?
Can I start IDENTITY values at a new seed?
Can SQL Server tell me which row was inserted most recently?
How can I learn more about undocumented SQL Server stored procedures?
How can I make my SQL queries case sensitive?
How do I audit changes to SQL Server data?
How do I connect to a non-default instance of SQL Server?
How do I connect to SQL Server on a port other than 1433?
How do I create a cross-tab (or "pivot") query?
How do I determine if a table exists in a SQL Server database?
How do I drop a SQL Server database?
How do I find all the available SQL Servers on my network?
How do I get a list of SQL Server tables and their row counts?
How do I get rid of Named Pipes / DBNMPNTW errors?
How do I get the correct date/time from the msdb.sysjob* tables?
How do I get the nth row in a SQL Server table?
How do I get the result of dynamic SQL into a variable?
How do I handle REPLACE() within an NTEXT column in SQL Server?
How do I hide system tables in SQL Server's Enterprise Manager?
How do I know which version of SQL Server I'm running?
How do I limit the number of rows returned in my resultset?
How do I load text or csv file data into SQL Server?
How do I manage changes in SQL Server objects?
How do I monitor SQL Server performance?
How do I prevent linked server errors?
How do I reclaim space in SQL Server?
How do I recover data from SQL Server's log files?
How do I search for special characters (e.g. %) in SQL Server?
How do I start SQL Server Agent from ASP?
How do I time my T-SQL code?
How do I upsize from Access to SQL Server?
How do I upsize my MSDE database to full-blown SQL Server 2000?
How do I use a variable in a TOP clause in SQL Server?
How do I use GETDATE() within a User-Defined Function (UDF)?
How should I store an IP address in SQL Server?
Schema: How do I find all the foreign keys in a database?
SQL Server & MSDE
What are reserved Access, ODBC and SQL Server keywords?
What are the capacities of Access, SQL Server, and MSDE?
What are the main differences between Access and SQL Server?
What do I need to know about SQL Server 2000 SP4?
Where else can I learn about SQL Server?
Where is SP4 for SQL Server 2000?
Why am I having problems with SQL Server 2000 SP3 / SP3a?
Why can't I install SQL Server on Windows Server 2003?
Why can't I install SQL Server on Windows XP?
Why do I get "Login failed for user '\'."?
Why do I get 'object could not be found' or 'invalid object name'?
Why do I get errors about master..spt_values?
Why do I get script errors in Enterprise Manager's 'taskpad' view?
Why do I get SQLSetConnectAttr Failed errors?
Why do I have problems with views after altering the base table?
Why does EM crash when I get an error in a stored procedure?
Why does Enterprise Manager return 'Invalid cursor state'?
Why does my DELETE query not work?
Why does sp_spaceused return inaccurate values?
Why is Enterprise Manager slow at expanding my database list?
Why is my app slow after upgrading from SQL Server 7 to 2000?
Why is tempdb full, and how can I prevent this from happening?
Why should I consider using an auxiliary calendar table?
Why should I consider using an auxiliary numbers table?

 

 


Created: 7/1/2002 | Last Updated: 3/24/2005 | broken links | helpful | not helpful | statistics
© Copyright 2006, UBR, Inc. All Rights Reserved. (130)

 

Copyright 1999-2006, All rights reserved.
Finding content
Finding content.  An error has occured...