all groups > sql server programming > october 2003 >
You're in the

sql server programming

group:

DateTime in an SP


DateTime in an SP Nigel Rivett
10/31/2003 8:44:10 PM
sql server programming: try
SELECT @sdate = '20030315'

what datatype is ShowDate?

You need to convert @sdate to a character string in the
correct format

AND (ShowDate = ''' + convert(varchar(8),@sdate,112) + ''')

see convert in bol for the styles.
DateTime in an SP Wayne Wengert
10/31/2003 9:32:07 PM
I am trying to specify a date as a criteria in an SQL statement used within
an SP. The date is an input parameter. When I run the SP I get an error
"Syntax error converting datetime from character string."

The declaration shows:
DECLARE @sdate datetime
DECLARE @stype char(2)
DECLARE @regyear char(4)
SELECT @sdate = '03/15/2003'
SELECT @stype = 'SC'
SELECT @regyear = '2003'

And the relevant part of the SQL statement is:
AND (ShowDate = ''' + @sdate + ''') <= those are 3 single quotes

What is the correct syntax within an SP for datetime?

--
------------------------------------
Wayne Wengert
wayne@wengert.org

DateTime in an SP Nigel Rivett
10/31/2003 9:33:50 PM
[quoted text, click to view]
only be done by converting the date to a string?

No but you seemed to be trying to create dynamic sql for
the query.

select *
from tbl
where ShowDate = @sdate

Should work as you have smalldatetime and datetime - but
Re: DateTime in an SP Wayne Wengert
10/31/2003 9:58:53 PM
The ShowDate is a SmallDateTime value. I tried CASTing the @sdate but that
didn't work.

So to compare a date within the SQL in an SP it can only be done by
converting the date to a string? That sure seems weird. I'll go to bol to
see if I can figure this out.

Wayne

[quoted text, click to view]

Re: DateTime in an SP Andrew J. Kelly
11/1/2003 12:23:43 AM
Wayne,

If the sql statement your referring to is not a dynamic one (exec or
sp_executesql) then you don't need quotes at all if the variable is already
a datetime or smalldatetime.

--

Andrew J. Kelly
SQL Server MVP


[quoted text, click to view]

Re: DateTime in an SP Nigel Rivett
11/1/2003 7:37:27 AM
Spotted your earlier post and you are indeed using dynamic sql.
Try this - I have reformatted to make it easier - if it loses the
formatting insert tabs so that all the statements line up

select @sql = 'SELECT DISTINCT ShowName, ShowTable, CircuitID,
ShowDate, ShowType, RegCode, PorF'
select @sql = @sql + ' FROM ' + @sglpnltable
select @sql = @sql + ' WHERE CircuitID = 0' +
select @sql = @sql + ' AND ShowDate = ''' +
convert(varchar(8),@sdate,112) + ''''
select @sql = @sql + ' AND ShowType = ''' + @stype +''''
select @sql = @sql + ' UNION '
select @sql = @sql + 'SELECT DISTINCT ShowName, ShowTable, CircuitID,
ShowDate, ShowType, RegCode, PorF'
select @sql = @sql + ' FROM ' + @dblpnltable
select @sql = @sql + ' WHERE CircuitID = 0)'
select @sql = @sql + ' AND ShowDate = '''+
convert(varchar(8),@sdate,112) + ''''
select @sql = @sql + ' AND ShowType = ''' + @stype + ''''
select @sql = @sql + ' Order By RegCode'
Print (@sql)
EXEC (@sql)


Nigel Rivett (SQL Server MVP)
www.nigelrivett.net

*** Sent via Developersdex http://www.developersdex.com ***
Re: DateTime in an SP Nigel Rivett
11/1/2003 7:37:28 AM
dynamic sql is when you create a string and execute it

exec ('select * from mytbl')

It is used when you need to build up an sql statement
declare @sql varchar(1000)
select @sql = 'select * from ' + @tblname
exec(@sql)
which is why it is called dynamic.

Your original question had

AND (ShowDate = ''' + @sdate + ''') <= those are 3 single quotes

which looks like you are building a string for the sql statement - hence
my convertion to a character string for the date

if not

select *
from tbl
where ....
and ShowDate = @sdate
and ...


Nigel Rivett (SQL Server MVP)
www.nigelrivett.net

*** Sent via Developersdex http://www.developersdex.com ***
Re: DateTime in an SP Wayne Wengert
11/1/2003 7:59:27 AM
Andrew;

Thanks for the response. I am confused on exactly what "dynamic sql" means
(I'll do more reading in bol) so I'm not sure what I have. The SQL is within
an SP that is called from an ASP page using EXEC.

Wayne

[quoted text, click to view]

Re: DateTime in an SP Wayne Wengert
11/1/2003 1:34:41 PM
Nigel;

Thanks for the responses. I got it working but I need to understand this
better so I'll do more looking in my books and bol

Appreciate the education

Wayne


[quoted text, click to view]

Re: DateTime in an SP Amit Arora
11/1/2003 5:06:20 PM
Try using
SELECT @sdate = CAST('03/15/2003' AS DATETIME)

[quoted text, click to view]

AddThis Social Bookmark Button