I'm relatively new to sql-server; I'm trying to have a start-date and end-date pulled from a form-variable as string then convert it into datetime (yyyy-mm-dd) format and I can't seem to find anything that works. Attempted code and resulting error is below. Any advice would be appreciated.
declare @startdate as varchar
declare @enddate as varchar
set @startdate=cast(@startdate as datetime)
set @enddate=cast(@enddate as datetime)
SELECT order_date, inv_no
from invoices
where order_date between @startdate and @enddate
The error I keep getting is:
Conversion failed when converting datetime from character string.
How do I fix this?
yyyy-mm-dddoesn't work on all locales fordatetime.yyyymmddis more robust.