Left-filling a text field with zerosHow to concatenate text from multiple rows into a single text string in SQL server?Can I concatenate multiple MySQL rows into one field?What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?why does my nested case statement return a character set mismatch?Search text in stored procedure in SQL ServerCAST to int used with LEFT on an nvarchar field not working properly?Divide by zero error in SQL even though I excluded the zero cases?Updating Web2py's auto_increment id with filled database?How do I need to modify this MERGE statement, for a mass update of one field based on the value of a field in another table?Nested SQL query searching the wrong table (postgreSQL)

I'm in charge of equipment buying but no one's ever happy with what I choose. How to fix this?

Generic lambda vs generic function give different behaviour

Is it okay / does it make sense for another player to join a running game of Munchkin?

At which point does a character regain all their Hit Dice?

Implement the Thanos sorting algorithm

How can I replace every global instance of "x[2]" with "x_2"

Is there any reason not to eat food that's been dropped on the surface of the moon?

There is only s̶i̶x̶t̶y one place he can be

Is the destination of a commercial flight important for the pilot?

What is difference between behavior and behaviour

Tiptoe or tiphoof? Adjusting words to better fit fantasy races

Why are on-board computers allowed to change controls without notifying the pilots?

Coordinate position not precise

Is this Spell Mimic feat balanced?

How do I rename a LINUX host without needing to reboot for the rename to take effect?

Why is delta-v is the most useful quantity for planning space travel?

Valid Badminton Score?

What is the intuitive meaning of having a linear relationship between the logs of two variables?

What would happen if the UK refused to take part in EU Parliamentary elections?

Can somebody explain Brexit in a few child-proof sentences?

Trouble understanding overseas colleagues

Efficiently merge handle parallel feature branches in SFDX

What's the purpose of "true" in bash "if sudo true; then"

What defines a dissertation?



Left-filling a text field with zeros


How to concatenate text from multiple rows into a single text string in SQL server?Can I concatenate multiple MySQL rows into one field?What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?why does my nested case statement return a character set mismatch?Search text in stored procedure in SQL ServerCAST to int used with LEFT on an nvarchar field not working properly?Divide by zero error in SQL even though I excluded the zero cases?Updating Web2py's auto_increment id with filled database?How do I need to modify this MERGE statement, for a mass update of one field based on the value of a field in another table?Nested SQL query searching the wrong table (postgreSQL)













-1















I have a postgres database that I can access with PGAdmin III. I run a script to change a number stored in a text field and then add left zeros to file it to four characters. It's a time field that must be stored as text. I'd like to do it in a single run instead of two. Here's the first clause to add hours to the time field as text;



UPDATE timetable
SET eta = (
CASE
WHEN (trips.starttime::int / 100) + (destination.zuluoffset * -1 ) < 24 THEN ((trips.starttime::int / 100) + (destination.zuluoffset * -1 )) * 100
WHEN (trips.starttime::int / 100) + (destination.zuluoffset * -1 ) > 23 THEN ((trips.starttime::int / 100) + (destination.zuluoffset * -1 ) - 24) * 100
END )
FROM
destination,
trips
WHERE
timetable.tripsid = trips.id;


This does fine and adds the desired number of hours while correcting for results of greater than 24 hours. However, this leave any times less than 1000 hours as three digits or even a single 0 for midnight. The field needs to be 4 characters.



So I run this as a second clause;



UPDATE timetable
SET eta = lpad(eta, 4, '0');


and this works also. But how can I add the lpad to the first Update clause? I tried putting the entire CASE statement in the lpad statement in place of eta like this;



SET eta = lpad((CASE statement here), 4, '0')


but I get this error;



