Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CASE STATEMENT 1

Status
Not open for further replies.

CompCodeBoy

Programmer
Aug 18, 2005
33
US
I have an input parameter in a stored procedure "@TicketNumber BigInt". I need to check the length of the parameter. If the length is 18, then all is fine. If the length is 17 then I need to append a '0' to the beginning of the parameter. If the length is 16, then append '00' and so on ... What is the best way of doing this? I'm thinking of using CASE statement but a CASE STATEMENT in T-SQL has to be included within a SELECT STATEMENT. Any help or ideas would be appreciated!
 
You cannot pad a number with 0's. You will need to convert this to varchar instead. Something like this...

Code:
Create Procedure Blah(@TicketNumber BigInt)
As
SET NOCOUNT ON

Declare @StringTicketNumber VarChar(18)

Set @StringTicketNumber = Right(Replicate('0', 18) + Convert(VarChar(18), @TicketNumber), 18)

Then, wherever in your code you are using @TicketNumber, change it to @StringTicketNumber.

Make sense?

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top