简体   繁体   中英

Postgresql interval to Integer

Currently I have a column call days and the values are 1 day, 2 days etc. I am trying to extract or keep only the numeric value.

Something like:

select CAST(LEFT(days, CHARINDEX(' ', days)) as integer) as #days from daily_table

You could first cast the string to an interval, then use extract() :

select extract(day from days::interval) as "#days"
from daily_table;

You can use substring() function to get the numeric value and then cast to INTEGER

select cast(substring(days, 1,  position('d' in days)-2) as INTEGER)
as "#days" from daily_table;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM