Populating MS SQL Server Database

EDIT:
I am changing the column data type to Varchar, should offer a job, the answer will be saved

Full story: I get data for a person with an associated temporary number for each person 5 digits long, I process this information, and then send the variables to a stored procedure that processes the insertion of this data. When sending variables to a stored procedure, I seem to lose any prefix 0.
For example:
Number sent to stored number Proc - Number actually inserted column

12345 - 12345  
01234 - 1234  
12340 - 12340  

This only happens for numbers with a 0front. Value, if I received:
00012it will be inserted as12

Is there a way in which I could either update the column always 0pad left to a fixed number, that is, if we got it 12, it would automatically produce a value 00012.
OR
Is there a way to do this with a variable when it is received by the stored procedure before the variable is inserted into the table. Sort of:

SET @zeroPaddedFixedNum = LeftPad(@numberRecieved, '0', 5);  

In addition, I now need to stop any numbers from inserting and updating all current incorrectly obsolete numbers. Any suggestions?

Perhaps this is just my Google ability, which failed, but I tried to find numerous pages.

+4
source share
2 answers

varchar.

Insert into table(col)
select right('00000'+cast(@var as varchar(5)),5)

EDIT:

Update table
set col=right('00000'+cast(col as varchar(5)),5)
where len(col)<5
+5

, VARCHAR(5) ... , . , , SP ( VIEW ).

SELECT REPLACE(STR(YourNumber,5),' ','0');

: , 5 , *****. , ... RIGHT() .

SQL Server 2012 FORMAT()

SELECT FORMAT(YourNumber,'00000')
+3

Source: https://habr.com/ru/post/1663305/


All Articles