I am working in SQLLite Studio and trying to find all rows with a given CUSIP. However, when I add "where cusip='00080010'", my query returns no results, despite that fact that I can see that the first row has that cusip.
I first considered that cusip might actually be an integer, so I checked the table definition and it is indeed listed as TEXT.
I created this test query and found that string equality with my string literal is failing:
select case when cusip='00080010' then 1 else 0 end test, cusip
from msf
order by date,cusip
limit 1
test cusip
0 00080010
I tried running the query in python instead in case this was an issue with SQLLite Studio and got the following results:
db.execute("""select case when cusip='00080010' then 1 else 0 end test, cusip
...: from msf
...: order by date,cusip
...: limit 1""").fetchone()
(0, b'00080010')
So, possibly it's being stored as bytes. I then tried modifying my query:
select case when cusip=b'00080010' then 1 else 0 end test, cusip
from msf
order by date,cusip
limit 1
Error while executing SQL query on database 'a_stock': near "'00080010'": syntax error
Second attempt:
select case when cusip=cast('00080010' as bytes) then 1 else 0 end test, cusip
from msf
order by date,cusip
limit 1
test cusip
0 00080010
In python:
db.execute("""select case when cusip=cast('00080010' as bytes) then 1 else 0 end test, cusip
...: from msf
...: order by date,cusip
...: limit 1""").fetchone()
(0, b'00080010')
I'm out of ideas now. Why is the string comparison failing, how do I fix it, and is there a way to know ahead of time if this is going to happen?
Edit: Sorry for the typo, the value is '00080010' and I put it into SO incorrectly. I retested the code to make sure I'm still having the same issue.