[SalesForce] comparing id field with string field in soql query

I am new to Apex. I have a SOQL query like SELECT Session_Id__c FROM Device__c WHERE User_Id__c = :userid. In this User_id__c is custom field defined datatype as Text and userid is id of user. Is that qury works or do i need to do anything else? I believe it is not working as expected.I will appreciate help.

Best Answer

Yes, it should work just fine, assuming you're using 18-character ID values exclusively. Since queries are case insensitive, if you're only using the 15-character ID values in your text field, you'll get invalid results. I know I've seen a lot of questions where people intentionally remove the "extra" three characters from the end of ID values for some reason or another, but that will mess up queries like this one.