Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Add a 0 padding to an id number less then 6

Posted on 2013-01-28
3
Medium Priority
?
295 Views
Last Modified: 2013-01-28
I have a Query that I export as a .udb file that is uploaded to our food service software.
The query is working ok now but I am getting errors on the upload because a few of the students have old ID numbers less then 6 digits and the sofwtare requires students with less then 6 digit card numbers to be padded with 0's.

Here is the query I have, I need to know how to pad the ID_NUM with a 0
SET NOCOUNT ON

SELECT LTRIM('!Version: 4.2') + CHAR(10) +
'!RecordCount:'+
cast((SELECT count(DISTINCT NAME_MASTER.ID_NUM)
FROM         NAME_MASTER INNER JOIN
                      STUDENT_MASTER ON NAME_MASTER.ID_NUM = STUDENT_MASTER.ID_NUM INNER JOIN
                      SUBSID_MASTER ON NAME_MASTER.ID_NUM = SUBSID_MASTER.ID_NUM INNER JOIN
                      STUDENT_CRS_HIST ON STUDENT_MASTER.ID_NUM = STUDENT_CRS_HIST.ID_NUM
WHERE    ((SUBSID_MASTER.UDEF_2A_1 = 'F') AND (STUDENT_CRS_HIST.TRM_CDE = 'SP') AND (STUDENT_CRS_HIST.YR_CDE = '2012') AND 
                      (STUDENT_MASTER.ROOM_TYPE = '1S')) OR
                      ((SUBSID_MASTER.UDEF_2A_1 = 'F') AND (STUDENT_CRS_HIST.TRM_CDE = 'SP') AND (STUDENT_CRS_HIST.YR_CDE = '2012') AND 
                      (STUDENT_MASTER.ROOM_TYPE = '2S')) OR
                      ((SUBSID_MASTER.UDEF_2A_1 = 'F') AND (STUDENT_CRS_HIST.TRM_CDE = 'SP') AND (STUDENT_CRS_HIST.YR_CDE = '2012') AND 
                      (STUDENT_MASTER.ROOM_TYPE = '3S')) OR
                      ((SUBSID_MASTER.UDEF_2A_1 = 'F') AND (STUDENT_CRS_HIST.TRM_CDE = 'SP') AND (STUDENT_CRS_HIST.YR_CDE = '2012') AND 
                      (STUDENT_MASTER.ROOM_TYPE = '4S')) OR
                      ((SUBSID_MASTER.UDEF_2A_1 = 'F') AND (STUDENT_CRS_HIST.TRM_CDE = 'SP') AND (STUDENT_CRS_HIST.YR_CDE = '2012') AND 
                      (STUDENT_MASTER.ROOM_TYPE = '5S')) OR
                      ((SUBSID_MASTER.UDEF_2A_1 = 'F') AND (STUDENT_CRS_HIST.TRM_CDE = 'SP') AND (STUDENT_CRS_HIST.YR_CDE = '2012') AND 
                      (STUDENT_MASTER.ROOM_TYPE = '6S'))
                      ) AS varchar)
 +CHAR(10)+
'!BeginFormat' + CHAR(10) +
 '!Odyssey_PCS,' + '      ' + '1,' + '      ' + '6,' + '      ' + '"IDNUMBER"' +CHAR(10)+
'!Odyssey_PCS,' +  '      ' + '1,'+ '      ' + '6,'+ '      ' + '"CARDNUMBER"' +CHAR(10)+
'!Odyssey_PCS,' + '      ' + '14,' + '      ' + '20,' + '      ' + '"LASTNAME"' +CHAR(10)+
'!Odyssey_PCS,' + '      ' + '34,' + '      ' + '21,' + '      ' + '"FIRSTNAME"' +CHAR(10)+
'!EndFormat'+ CHAR(10) +
RTRIM('!BeginData')

SELECT DISTINCT LTRIM(CAST(NAME_MASTER.ID_NUM AS CHAR(14))) + CAST(NAME_MASTER.LAST_NAME AS CHAR(22)) + RTRIM(NAME_MASTER.FIRST_NAME) 
FROM         NAME_MASTER INNER JOIN
                      STUDENT_MASTER ON NAME_MASTER.ID_NUM = STUDENT_MASTER.ID_NUM INNER JOIN
                      SUBSID_MASTER ON NAME_MASTER.ID_NUM = SUBSID_MASTER.ID_NUM INNER JOIN
                      STUDENT_CRS_HIST ON STUDENT_MASTER.ID_NUM = STUDENT_CRS_HIST.ID_NUM
WHERE     ((SUBSID_MASTER.UDEF_2A_1 = 'F') AND (STUDENT_CRS_HIST.TRM_CDE = 'SP') AND (STUDENT_CRS_HIST.YR_CDE = '2012') AND 
                      (STUDENT_MASTER.ROOM_TYPE = '1S')) OR
                      ((SUBSID_MASTER.UDEF_2A_1 = 'F') AND (STUDENT_CRS_HIST.TRM_CDE = 'SP') AND (STUDENT_CRS_HIST.YR_CDE = '2012') AND 
                      (STUDENT_MASTER.ROOM_TYPE = '2S')) OR
                      ((SUBSID_MASTER.UDEF_2A_1 = 'F') AND (STUDENT_CRS_HIST.TRM_CDE = 'SP') AND (STUDENT_CRS_HIST.YR_CDE = '2012') AND 
                      (STUDENT_MASTER.ROOM_TYPE = '3S')) OR
                      ((SUBSID_MASTER.UDEF_2A_1 = 'F') AND (STUDENT_CRS_HIST.TRM_CDE = 'SP') AND (STUDENT_CRS_HIST.YR_CDE = '2012') AND 
                      (STUDENT_MASTER.ROOM_TYPE = '4S')) OR
                      ((SUBSID_MASTER.UDEF_2A_1 = 'F') AND (STUDENT_CRS_HIST.TRM_CDE = 'SP') AND (STUDENT_CRS_HIST.YR_CDE = '2012') AND 
                      (STUDENT_MASTER.ROOM_TYPE = '5S')) OR
                      ((SUBSID_MASTER.UDEF_2A_1 = 'F') AND (STUDENT_CRS_HIST.TRM_CDE = 'SP') AND (STUDENT_CRS_HIST.YR_CDE = '2012') AND 
                      (STUDENT_MASTER.ROOM_TYPE = '6S'))


SELECT '!EndData' AS ' '

Open in new window

0
Comment
Question by:CCC-Ravens
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 2000 total points
ID: 38828966
You'll have to pass it as text, as numeric formats do not support leading digits.

In T-SQL, copy-paste the below into SSMS to verify that it works, then plug it into your code:

Declare @student_id int = 12345

SELECT RIGHT('000000' + CAST(@student_id as varchar(6)),6)
0
 

Author Closing Comment

by:CCC-Ravens
ID: 38829025
Perfect that worked
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 38829056
Thanks for the grade.  Good luck with your project. -Jim
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question