Olga Melnikova
Olga Melnikova

Reputation: 73

How to Sum text and number sqlite3

input:

cursor.execute('SELECT SUM(CAST("Item Total" as FLOAT)) FROM price')
res_sum = cursor.fetchone()
print(res_sum)

output:

(0.0,) 

example: $47.99 + $47.99 + $47.99 + $47.99

Upvotes: 0

Views: 57

Answers (1)

MikeT
MikeT

Reputation: 56958

You could use SELECT SUM(CAST(substr("item total",2) AS FLOAT)) FROM price;

However, it would be better to store the values without the currency character. You could always prepend the value when extracting if so desired.

e.g. SELECT '$'||SUM(CAST(substr("item total",2) AS FLOAT)) FROM price; would output $191.96 as opposed to just 191.96.

Upvotes: 1

Related Questions