?
Solved

Add a 0 padding to an id number less then 6

Posted on 2013-01-28
3
Medium Priority
?
294 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

777 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