Question: I am writing a stored proc to Select information, i would like it to only select between dates?

**This is what it looks like now:**

```
ALTER PROCEDURE [dbo].[AutoCompleate] 
 @DateFrom datetime,
 @DateTo datetime,
 @SearchField varchar(50)
 AS

 -- V1.0 : ShaunM : 15 jun 2012
    --  AutoComplete textbox

 exec ('
 SELECT DISTINCT ' +
 @SearchField + ' FROM SchemaAudit 
             ORDER BY ' + @SearchField +' ASC')

```

I want the select to run for entry's into the database between @DateTo and DateFrom, Does any one know how to do this?

Answer: 
You would need to `CAST` the date parameters as `varchar`:

```
exec ('
     SELECT DISTINCT ' +
     @SearchField + ' FROM SchemaAudit WHERE StartDate >= "' + CAST(@DateFrom as varchar(20)) + '" AND EndDate >= "' + CAST(@DateTo as varchar(20)) + '"
                 ORDER BY ' + @SearchField +' ASC')

```
Ignoring the fact that this technique could leave you vulnerable to a SQL injection attack, you need to use `Convert` to convert the date to a varchar

```
Convert(varchar(20), DateTime, 111)

```