How do I convert an integer to string as part of a PostgreSQL query?

后端 未结 3 1871
别跟我提以往
别跟我提以往 2020-12-13 03:19

How do I convert an integer to string as part of a PostgreSQL query?

So, for example, I need:

SELECT * FROM table WHERE  = \'stri         


        
相关标签:
3条回答
  • 2020-12-13 03:41

    You can cast an integer to a string in this way

    intval::text
    

    and so in your case

    SELECT * FROM table WHERE <some integer>::text = 'string of numbers'
    
    0 讨论(0)
  • 2020-12-13 03:52

    Because the number can be up to 15 digits, you'll meed to cast to an 64 bit (8-byte) integer. Try this:

    SELECT * FROM table
    WHERE myint = mytext::int8
    

    The :: cast operator is historical but convenient. Postgres also conforms to the SQL standard syntax

    myint = cast ( mytext as int8)
    

    If you have literal text you want to compare with an int, cast the int to text:

    SELECT * FROM table
    WHERE myint::varchar(255) = mytext
    
    0 讨论(0)
  • 2020-12-13 03:59

    You could do this:

    SELECT * FROM table WHERE cast(YOUR_INTEGER_VALUE as varchar) = 'string of numbers'
    
    0 讨论(0)
提交回复
热议问题