Solved

Add a 0 padding to an id number less then 6

Posted on 2013-01-28
3
288 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
  • 2
3 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 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 65

Expert Comment

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

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

919 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now