![]() So if we drop the character part from the above example, it will work fine. So when we specify it to be cast to integer, the "to be casted" object should comprise of numbers only. The reason is that 100ab comprises of both numbers and characters. We get a proper result set where 100 is returned as an integer and not a string.īut if we want to cast an alphanumeric text, say "100ab " to integer, which is not possible. Like we discussed above, suppose we need to do some mathematical calculations on a number, which is actually stored in a varchar datatype column. If we write something like: SELECT '100'::integer We will understand few such DOs and DONTs with some simple examples. To use the CAST operator to type cast from datatype to another, we also need to ensure that the data can be converted to the target datatype or qualifies to be of the target datatype too. Just like other operations, CAST in PostgreSQL also has it's own DOs and DONTs. We can do it either writing: SELECT CAST(97 AS varchar) ĭifferent scenarios of CAST in PostgreSQL This means we need to convert an integer datatype to string datatype. Suppose we want the number 97 to be returned to us a String datatype. Instead of using the above syntax, we can also use the following condensed syntax: expression::type
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |