Search code examples
sqlsql-serverdatabaseopenquery

How to pass datetime as parameter to a sql openquery


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


Solution

  • 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).