Home All Groups Group Topic Archive Search About

Dynamic SQL with Datetime variables

Author
16 May 2005 12:11 PM
Larry Menzin
I am having difficulty using dynamic SQL with datetime variables. A code
snippet like this gives me an error about converting a string to datetime:

....
WHERE cat LIKE ' + @Product + ' AND Week IS NOT NULL
AND RegionID LIKE ' + @Region + ' AND chnl_cd LIKE ' + @Channel + '
AND clutr_nm LIKE ' + @Market + '
AND Convert(datetime, Week) >= ' + @startdate + '  '

@startdate is the datetime variable.  Using '' + @startdate + '' prints the
literal variable @startdate, while using ''' + @startdate + ''' gives me the
same conversion error. How do I solve this?

--
Larry Menzin
American Techsystems Corp.

Author
16 May 2005 12:18 PM
Chandra
The string concatination looks correct

probable you are looking for:

WHERE cat LIKE ' + @Product + ' AND Week IS NOT NULL
AND RegionID LIKE ' + @Region + ' AND chnl_cd LIKE ' + @Channel + '
AND clutr_nm LIKE ' + @Market + '
AND Week >= ' + datepart(w,@startdate)

is this the one you are looking for? what should be the result of the query

--
best Regards,
Chandra
http://chanduas.blogspot.com/
http://groups.msn.com/SQLResource/
---------------------------------------



Show quote
"Larry Menzin" wrote:

> I am having difficulty using dynamic SQL with datetime variables. A code
> snippet like this gives me an error about converting a string to datetime:
>
> ...
> WHERE cat LIKE ' + @Product + ' AND Week IS NOT NULL
> AND RegionID LIKE ' + @Region + ' AND chnl_cd LIKE ' + @Channel + '
> AND clutr_nm LIKE ' + @Market + '
> AND Convert(datetime, Week) >= ' + @startdate + '  '
>
> @startdate is the datetime variable.  Using '' + @startdate + '' prints the
> literal variable @startdate, while using ''' + @startdate + ''' gives me the
> same conversion error. How do I solve this?
>
> --
> Larry Menzin
> American Techsystems Corp.
Author
16 May 2005 12:21 PM
Chandra
Correction:
datepart(ww,@startdate)


--
best Regards,
Chandra
http://chanduas.blogspot.com/
http://groups.msn.com/SQLResource/
---------------------------------------



Show quote
"Chandra" wrote:

> The string concatination looks correct
>
> probable you are looking for:
>
> WHERE cat LIKE ' + @Product + ' AND Week IS NOT NULL
> AND RegionID LIKE ' + @Region + ' AND chnl_cd LIKE ' + @Channel + '
> AND clutr_nm LIKE ' + @Market + '
> AND Week >= ' + datepart(w,@startdate)
>
> is this the one you are looking for? what should be the result of the query
>
> --
> best Regards,
> Chandra
> http://chanduas.blogspot.com/
> http://groups.msn.com/SQLResource/
> ---------------------------------------
>
>
>
> "Larry Menzin" wrote:
>
> > I am having difficulty using dynamic SQL with datetime variables. A code
> > snippet like this gives me an error about converting a string to datetime:
> >
> > ...
> > WHERE cat LIKE ' + @Product + ' AND Week IS NOT NULL
> > AND RegionID LIKE ' + @Region + ' AND chnl_cd LIKE ' + @Channel + '
> > AND clutr_nm LIKE ' + @Market + '
> > AND Convert(datetime, Week) >= ' + @startdate + '  '
> >
> > @startdate is the datetime variable.  Using '' + @startdate + '' prints the
> > literal variable @startdate, while using ''' + @startdate + ''' gives me the
> > same conversion error. How do I solve this?
> >
> > --
> > Larry Menzin
> > American Techsystems Corp.
Author
16 May 2005 12:52 PM
Larry Menzin
Doesn't work.

When using dynamic SQL, '+@variable+' is used for numbers, '''+@variable+'''
is used for strings, but what syntax is used for dates? I can't seem to find
anything that works.


--
Larry Menzin
American Techsystems Corp.


Show quote
"Chandra" wrote:

