0

I have a Java system that uses Sequence in PK in database (postgreSQL and PGadmin) This sequence should take the current month (with two digits) followed by the current year. It turns out that a certain time of day arrives and the command below is executed (Java project class):

String sql = "select case when trim(substring(cast((SELECT last_value FROM  sequencia.Tb_um) as varchar),1,4)) <> trim(to_char(current_date,'MMYY')) then"
                + " setval(‘sequencia.Tb_um', cast(trim(to_char(current_date,'MMYY')||'001') as integer), true) "
                + " else 0 end as valor";

But I don't know what happens when the leading zero of the month is removed, leaving for example: “422001” in the DB (the corret: "0422001"), the question is: what is it that is deleting this zero?

Thanks for any help!

4
  • Ah... "Smart IDs" were all the rage back in the 80s. Commented Apr 26, 2022 at 17:59
  • Check the "as integer" part ;p Commented Apr 26, 2022 at 18:20
  • 1
    Well, the expression cast(trim(to_char(current_date,'MMYY')||'001') as integer) is an integer - so it has no leading zero 422001. Other way round, do not cast to integer if you want a varchar with leading zero trim(to_char(current_date,'MMYY')||'001') gives 0422001 Commented Apr 26, 2022 at 18:21
  • 1
    @MarmiteBomber - Ahh that was it! thank you so much, sorry for being such a silly thing now i understand why. They helped a lot. Commented Apr 26, 2022 at 18:28

0

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.