ERROR: function lpad(numeric, integer, unknown) does not exist
LINE 3: SET eta = lpad((
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.


I've tried casting eta with ::int, ::text, and ::varchar, but that just return a sytax error.










share|improve this question




























    -1















    I have a postgres database that I can access with PGAdmin III. I run a script to change a number stored in a text field and then add left zeros to file it to four characters. It's a time field that must be stored as text. I'd like to do it in a single run instead of two. Here's the first clause to add hours to the time field as text;



    UPDATE timetable
    SET eta = (
    CASE
    WHEN (trips.starttime::int / 100) + (destination.zuluoffset * -1 ) < 24 THEN ((trips.starttime::int / 100) + (destination.zuluoffset * -1 )) * 100
    WHEN (trips.starttime::int / 100) + (destination.zuluoffset * -1 ) > 23 THEN ((trips.starttime::int / 100) + (destination.zuluoffset * -1 ) - 24) * 100
    END )
    FROM
    destination,
    trips
    WHERE
    timetable.tripsid = trips.id;


    This does fine and adds the desired number of hours while correcting for results of greater than 24 hours. However, this leave any times less than 1000 hours as three digits or even a single 0 for midnight. The field needs to be 4 characters.



    So I run this as a second clause;



    UPDATE timetable
    SET eta = lpad(eta, 4, '0');


    and this works also. But how can I add the lpad to the first Update clause? I tried putting the entire CASE statement in the lpad statement in place of eta like this;



    SET eta = lpad((CASE statement here), 4, '0')


    but I get this error;



    ERROR: function lpad(numeric, integer, unknown) does not exist
    LINE 3: SET eta = lpad((
    ^
    HINT: No function matches the given name and argument types. You might need to add explicit type casts.


    I've tried casting eta with ::int, ::text, and ::varchar, but that just return a sytax error.










    share|improve this question


























      -1












      -1








      -1








      I have a postgres database that I can access with PGAdmin III. I run a script to change a number stored in a text field and then add left zeros to file it to four characters. It's a time field that must be stored as text. I'd like to do it in a single run instead of two. Here's the first clause to add hours to the time field as text;



      UPDATE timetable
      SET eta = (
      CASE
      WHEN (trips.starttime::int / 100) + (destination.zuluoffset * -1 ) < 24 THEN ((trips.starttime::int / 100) + (destination.zuluoffset * -1 )) * 100
      WHEN (trips.starttime::int / 100) + (destination.zuluoffset * -1 ) > 23 THEN ((trips.starttime::int / 100) + (destination.zuluoffset * -1 ) - 24) * 100
      END )
      FROM
      destination,
      trips
      WHERE
      timetable.tripsid = trips.id;


      This does fine and adds the desired number of hours while correcting for results of greater than 24 hours. However, this leave any times less than 1000 hours as three digits or even a single 0 for midnight. The field needs to be 4 characters.



      So I run this as a second clause;



      UPDATE timetable
      SET eta = lpad(eta, 4, '0');


      and this works also. But how can I add the lpad to the first Update clause? I tried putting the entire CASE statement in the lpad statement in place of eta like this;



      SET eta = lpad((CASE statement here), 4, '0')


      but I get this error;



      ERROR: function lpad(numeric, integer, unknown) does not exist
      LINE 3: SET eta = lpad((
      ^
      HINT: No function matches the given name and argument types. You might need to add explicit type casts.


      I've tried casting eta with ::int, ::text, and ::varchar, but that just return a sytax error.










      share|improve this question
















      I have a postgres database that I can access with PGAdmin III. I run a script to change a number stored in a text field and then add left zeros to file it to four characters. It's a time field that must be stored as text. I'd like to do it in a single run instead of two. Here's the first clause to add hours to the time field as text;



      UPDATE timetable
      SET eta = (
      CASE
      WHEN (trips.starttime::int / 100) + (destination.zuluoffset * -1 ) < 24 THEN ((trips.starttime::int / 100) + (destination.zuluoffset * -1 )) * 100
      WHEN (trips.starttime::int / 100) + (destination.zuluoffset * -1 ) > 23 THEN ((trips.starttime::int / 100) + (destination.zuluoffset * -1 ) - 24) * 100
      END )
      FROM
      destination,
      trips
      WHERE
      timetable.tripsid = trips.id;


      This does fine and adds the desired number of hours while correcting for results of greater than 24 hours. However, this leave any times less than 1000 hours as three digits or even a single 0 for midnight. The field needs to be 4 characters.



      So I run this as a second clause;



      UPDATE timetable
      SET eta = lpad(eta, 4, '0');


      and this works also. But how can I add the lpad to the first Update clause? I tried putting the entire CASE statement in the lpad statement in place of eta like this;



      SET eta = lpad((CASE statement here), 4, '0')


      but I get this error;



      ERROR: function lpad(numeric, integer, unknown) does not exist
      LINE 3: SET eta = lpad((
      ^
      HINT: No function matches the given name and argument types. You might need to add explicit type casts.


      I've tried casting eta with ::int, ::text, and ::varchar, but that just return a sytax error.







      sql postgresql sql-update






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 21 at 15:50









      a_horse_with_no_name

      305k46467562




      305k46467562










      asked Mar 21 at 15:25









      Stuart K. SmithStuart K. Smith

      325




      325






















          2 Answers
          2






          active

          oldest

          votes


















          1














          Why are you using LPAD if the result of your CASE statement is numeric , use simply to_char :



           UPDATE timetable
          SET eta = to_char (
          CASE
          WHEN (trips.starttime::int / 100) + (destination.zuluoffset * -1 ) < 24 THEN ((trips.starttime::int / 100) + (destination.zuluoffset * -1 )) * 100
          WHEN (trips.starttime::int / 100) + (destination.zuluoffset * -1 ) > 23 THEN ((trips.starttime::int / 100) + (destination.zuluoffset * -1 ) - 24) * 100
          END , 'FM0000')
          FROM destination
          INNER JOIN trips ON timetable.tripsid = trips.id;





          share|improve this answer























          • This worked as well as the one above. I selected this one because it seemed "more" correct in that it uses char formatting directly instead of converting num to char. Thanks for the excellent reply.

            – Stuart K. Smith
            Mar 21 at 17:53


















          1














          should be LPAD(your_col::text, 4, '0')



           UPDATE timetable
          SET eta = LPAD ((
          CASE
          WHEN (trips.starttime::int / 100) + (destination.zuluoffset * -1 ) < 24 THEN ((trips.starttime::int / 100) + (destination.zuluoffset * -1 )) * 100
          WHEN (trips.starttime::int / 100) + (destination.zuluoffset * -1 ) > 23 THEN ((trips.starttime::int / 100) + (destination.zuluoffset * -1 ) - 24) * 100
          END )::text, 4,'0')
          FROM destination
          INNER JOIN trips ON timetable.tripsid = trips.id;





          share|improve this answer























          • This worked as well as the one below. However, I can only select one "correct" reply. Thanks a lot for your reply.

            – Stuart K. Smith
            Mar 21 at 17:53











          • @StuartK.Smith at least you could rate as usefull .. (upper arrow)

            – scaisEdge
            Mar 21 at 17:58











          • Thanks again - new to Stack Overflow. Done. :)

            – Stuart K. Smith
            yesterday










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55283890%2fleft-filling-a-text-field-with-zeros%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Why are you using LPAD if the result of your CASE statement is numeric , use simply to_char :



           UPDATE timetable
          SET eta = to_char (
          CASE
          WHEN (trips.starttime::int / 100) + (destination.zuluoffset * -1 ) < 24 THEN ((trips.starttime::int / 100) + (destination.zuluoffset * -1 )) * 100
          WHEN (trips.starttime::int / 100) + (destination.zuluoffset * -1 ) > 23 THEN ((trips.starttime::int / 100) + (destination.zuluoffset * -1 ) - 24) * 100
          END , 'FM0000')
          FROM destination
          INNER JOIN trips ON timetable.tripsid = trips.id;





          share|improve this answer























          • This worked as well as the one above. I selected this one because it seemed "more" correct in that it uses char formatting directly instead of converting num to char. Thanks for the excellent reply.

            – Stuart K. Smith
            Mar 21 at 17:53















          1














          Why are you using LPAD if the result of your CASE statement is numeric , use simply to_char :



           UPDATE timetable
          SET eta = to_char (
          CASE
          WHEN (trips.starttime::int / 100) + (destination.zuluoffset * -1 ) < 24 THEN ((trips.starttime::int / 100) + (destination.zuluoffset * -1 )) * 100
          WHEN (trips.starttime::int / 100) + (destination.zuluoffset * -1 ) > 23 THEN ((trips.starttime::int / 100) + (destination.zuluoffset * -1 ) - 24) * 100
          END , 'FM0000')
          FROM destination
          INNER JOIN trips ON timetable.tripsid = trips.id;





          share|improve this answer























          • This worked as well as the one above. I selected this one because it seemed "more" correct in that it uses char formatting directly instead of converting num to char. Thanks for the excellent reply.

            – Stuart K. Smith
            Mar 21 at 17:53













          1












          1








          1







          Why are you using LPAD if the result of your CASE statement is numeric , use simply to_char :



           UPDATE timetable
          SET eta = to_char (
          CASE
          WHEN (trips.starttime::int / 100) + (destination.zuluoffset * -1 ) < 24 THEN ((trips.starttime::int / 100) + (destination.zuluoffset * -1 )) * 100
          WHEN (trips.starttime::int / 100) + (destination.zuluoffset * -1 ) > 23 THEN ((trips.starttime::int / 100) + (destination.zuluoffset * -1 ) - 24) * 100
          END , 'FM0000')
          FROM destination
          INNER JOIN trips ON timetable.tripsid = trips.id;





          share|improve this answer













          Why are you using LPAD if the result of your CASE statement is numeric , use simply to_char :



           UPDATE timetable
          SET eta = to_char (
          CASE
          WHEN (trips.starttime::int / 100) + (destination.zuluoffset * -1 ) < 24 THEN ((trips.starttime::int / 100) + (destination.zuluoffset * -1 )) * 100
          WHEN (trips.starttime::int / 100) + (destination.zuluoffset * -1 ) > 23 THEN ((trips.starttime::int / 100) + (destination.zuluoffset * -1 ) - 24) * 100
          END , 'FM0000')
          FROM destination
          INNER JOIN trips ON timetable.tripsid = trips.id;






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 21 at 15:56









          Rémy BaronRémy Baron

          879511




          879511












          • This worked as well as the one above. I selected this one because it seemed "more" correct in that it uses char formatting directly instead of converting num to char. Thanks for the excellent reply.

            – Stuart K. Smith
            Mar 21 at 17:53

















          • This worked as well as the one above. I selected this one because it seemed "more" correct in that it uses char formatting directly instead of converting num to char. Thanks for the excellent reply.

            – Stuart K. Smith
            Mar 21 at 17:53
















          This worked as well as the one above. I selected this one because it seemed "more" correct in that it uses char formatting directly instead of converting num to char. Thanks for the excellent reply.

          – Stuart K. Smith
          Mar 21 at 17:53





          This worked as well as the one above. I selected this one because it seemed "more" correct in that it uses char formatting directly instead of converting num to char. Thanks for the excellent reply.

          – Stuart K. Smith
          Mar 21 at 17:53













          1














          should be LPAD(your_col::text, 4, '0')



           UPDATE timetable
          SET eta = LPAD ((
          CASE
          WHEN (trips.starttime::int / 100) + (destination.zuluoffset * -1 ) < 24 THEN ((trips.starttime::int / 100) + (destination.zuluoffset * -1 )) * 100
          WHEN (trips.starttime::int / 100) + (destination.zuluoffset * -1 ) > 23 THEN ((trips.starttime::int / 100) + (destination.zuluoffset * -1 ) - 24) * 100
          END )::text, 4,'0')
          FROM destination
          INNER JOIN trips ON timetable.tripsid = trips.id;





          share|improve this answer























          • This worked as well as the one below. However, I can only select one "correct" reply. Thanks a lot for your reply.

            – Stuart K. Smith
            Mar 21 at 17:53











          • @StuartK.Smith at least you could rate as usefull .. (upper arrow)

            – scaisEdge
            Mar 21 at 17:58











          • Thanks again - new to Stack Overflow. Done. :)

            – Stuart K. Smith
            yesterday















          1














          should be LPAD(your_col::text, 4, '0')



           UPDATE timetable
          SET eta = LPAD ((
          CASE
          WHEN (trips.starttime::int / 100) + (destination.zuluoffset * -1 ) < 24 THEN ((trips.starttime::int / 100) + (destination.zuluoffset * -1 )) * 100
          WHEN (trips.starttime::int / 100) + (destination.zuluoffset * -1 ) > 23 THEN ((trips.starttime::int / 100) + (destination.zuluoffset * -1 ) - 24) * 100
          END )::text, 4,'0')
          FROM destination
          INNER JOIN trips ON timetable.tripsid = trips.id;





          share|improve this answer























          • This worked as well as the one below. However, I can only select one "correct" reply. Thanks a lot for your reply.

            – Stuart K. Smith
            Mar 21 at 17:53











          • @StuartK.Smith at least you could rate as usefull .. (upper arrow)

            – scaisEdge
            Mar 21 at 17:58











          • Thanks again - new to Stack Overflow. Done. :)

            – Stuart K. Smith
            yesterday













          1












          1








          1







          should be LPAD(your_col::text, 4, '0')



           UPDATE timetable
          SET eta = LPAD ((
          CASE
          WHEN (trips.starttime::int / 100) + (destination.zuluoffset * -1 ) < 24 THEN ((trips.starttime::int / 100) + (destination.zuluoffset * -1 )) * 100
          WHEN (trips.starttime::int / 100) + (destination.zuluoffset * -1 ) > 23 THEN ((trips.starttime::int / 100) + (destination.zuluoffset * -1 ) - 24) * 100
          END )::text, 4,'0')
          FROM destination
          INNER JOIN trips ON timetable.tripsid = trips.id;





          share|improve this answer













          should be LPAD(your_col::text, 4, '0')



           UPDATE timetable
          SET eta = LPAD ((
          CASE
          WHEN (trips.starttime::int / 100) + (destination.zuluoffset * -1 ) < 24 THEN ((trips.starttime::int / 100) + (destination.zuluoffset * -1 )) * 100
          WHEN (trips.starttime::int / 100) + (destination.zuluoffset * -1 ) > 23 THEN ((trips.starttime::int / 100) + (destination.zuluoffset * -1 ) - 24) * 100
          END )::text, 4,'0')
          FROM destination
          INNER JOIN trips ON timetable.tripsid = trips.id;






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 21 at 15:38









          scaisEdgescaisEdge

          96.7k105272




          96.7k105272












          • This worked as well as the one below. However, I can only select one "correct" reply. Thanks a lot for your reply.

            – Stuart K. Smith
            Mar 21 at 17:53











          • @StuartK.Smith at least you could rate as usefull .. (upper arrow)

            – scaisEdge
            Mar 21 at 17:58











          • Thanks again - new to Stack Overflow. Done. :)

            – Stuart K. Smith
            yesterday

















          • This worked as well as the one below. However, I can only select one "correct" reply. Thanks a lot for your reply.

            – Stuart K. Smith
            Mar 21 at 17:53











          • @StuartK.Smith at least you could rate as usefull .. (upper arrow)

            – scaisEdge
            Mar 21 at 17:58











          • Thanks again - new to Stack Overflow. Done. :)

            – Stuart K. Smith
            yesterday
















          This worked as well as the one below. However, I can only select one "correct" reply. Thanks a lot for your reply.

          – Stuart K. Smith
          Mar 21 at 17:53





          This worked as well as the one below. However, I can only select one "correct" reply. Thanks a lot for your reply.

          – Stuart K. Smith
          Mar 21 at 17:53













          @StuartK.Smith at least you could rate as usefull .. (upper arrow)

          – scaisEdge
          Mar 21 at 17:58





          @StuartK.Smith at least you could rate as usefull .. (upper arrow)

          – scaisEdge
          Mar 21 at 17:58













          Thanks again - new to Stack Overflow. Done. :)

          – Stuart K. Smith
          yesterday





          Thanks again - new to Stack Overflow. Done. :)

          – Stuart K. Smith
          yesterday

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55283890%2fleft-filling-a-text-field-with-zeros%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

          은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현