Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. SQL Server
  3. Question
SQL Server

Which method should I choose to postgres cast split string to double precision?

Asked by Buffy Heaton Feb 7, 2023 1.1K views 1 answer
Share

About this question

 I have a cost column in my postgres db whose type is varchar. It stores the costs of each service a client gets in this format "KES 0.80". I am trying to sum up everything to get a cost breakdown.


This is how my query looks


select sum(cast(split_part("cost", ' ', 2) as double precision)) from send_log;

I am having a challenge casting the string to double precision so that I can get the sum. This is the error i am getting


ERROR:  invalid input syntax for type double precision: ""

And if i try to cast into an integer i get this error


ERROR:  invalid input syntax for integer: "0.8000"

I am looking for a better method to cast the string to double precision or advise on how I can format my query to get the total sum from the cost column. Any pointers or useful links will be appreciated. 

Your answer

1 Answer

More SQL Server discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest SQL Server Blogs

Guides, tips and career advice on SQL Server from JanBask experts.