> The string concatination looks correct
>
> probable you are looking for:
>
> WHERE cat LIKE ' + @Product + ' AND Week IS NOT NULL
> AND RegionID LIKE ' + @Region + ' AND chnl_cd LIKE ' + @Channel + '
> AND clutr_nm LIKE ' + @Market + '
> AND Week >= ' + datepart(w,@startdate)
>
> is this the one you are looking for? what should be the result of the query
>
> --
> best Regards,
> Chandra
> http://chanduas.blogspot.com/
> http://groups.msn.com/SQLResource/
> ---------------------------------------
>
>
>
> "Larry Menzin" wrote:
>
> > I am having difficulty using dynamic SQL with datetime variables. A code
> > snippet like this gives me an error about converting a string to datetime:
> >
> > ...
> > WHERE cat LIKE ' + @Product + ' AND Week IS NOT NULL
> > AND RegionID LIKE ' + @Region + ' AND chnl_cd LIKE ' + @Channel + '
> > AND clutr_nm LIKE ' + @Market + '
> > AND Convert(datetime, Week) >= ' + @startdate + '  '
> >
> > @startdate is the datetime variable.  Using '' + @startdate + '' prints the
> > literal variable @startdate, while using ''' + @startdate + ''' gives me the
> > same conversion error. How do I solve this?
> >
> > --
> > Larry Menzin
> > American Techsystems Corp.
Author
16 May 2005 12:58 PM
Jens Süßmeyer
Like mentioned in my previous post, the "Date" you  are pasting in is in
some numeric format, so that SQL Server implicitly converts it to a number,
if you wanna add a number to a string (which can´t be converted to a number)
the query parser will punish you with an error.

HTH, Jens Suessmeyer.

---
http://www.sqlserver2005.de
---
Author
16 May 2005 1:02 PM
Alejandro Mesa
Larry,

> > > AND Convert(datetime, Week) >= ' + @startdate + '  '

What data type is column [Week]?

Datetime values should be concatenated as characters values.

Example:

use northwind
go

declare @i int
declare @sql nvarchar(4000)
declare @sd datetime
declare @ed datetime

set @sd = '19970101'
set @ed = '19971231'

