SQL Server Performance Forum – Threads Archive
tsql-Query
HiI have a column where it can have 10 digits numerical value, when a user enter only 5 digits instead of 10 from his application, i want to display 0’s for the remaining places where vlaue is not entered, How can I write a query for that? Thanks!
"He laughs best who laughs last"
Something like this?
DECLARE @MeineZahl INT
SET @MeineZahl = 12345
SELECT LEFT(CAST(@MeineZahl AS VARCHAR(10)) + REPLICATE(‘0’,10) ,10) ———-
1234500000 (1 row(s) affected) —
Frank Kalis
Microsoft SQL Server MVP
http://www.insidesql.de
Heute schon gebloggt?http://www.insidesql.de/blogs
Ich unterstuetze PASS Deutschland e.V. http://www.sqlpass.de)
If possible do this in front end application Madhivanan Failing to plan is Planning to fail
an update..how can i do this if i need 0’s 1st and number later like 00001234 Thanks!
"He laughs best who laughs last"
You are aware that if the column is of a numeric data type, and you want to store the leading zeroes, the leading zeroes will not "hold"? If your column is already of a character type, you can use either one of these: RIGHT(‘0000000000’ + column, 10) REPLICATE(‘0’, 10 – LEN(column)) + column If the column is numeric, then the first one is probably easier on the brain: RIGHT(‘0000000000’ + CAST(column AS VARCHAR(10)), 10)
DECLARE @MeineZahl INT<br />SET @MeineZahl = 12345<br />SELECT replace(str(@MeineZahl,10),’ ‘,’0′)<br /><br />But this is front end issue [<img src=’/community/emoticons/emotion-5.gif’ alt=’

Madhivanan, I agree it might be a front-end issue, but it might also be a data conversion issue.
to ensure domain integrity (i.e. incase you perform matching), enforce this rule either through your application or trigger. If you can’t touch the application, use the trigger using the code suggested by Adriaan.<br /><br />FrankKalis, being picky on the 2+2+2+2+2, well, i think this one is leading zeroes not trailing zeroes <img src=’/community/emoticons/emotion-5.gif’ alt=’

<blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote"><i>Originally posted by Adriaan</i><br /><br />Madhivanan,<br /><br />I agree it might be a front-end issue, but it might also be a data conversion issue.<br /><hr height="1" noshade id="quote"></font id="quote"></blockquote id="quote"><br />Yes. But when I ask "Where do you want to show the data?", mostly the reply is "Front end application or Reports" [<img src=’/community/emoticons/emotion-1.gif’ alt=’

<blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote"><br />FrankKalis, being picky on the 2+2+2+2+2, well, i think this one is leading zeroes not trailing zeroes <img src=’/community/emoticons/emotion-5.gif’ alt=’


>>FrankKalis, being picky on the 2+2+2+2+2, well, i think this one is leading zeroes not trailing zeroes <img src=’/community/emoticons/emotion-5.gif’ alt=’

Take a look here:http://www.sql-server-performance.com/forum/topic.asp?TOPIC_ID=14288&whichpage=2 —
Frank Kalis
Microsoft SQL Server MVP
http://www.insidesql.de
Heute schon gebloggt?http://www.insidesql.de/blogs
Ich unterstuetze PASS Deutschland e.V. http://www.sqlpass.de)
I am just teasing you back Frank <img src=’/community/emoticons/emotion-1.gif’ alt=’


I know. [<img src=’/community/emoticons/emotion-1.gif’ alt=’


]]>