Openquery with variables in datetime using like clause with linked server

北城余情 提交于 2019-12-11 15:36:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!