Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7877517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:23:40+00:00 2026-06-03T03:23:40+00:00

I am creating a stored procedure in my sql work bench. I am modifying

  • 0

I am creating a stored procedure in my sql work bench. I am modifying the postcode of an address to format ‘dn35 7tg’ With a 7 character postcode even if the spaces are not inserted by the user.

Firstly I cannot find a way of printing the variables to the consol screen, which does not help my situation.

When running the Call statement

CALL usp_addsupplier('Bobs shopping mania', 'dn465th');

I except to find the values entered into the database with the correct formatting, however all I get is the PK, Null and Null entered in the columns.

If some could point me in the right direction I would be great full

Thank you all.

DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `usp_addsupplier`(IN SuppliersName VARCHAR(30), IN SuppliersPostCode VARCHAR(15))

BEGIN


-- Declare the change varables
DECLARE postcode VARCHAR(8) DEFAULT SuppliersPostCode;

SET postcode = SuppliersPostCode;

-- Postcode reformat -----------------------------------------------

-- Change postcode to uppercase

SET postCode = UPPER(postCode);

-- Grabs the first 4 letters and stores them in a new varable

SET new_postcode = LEFT (postCode,4);

-- Adds the space to the first 4 letters

SET new_postcode = new_postcode + ' ';

-- Add the last 3 letters to the new varable

SET new_postcode = new_postcode + RIGHT(postCode,3);

INSERT INTO tblsupplier (SupplierName, SupplierPostCode VALUES (new_name, new_postcode));

END
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:23:41+00:00Added an answer on June 3, 2026 at 3:23 am

    I am not great on MySQL, but there are a couple of errors that jump out at me.

    Firstly, you never set new_name before inserting it, so this will always be null.

    Secondly, I don’t think MySQL likes string concatenation using StringA + StringB, you need to useCONCAT(StringA, StringB)

    Thirdly, There is a syntax error in your insert command (As pointed out by eggyal).

    On a separate note however, your logic will not work to properly format UK Postcodes. For example, M1 1AA is a perfectly valid UK Postcode:

    M1 1AA -->  M1 1 1AA
    M11AA  -->  M11A 1AA
    

    As you can see the “formatted” postcodes are a mess. You can achieve this in a much simpler manner using the INSERT function. UK postcodes are a variable number of characters (2-4) followed by a space followed by 3 characters.

    The first step should be to cleanse the input (assummed to be @PostCode based on my test Fiddle)

    REPLACE(@PostCode, ' ', '');
    

    this will remove all spaces, so it is irrelevant whether the raw input has spaces or not.

    You then need to insert a space 3 characters from the end of the string. To find this position you use:

    CHAR_LENGTH(@PostCode) - 2
    

    This gives an end result of:

    UPPER(INSERT(REPLACE(@PostCode, ' ', ''), CHAR_LENGTH(REPLACE(@PostCode, ' ', '')) - 2, 0, ' ')))
    

    So this can all be done in one call:

    SET @SupplierName = 'Test';
    SET @PostCode = 'M 1 1 A A   ';
    
    INSERT INTO tblSupplier (SupplierName, SupplierPostCode )
    VALUES (@SupplierName, UPPER(INSERT(REPLACE(@PostCode, ' ', ''), CHAR_LENGTH(REPLACE(@PostCode, ' ', '')) - 2, 0, ' ')));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a stored procedure in SQL server and want to achieve the following:
I am creating a stored procedure in SQL Server via SSMS. I have written
I have a sql 2008 database and I am creating a stored procedure that
I am creating a simple stored procedure in VS 2010/SQL Server 2008 as follows:
I am creating a stored procedure in Sql Server 2008 database. I want to
After creating stored procedure in SQL Server why to replace Create with Alter ?
Im creating a Stored Procedure on an 2008 SQL server. I cant find how
I'm creating a report (in Crystal Reports XI) based on a SQL stored procedure
I have a stored procedure in SQL 2005. The Stored Procedure is actually creating
I'm a newbie when it comes to SQL. When creating a stored procedure with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.