how to convert a varchar field value into an integ

2019-07-16 14:48发布

for example in a table one field is character varying so how to convert that character varying to an Integer type, what exactly is I need to get the return value in Integer datatype

Database :

标签: postgresql
3条回答
Viruses.
2楼-- · 2019-07-16 15:21
SELECT mycolumn::integer FROM mytable WHERE something 

SELECT CASE WHEN mycolumn = NULL THEN NULL ELSE mycolumn :: Integer END
FROM mytable WHERE something 
查看更多
萌系小妹纸
3楼-- · 2019-07-16 15:24

i would suggest to make Functions to do the job i.e

    CREATE OR REPLACE FUNCTION chartoint(charparam character varying)
    RETURNS integer AS
    $BODY$
    SELECT CASE WHEN trim($1) ~ '[0-9]+' THEN CAST(trim($1) AS integer) ELSE NULL END;
    $BODY$
    LANGUAGE sql IMMUTABLE STRICT
查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-07-16 15:27

You may try like this:

SELECT NULLIF(your_value, '')::int

or extend that like thats

SELECT CAST(coalesce(column_name, '0') AS integer) as Value
from table_name
查看更多
登录 后发表回答