0

i have an issue with this piece of code coming from SQL :

UPDATE resultats_du_jour
SET Heure_debut =  CONCAT(SUBSTRING(Heure_debut,1,2) +
    12,SUBSTRING(Heure_debut,3,3))
WHERE Heure_debut LIKE '%PM';

This gives me the following output :

sql:53: ER ROR: operator does not exist: text + integer LINE 1: ...ET Heure_debut = CONCAT(SUBSTRING(Heure_debut,1,2)+12,SUBSTR... 

I understand that you cannot add text+integer, but how can i proceed to do that?

Many thanks.

2
  • Having do update parts of a column value smells like a bad design. Given the name of the column (if my french serves me well), this looks like you are misusing a text column to store something like a time or an interval. If that was a time column, simply adding 12 hours would be as simple as heure_debut + interval '12 hour' - or it might not be necessary at all given your condition like '%PM' which sounds as if you are fixing a bad user input that could not have happened if you had chosen the correct data type Commented Jun 27, 2018 at 14:26
  • In fact, data from csv file come from a format like "03:40 PM" so if we could have use TIME datatype, we would have do it. Your french serves you well :) Commented Jun 28, 2018 at 7:18

1 Answer 1

1

If you want to add 12 to the numerical equivalent of the first substring you take, and then concatenate it again back to text, then you may use casts:

UPDATE resultats_du_jour
SET Heure_debut =  CONCAT((SUBSTRING(Heure_debut, 1, 2)::int + 12)::text,
    SUBSTRING(Heure_debut, 3, 3))
WHERE Heure_debut LIKE '%PM';

This feels a bit hackish, and in general as a matter of good design you should decide whether a certain type of data is text or a number. Here, if you had things stored as numbers, you might not need to cast at all.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.