Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TSQL -- Inserting Dates Into Dynamic SQL

Consider the following TSQL:

SET @WhereClause1 = 'where a.Date > ' + @InvoiceDate

I get a date/string conversion error. @InvoiceDate is a datetime variable. What is the right syntax?

like image 656
Jeff Avatar asked Apr 02 '09 05:04

Jeff


2 Answers

This might work.

SET @WhereClause1 = 'where a.Date > ''' + convert(varchar, @InvoiceDate) + ''''

although an error will be raised if the value is null.

like image 99
Andy White Avatar answered Sep 24 '22 06:09

Andy White


This will work:

SET @WhereClause1 = 'where a.Date > ''' + cast(@InvoiceDate as varchar(100)) + ''''
like image 32
eKek0 Avatar answered Sep 23 '22 06:09

eKek0