Solved

Add a 0 padding to an id number less then 6

Posted on 2013-01-28
3
287 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

705 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

13 Experts available now in Live!

Get 1:1 Help Now