How to pass datetime as parameter to a sql openquery

Zvonimir Tokic picture Zvonimir Tokic · Jul 9, 2015 · Viewed 8.2k times · Source

I have an OPENQUERY statement

SELECT * 
FROM OPENQUERY (NETLINE, 
                'SELECT * FROM XCREW.CTNAISV_HOTELCREW where RESERVATION_DATE = ''2015-05-01''')

After executing it I have error message on 2015

How to pass datetime as parameter in OPENQUERY ?

Thanks

Answer

Ionic picture Ionic · Jul 9, 2015

Try this instead. Explicitly convert it to a datetime (maybe convert it to the proper datetime format you use).

SELECT * 
FROM OPENQUERY (NETLINE, 
                'SELECT * FROM XCREW.CTNAISV_HOTELCREW where RESERVATION_DATE = CONVERT(datetime,''2015-05-01'')')

The proper format can also applied using CONVERT(datetime, N'2015-05-01', 112) (for example).