问题
Everytime I execute this query I get this error: The data types varchar and varchar are incompatible in the modulo operator. Asking for your help. Thanks!
DECLARE @date datetime, @SQL NVARCHAR(MAX);
SET @date = '2019';
SET @SQL = 'SELECT * FROM OPENQUERY(LINK2, ''SELECT * FROM wordpress.wp_users WHERE user_registered LIKE '''%' +@date+ '%''''')';
EXEC sp_executesql @SQL;
回答1:
you can be varchar(4) because you are setting it as string. then use datepart()
DECLARE @date varchar(4), @SQL NVARCHAR(MAX);
SET @date = '2019';
SET @SQL = 'SELECT * FROM OPENQUERY(LINK2, ''SELECT * FROM wordpress.wp_users WHERE DATEPART(yy, user_registered) LIKE ''''%' +@date+ '%'''''')';
EXEC sp_executesql @SQL;
来源:https://stackoverflow.com/questions/57722978/openquery-with-variables-in-datetime-using-like-clause-with-linked-server