|
|
|
|
|
|
Re: Start value of sequence 00001 [message #336843 is a reply to message #336828] |
Tue, 29 July 2008 03:32 |
naikjigar
Messages: 51 Registered: July 2008 Location: India
|
Member |
|
|
How am i going to use TO_CHAR
if my seq.nextval gives me 10 than my total length will be 21 digits. n so on. 22.. 23
i can not even measure the length of the seq's returned value, and append conditional 0
SELECT '2' || TO_CHAR(SYSDATE,'YYYYMMDDHHMISS') || '0000' || TO_CHAR(SEQ_OUTBOUND.NEXTVAL) AS PrimaryKey FROM DUAL
PRIMARYKEY
------------------------
22008072901523100009 //Length is 20 fine.
SQL> /
PRIMARYKEY
------------------------
220080729015233000010 //Length is 21 bad.
SQL>
[Updated on: Tue, 29 July 2008 03:33] Report message to a moderator
|
|
|
Re: Start value of sequence 00001 [message #336845 is a reply to message #336843] |
Tue, 29 July 2008 03:34 |
ThomasG
Messages: 3212 Registered: April 2005 Location: Heilbronn, Germany
|
Senior Member |
|
|
Use it the way Frank posted it :
Quote: |
Use to_char(your_num, '00000') to add leading zeroes.
|
(That will also add an additional leading blank, though, try the format model 'fm00000' instead if you don't want that.
|
|
|
|
|
|
Re: Start value of sequence 00001 [message #336853 is a reply to message #336848] |
Tue, 29 July 2008 04:13 |
Frank
Messages: 7901 Registered: March 2000
|
Senior Member |
|
|
Very well, suit yourself.
Use nonsensical functions to achieve what can be done by using the one and only built-in function that was MADE for doing that.
LPAD is supposed to be used on strings, not on numbers.
I have an even better advise: Split the contents into separate columns. That will save you a lot of headaches later on. (Trust me!)
|
|
|
|
|
|
|
|
Re: Start value of sequence 00001 [message #336878 is a reply to message #336853] |
Tue, 29 July 2008 04:59 |
sarwagya
Messages: 87 Registered: February 2008 Location: Republic of Nepal
|
Member |
|
|
Frank wrote on Tue, 29 July 2008 14:58 |
Use nonsensical functions to achieve what can be done by using the one and only built-in function that was MADE for doing that.
LPAD is supposed to be used on strings, not on numbers.
|
Please tell us the disadvantage of using LPAD in this case.
|
|
|
Re: Start value of sequence 00001 [message #336883 is a reply to message #336878] |
Tue, 29 July 2008 05:10 |
ThomasG
Messages: 3212 Registered: April 2005 Location: Heilbronn, Germany
|
Senior Member |
|
|
Quote: | Please tell us the disadvantage of using LPAD in this case.
|
One disadvantage is that you have:
- Executed one implicit conversion from number to char
- Executed one LPAD function.
When you just do an
to_char(SEQ_OUTBOUND.NEXTVAL,'fm00000')
you only have executed one explicit conversion from number to char.
So you have only half the number of function calls running on the DB.
Another disadvantage is that you might get wrap-around duplicate values some day. Consider the difference in results:
SELECT LPad(4444,2,'0') FROM dual;
SELECT to_char(4444,'fm00') FROM dual;
[Updated on: Tue, 29 July 2008 05:14] Report message to a moderator
|
|
|
Re: Start value of sequence 00001 [message #336887 is a reply to message #336878] |
Tue, 29 July 2008 05:14 |
Frank
Messages: 7901 Registered: March 2000
|
Senior Member |
|
|
sarwagya wrote on Tue, 29 July 2008 11:59 | Frank wrote on Tue, 29 July 2008 14:58 |
Use nonsensical functions to achieve what can be done by using the one and only built-in function that was MADE for doing that.
LPAD is supposed to be used on strings, not on numbers.
|
Please tell us the disadvantage of using LPAD in this case.
|
To use LPAD properly, you should first convert your number to a string.
Guess what function you would use for that..
|
|
|
|
Re: Start value of sequence 00001 [message #337065 is a reply to message #336828] |
Tue, 29 July 2008 16:37 |
Bill B
Messages: 1971 Registered: December 2004
|
Senior Member |
|
|
MAKE YOUR SEQUENCE USING THE FOLLOWING COMMAND
CREATE SEQUENCE SEQ_OUTBOUND
INCREMENT BY 1
START WITH 1
MAXVALUE 99999
MINVALUE 1
NOCACHE
CYCLE;
And insert your primary key during the insert, not by making the key first.
insert into my_table(key,col1,col2)
values('2'||TO_CHAR(SYSDATE,'YYYYMMDDHHMISS')||to_char(SEQ_OUTBOUND.NEXTVAL,'fm0000'),junk1,junk2);
|
|
|