set @sql = N'select @i = count(*) from dbo.orders where orderdate > ''' +
convert(char(8), @sd, 112) + N''' and orderdate < ''' + convert(char(8),
dateadd(day, 1, @ed), 112) + N''''

print @sql

exec sp_executesql @sql, N'@i int output', @i output

print @i
go


AMB

exec

Show quote
"Larry Menzin" wrote:

> Doesn't work.
>
> When using dynamic SQL, '+@variable+' is used for numbers, '''+@variable+'''
> is used for strings, but what syntax is used for dates? I can't seem to find
> anything that works.
>
>
> --
> Larry Menzin
> American Techsystems Corp.
>
>
> "Chandra" wrote:
>
> > The string concatination looks correct
> >
> > probable you are looking for:
> >
> > WHERE cat LIKE ' + @Product + ' AND Week IS NOT NULL
> > AND RegionID LIKE ' + @Region + ' AND chnl_cd LIKE ' + @Channel + '
> > AND clutr_nm LIKE ' + @Market + '
> > AND Week >= ' + datepart(w,@startdate)
> >
> > is this the one you are looking for? what should be the result of the query
> >
> > --
> > best Regards,
> > Chandra
> > http://chanduas.blogspot.com/
> > http://groups.msn.com/SQLResource/
> > ---------------------------------------
> >
> >
> >
> > "Larry Menzin" wrote:
> >
> > > I am having difficulty using dynamic SQL with datetime variables. A code
> > > snippet like this gives me an error about converting a string to datetime:
> > >
> > > ...
> > > WHERE cat LIKE ' + @Product + ' AND Week IS NOT NULL
> > > AND RegionID LIKE ' + @Region + ' AND chnl_cd LIKE ' + @Channel + '
> > > AND clutr_nm LIKE ' + @Market + '
> > > AND Convert(datetime, Week) >= ' + @startdate + '  '
> > >
> > > @startdate is the datetime variable.  Using '' + @startdate + '' prints the
> > > literal variable @startdate, while using ''' + @startdate + ''' gives me the
> > > same conversion error. How do I solve this?
> > >
> > > --
> > > Larry Menzin
> > > American Techsystems Corp.
Author
16 May 2005 1:20 PM
Larry Menzin
Ok. I got it to work by converting datetime variables to character strings
and then comparing strings. Dates apparently cannot be used directly in
dynamic SQL. What is the performance hit from all these date to string
conversions?

--
Larry Menzin
American Techsystems Corp.


Show quote
"Alejandro Mesa" wrote:

> Larry,
>
> > > > AND Convert(datetime, Week) >= ' + @startdate + '  '
>
> What data type is column [Week]?
>
> Datetime values should be concatenated as characters values.
>
> Example:
>
> use northwind
> go
>
> declare @i int
> declare @sql nvarchar(4000)
> declare @sd datetime
> declare @ed datetime
>
> set @sd = '19970101'
> set @ed = '19971231'
>
> set @sql = N'select @i = count(*) from dbo.orders where orderdate > ''' +
> convert(char(8), @sd, 112) + N''' and orderdate < ''' + convert(char(8),
> dateadd(day, 1, @ed), 112) + N''''
>
> print @sql
>
> exec sp_executesql @sql, N'@i int output', @i output
>
> print @i
> go
>
>
> AMB
>
> exec
>
> "Larry Menzin" wrote:
>
> > Doesn't work.
> >
> > When using dynamic SQL, '+@variable+' is used for numbers, '''+@variable+'''
> > is used for strings, but what syntax is used for dates? I can't seem to find
> > anything that works.
> >
> >
> > --
> > Larry Menzin
> > American Techsystems Corp.
> >
> >
> > "Chandra" wrote:
> >
> > > The string concatination looks correct
> > >
> > > probable you are looking for:
> > >
> > > WHERE cat LIKE ' + @Product + ' AND Week IS NOT NULL
> > > AND RegionID LIKE ' + @Region + ' AND chnl_cd LIKE ' + @Channel + '
> > > AND clutr_nm LIKE ' + @Market + '
> > > AND Week >= ' + datepart(w,@startdate)
> > >
> > > is this the one you are looking for? what should be the result of the query
> > >
> > > --
> > > best Regards,
> > > Chandra
> > > http://chanduas.blogspot.com/
> > > http://groups.msn.com/SQLResource/
> > > ---------------------------------------
> > >
> > >
> > >
> > > "Larry Menzin" wrote:
> > >
> > > > I am having difficulty using dynamic SQL with datetime variables. A code
> > > > snippet like this gives me an error about converting a string to datetime:
> > > >
> > > > ...
> > > > WHERE cat LIKE ' + @Product + ' AND Week IS NOT NULL
> > > > AND RegionID LIKE ' + @Region + ' AND chnl_cd LIKE ' + @Channel + '
> > > > AND clutr_nm LIKE ' + @Market + '
> > > > AND Convert(datetime, Week) >= ' + @startdate + '  '
> > > >
> > > > @startdate is the datetime variable.  Using '' + @startdate + '' prints the
> > > > literal variable @startdate, while using ''' + @startdate + ''' gives me the
> > > > same conversion error. How do I solve this?
> > > >
> > > > --
> > > > Larry Menzin
> > > > American Techsystems Corp.
Author
16 May 2005 1:24 PM
Alejandro Mesa
> dynamic SQL. What is the performance hit from all these date to string
> conversions?

Can you show the code?. I have not idea what conversions are you talking
about.


AMB

Show quote
"Larry Menzin" wrote:

> Ok. I got it to work by converting datetime variables to character strings
> and then comparing strings. Dates apparently cannot be used directly in
> dynamic SQL. What is the performance hit from all these date to string
> conversions?
>
> --
> Larry Menzin
> American Techsystems Corp.
>
>
> "Alejandro Mesa" wrote:
>
> > Larry,
> >
> > > > > AND Convert(datetime, Week) >= ' + @startdate + '  '
> >
> > What data type is column [Week]?
> >
> > Datetime values should be concatenated as characters values.
> >
> > Example:
> >
> > use northwind
> > go
> >
> > declare @i int
> > declare @sql nvarchar(4000)
> > declare @sd datetime
> > declare @ed datetime
> >
> > set @sd = '19970101'
> > set @ed = '19971231'
> >
> > set @sql = N'select @i = count(*) from dbo.orders where orderdate > ''' +
> > convert(char(8), @sd, 112) + N''' and orderdate < ''' + convert(char(8),
> > dateadd(day, 1, @ed), 112) + N''''
> >
> > print @sql
> >
> > exec sp_executesql @sql, N'@i int output', @i output
> >
> > print @i
> > go
> >
> >
> > AMB
> >
> > exec
> >
> > "Larry Menzin" wrote:
> >
> > > Doesn't work.
> > >
> > > When using dynamic SQL, '+@variable+' is used for numbers, '''+@variable+'''
> > > is used for strings, but what syntax is used for dates? I can't seem to find
> > > anything that works.
> > >
> > >
> > > --
> > > Larry Menzin
> > > American Techsystems Corp.
> > >
> > >
> > > "Chandra" wrote:
> > >
> > > > The string concatination looks correct
> > > >
> > > > probable you are looking for:
> > > >
> > > > WHERE cat LIKE ' + @Product + ' AND Week IS NOT NULL
> > > > AND RegionID LIKE ' + @Region + ' AND chnl_cd LIKE ' + @Channel + '
> > > > AND clutr_nm LIKE ' + @Market + '
> > > > AND Week >= ' + datepart(w,@startdate)
> > > >
> > > > is this the one you are looking for? what should be the result of the query
> > > >
> > > > --
> > > > best Regards,
> > > > Chandra
> > > > http://chanduas.blogspot.com/
> > > > http://groups.msn.com/SQLResource/
> > > > ---------------------------------------
> > > >
> > > >
> > > >
> > > > "Larry Menzin" wrote:
> > > >
> > > > > I am having difficulty using dynamic SQL with datetime variables. A code
> > > > > snippet like this gives me an error about converting a string to datetime:
> > > > >
> > > > > ...
> > > > > WHERE cat LIKE ' + @Product + ' AND Week IS NOT NULL
> > > > > AND RegionID LIKE ' + @Region + ' AND chnl_cd LIKE ' + @Channel + '
> > > > > AND clutr_nm LIKE ' + @Market + '
> > > > > AND Convert(datetime, Week) >= ' + @startdate + '  '
> > > > >
> > > > > @startdate is the datetime variable.  Using '' + @startdate + '' prints the
> > > > > literal variable @startdate, while using ''' + @startdate + ''' gives me the
> > > > > same conversion error. How do I solve this?
> > > > >
> > > > > --
> > > > > Larry Menzin
> > > > > American Techsystems Corp.
Author
16 May 2005 2:24 PM
Bojidar Alexandrov
Just expand this example to using all parameterized query and there are no
more headaches ;)

use northwind
go

declare @i int
declare @sql nvarchar(4000)
declare @sd datetime
declare @ed datetime

set @sd = '1997/01/01'
set @ed = '1997/12/31'

set @sql = N'select @i = count(*) from dbo.orders where orderdate between
@sd and @ed'

exec sp_executesql @sql, N'@i int output,@sd datetime, @ed datetime', @i
output,@sd,@ed

print @i
go

Bojidar Alexandrov


Show quote
"Alejandro Mesa" <AlejandroM***@discussions.microsoft.com> wrote in message
news:1C984BCA-610F-4A83-A5DE-295E28582942@microsoft.com...
> Larry,
>
> > > > AND Convert(datetime, Week) >= ' + @startdate + '  '
>
> What data type is column [Week]?
>
> Datetime values should be concatenated as characters values.
>
> Example:
>
> use northwind
> go
>
> declare @i int
> declare @sql nvarchar(4000)
> declare @sd datetime
> declare @ed datetime
>
> set @sd = '19970101'
> set @ed = '19971231'
>
> set @sql = N'select @i = count(*) from dbo.orders where orderdate > ''' +
> convert(char(8), @sd, 112) + N''' and orderdate < ''' + convert(char(8),
> dateadd(day, 1, @ed), 112) + N''''
>
> print @sql
>
> exec sp_executesql @sql, N'@i int output', @i output
>
> print @i
> go
>
>
> AMB
>
> exec
>
> "Larry Menzin" wrote:
>
> > Doesn't work.
> >
> > When using dynamic SQL, '+@variable+' is used for numbers,
'''+@variable+'''
> > is used for strings, but what syntax is used for dates? I can't seem to
find
> > anything that works.
> >
> >
> > --
> > Larry Menzin
> > American Techsystems Corp.
> >
> >
> > "Chandra" wrote:
> >
> > > The string concatination looks correct
> > >
> > > probable you are looking for:
> > >
> > > WHERE cat LIKE ' + @Product + ' AND Week IS NOT NULL
> > > AND RegionID LIKE ' + @Region + ' AND chnl_cd LIKE ' + @Channel + '
> > > AND clutr_nm LIKE ' + @Market + '
> > > AND Week >= ' + datepart(w,@startdate)
> > >
> > > is this the one you are looking for? what should be the result of the
query
> > >
> > > --
> > > best Regards,
> > > Chandra
> > > http://chanduas.blogspot.com/
> > > http://groups.msn.com/SQLResource/
> > > ---------------------------------------
> > >
> > >
> > >
> > > "Larry Menzin" wrote:
> > >
> > > > I am having difficulty using dynamic SQL with datetime variables. A
code
> > > > snippet like this gives me an error about converting a string to
datetime:
> > > >
> > > > ...
> > > > WHERE cat LIKE ' + @Product + ' AND Week IS NOT NULL
> > > > AND RegionID LIKE ' + @Region + ' AND chnl_cd LIKE ' + @Channel + '
> > > > AND clutr_nm LIKE ' + @Market + '
> > > > AND Convert(datetime, Week) >= ' + @startdate + '  '
> > > >
> > > > @startdate is the datetime variable.  Using '' + @startdate + ''
prints the
> > > > literal variable @startdate, while using ''' + @startdate + '''
gives me the
Show quote
> > > > same conversion error. How do I solve this?
> > > >
> > > > --
> > > > Larry Menzin
> > > > American Techsystems Corp.
Author
16 May 2005 12:19 PM
Jens Süßmeyer
Don´t no whether you defined the datetime as ISO (then the implicit
conversion will to add a number to a string which wouldn´t be that nice).
How did you dfine your Datetime variable ?


HTH, Jens Suessmeyer.

---
http://www.sqlserver2005.de
---

Show quote
"Larry Menzin" <LarryMen***@discussions.microsoft.com> schrieb im
Newsbeitrag news:40CFE544-2384-4BAD-9FB3-339A5D10BB88@microsoft.com...
>I am having difficulty using dynamic SQL with datetime variables. A code
> snippet like this gives me an error about converting a string to datetime:
>
> ...
> WHERE cat LIKE ' + @Product + ' AND Week IS NOT NULL
> AND RegionID LIKE ' + @Region + ' AND chnl_cd LIKE ' + @Channel + '
> AND clutr_nm LIKE ' + @Market + '
> AND Convert(datetime, Week) >= ' + @startdate + '  '
>
> @startdate is the datetime variable.  Using '' + @startdate + '' prints
> the
> literal variable @startdate, while using ''' + @startdate + ''' gives me
> the
> same conversion error. How do I solve this?
>
> --
> Larry Menzin
> American Techsystems Corp.
Author
16 May 2005 12:57 PM
Larry Menzin
DECLARE     @startDate datetime

SET @startDate = '4/1/2005' /* for testing */

I still can't get dynamic SQL to work using date variables embedded in the
SQL string.


--
Larry Menzin
American Techsystems Corp.


Show quote
"Jens Süßmeyer" wrote:

> Don´t no whether you defined the datetime as ISO (then the implicit
> conversion will to add a number to a string which wouldn´t be that nice).
> How did you dfine your Datetime variable ?
>
>
> HTH, Jens Suessmeyer.
>
> ---
> http://www.sqlserver2005.de
> ---
>
> "Larry Menzin" <LarryMen***@discussions.microsoft.com> schrieb im
> Newsbeitrag news:40CFE544-2384-4BAD-9FB3-339A5D10BB88@microsoft.com...
> >I am having difficulty using dynamic SQL with datetime variables. A code
> > snippet like this gives me an error about converting a string to datetime:
> >
> > ...
> > WHERE cat LIKE ' + @Product + ' AND Week IS NOT NULL
> > AND RegionID LIKE ' + @Region + ' AND chnl_cd LIKE ' + @Channel + '
> > AND clutr_nm LIKE ' + @Market + '
> > AND Convert(datetime, Week) >= ' + @startdate + '  '
> >
> > @startdate is the datetime variable.  Using '' + @startdate + '' prints
> > the
> > literal variable @startdate, while using ''' + @startdate + ''' gives me
> > the
> > same conversion error. How do I solve this?
> >
> > --
> > Larry Menzin
> > American Techsystems Corp.
>
>
>
Author
16 May 2005 2:49 PM
CBretana
Larry,

  First of all, what's with Convert (DateTime, Week) syntax? is that a typo?
Did you mean DateAdd() ? or DatePart ?, cause Convert(DateTime, Week) is NOT
a legal SQL Server FUnction... Convert requires three parameters.

But anyway,  It's NOT the @StartDate that is causing the problem... It's
probably the [datetime] column in the convert function that is throwing the
error...

                          \/
AND Convert(DATETIME, Week) >= ' + @startdate + '  '


How is that column defined in the Table?



Show quote
"Larry Menzin" wrote:

> DECLARE     @startDate datetime
>
> SET @startDate = '4/1/2005' /* for testing */
>
> I still can't get dynamic SQL to work using date variables embedded in the
> SQL string.
>
>
> --
> Larry Menzin
> American Techsystems Corp.
>
>
> "Jens Süßmeyer" wrote:
>
> > Don´t no whether you defined the datetime as ISO (then the implicit
> > conversion will to add a number to a string which wouldn´t be that nice).
> > How did you dfine your Datetime variable ?
> >
> >
> > HTH, Jens Suessmeyer.
> >
> > ---
> > http://www.sqlserver2005.de
> > ---
> >
> > "Larry Menzin" <LarryMen***@discussions.microsoft.com> schrieb im
> > Newsbeitrag news:40CFE544-2384-4BAD-9FB3-339A5D10BB88@microsoft.com...
> > >I am having difficulty using dynamic SQL with datetime variables. A code
> > > snippet like this gives me an error about converting a string to datetime:
> > >
> > > ...
> > > WHERE cat LIKE ' + @Product + ' AND Week IS NOT NULL
> > > AND RegionID LIKE ' + @Region + ' AND chnl_cd LIKE ' + @Channel + '
> > > AND clutr_nm LIKE ' + @Market + '
> > > AND Convert(datetime, Week) >= ' + @startdate + '  '
> > >
> > > @startdate is the datetime variable.  Using '' + @startdate + '' prints
> > > the
> > > literal variable @startdate, while using ''' + @startdate + ''' gives me
> > > the
> > > same conversion error. How do I solve this?
> > >
> > > --
> > > Larry Menzin
> > > American Techsystems Corp.
> >
> >
> >
Author
16 May 2005 10:41 PM
Hugo Kornelis
On Mon, 16 May 2005 07:49:22 -0700, CBretana wrote:

>  First of all, what's with Convert (DateTime, Week) syntax? is that a typo?
(snip)

Hi Charly,

It's actually correct syntax: the CONVERT function can be used without
the style parameter:

SELECT CONVERT(datetime, '20050101')

In this case, week is the (pretty confusing) column name. SQL Server
will allow this:

SELECT CONVERT(datetime, Week)
FROM (SELECT '20050101' AS Week) AS x

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Author
17 May 2005 1:14 AM
CBretana
Thanks, Hugo... Confusing is correct,  I didn't even think of that...

   Then the error he was experiencing, (Larry's long gone now, methinks) 
was probably from a string value in the "Week" column that could not be
converted to a dateTime...

Regards, Charly

Show quote
"Hugo Kornelis" wrote:

> On Mon, 16 May 2005 07:49:22 -0700, CBretana wrote:
>
> >  First of all, what's with Convert (DateTime, Week) syntax? is that a typo?
> (snip)
>
> Hi Charly,
>
> It's actually correct syntax: the CONVERT function can be used without
> the style parameter:
>
> SELECT CONVERT(datetime, '20050101')
>
> In this case, week is the (pretty confusing) column name. SQL Server
> will allow this:
>
> SELECT CONVERT(datetime, Week)
> FROM (SELECT '20050101' AS Week) AS x
>
> Best, Hugo
> --
>
> (Remove _NO_ and _SPAM_ to get my e-mail address)
>
Author
16 May 2005 10:39 PM
Hugo Kornelis
On Mon, 16 May 2005 05:11:02 -0700, Larry Menzin wrote:

>I am having difficulty using dynamic SQL with datetime variables. A code
>snippet like this gives me an error about converting a string to datetime:
>
>...
>WHERE cat LIKE ' + @Product + ' AND Week IS NOT NULL
>AND RegionID LIKE ' + @Region + ' AND chnl_cd LIKE ' + @Channel + '
>AND clutr_nm LIKE ' + @Market + '
>AND Convert(datetime, Week) >= ' + @startdate + '  '
>
>@startdate is the datetime variable.  Using '' + @startdate + '' prints the
>literal variable @startdate, while using ''' + @startdate + ''' gives me the
>same conversion error. How do I solve this?

Hi Larry,

Why use dynamic SQL at all?

WHERE cat LIKE @Product
AND   Week IS NOT NULL
AND   RegionID LIKE @RegionID    -- Do you really need LIKE here??
-- AND RegionID = @RegionID    -- This version might be lots quicker!!
AND   chnl_cd LIKE @Channel    -- Same remark as above
AND   clutr_nm LIKE @Market    -- Same remark as above
AND   CONVERT(datetime, Week) >= @startdate

I also suggest that you rename the "Week" column. Though "week" is not a
reserved keyword, it is displayed in a special color in Query Analyzer.
This can easily lead to confusion.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)

AddThis Social Bookmark Button