I have field(nvarchar) in table which has maximum length of 8 characters .My need is that suppose if i have string 45 then sql server itself attach 6 zeroes and write 000000045 in the table.I can do it in procedures but there is any automatic mechanism or anyother better method . Thanks
You could use a trigger ...<br /><br />CREATE TRIGGER MyTrig ON dbo.MyTable<br />FOR INSERT, UPDATE<br />AS<br /><br />IF EXISTS (SELECT * FROM inserted WHERE LEN(MyField) < <img src='/community/emoticons/emotion-11.gif' alt='8)' /><br />BEGIN<br />UPDATE T SET MyField = RIGHT(('0000000' + MyField), <img src='/community/emoticons/emotion-11.gif' alt='8)' /><br />FROM dbo.MyTable T INNER JOIN inserted I<br />ON T.<key> = I.<key> WHERE LEN(I.MyField) < 8<br />END
I think just for this functionality, you should not go for trigger.<br />It is not going to be one time task right? so you better handle this in stored procedure itself.<br /><br />declare @val1 varchar(10)<br />set @val1='45'<br />select right('00000000'+@val1,<img src='/community/emoticons/emotion-11.gif' alt='8)' /><br />
For the records, you can also do something like <br /><pre id="code"><font face="courier" size="2" id="code"><br />SELECT REPLACE(STR(@val1, <img src='/community/emoticons/emotion-11.gif' alt='8)' />, ' ', '0')<br /></font id="code"></pre id="code"><br />But the more interesting question is, why you would want to store such data as you can easily derive that at run-time?<br /><br />--<br />Frank Kalis<br />Moderator<br />Microsoft SQL Server MVP<br />Webmaster:<a target="_blank" href=http://www.insidesql.de>http://www.insidesql.de</a>
This, or this is for some exchange format. [<img src='/community/emoticons/emotion-1.gif' alt='' />]<br /><br />--<br />Frank Kalis<br />Moderator<br />Microsoft SQL Server MVP<br />Webmaster:<a target="_blank" href=http://www.insidesql.de>http://www.insidesql.de</a>
1 You dont need to use nvarchar datatype if you store only numbers 2 Store numbers as such and it is your front end where you should format the data 3 You dont need to automate the process if you consider above methods Madhivanan Failing to plan is Planning to fail
Madhi, do you have an answer-bot for these kind of answers? [<img src='/community/emoticons/emotion-2.gif' alt='' />]<br /><br />--<br />Frank Kalis<br />Moderator<br />Microsoft SQL Server MVP<br />Webmaster:<a target="_blank" href=http://www.insidesql.de>http://www.insidesql.de</a>
<blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote"><i>Originally posted by FrankKalis</i><br /><br />For the records, you can also do something like <br /><pre id="code"><font face="courier" size="2" id="code"><br />SELECT REPLACE(STR(@val1, <img src='/community/emoticons/emotion-11.gif' alt='8)' />, ' ', '0')<br /></font id="code"></pre id="code"><br />But the more interesting question is, why you would want to store such data as you can easily derive that at run-time?<br /><br />--<br />Frank Kalis<br />Moderator<br />Microsoft SQL Server MVP<br />Webmaster:<a target="_blank" href=http://www.insidesql.de>http://www.insidesql.de</a><br /><hr height="1" noshade id="quote"></font id="quote"></blockquote id="quote"><br />This question is asked by my friend .I also don't know the reason behind attaching <br />zeroes .Since i don't know how to do it i posted here .Thanks all of you for your kind <br />help.<br /><br />Thanks<br />d17may
<blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote"><i>Originally posted by FrankKalis</i><br /><br />Madhi, do you have an answer-bot for these kind of answers? [<img src='/community/emoticons/emotion-2.gif' alt='' />]<br /><br />--<br />Frank Kalis<br />Moderator<br />Microsoft SQL Server MVP<br />Webmaster:<a target="_blank" href=http://www.insidesql.de>http://www.insidesql.de</a><br /><hr height="1" noshade id="quote"></font id="quote"></blockquote id="quote"><br />Yes. Now-adays, these type of questions are asked frequently<br />[<img src='/community/emoticons/emotion-2.gif' alt='' />]<br /><br />Madhivanan<br /><br />Failing to plan is Planning to fail
<blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote"><i>Originally posted by FrankKalis</i><br /><br />For the records, you can also do something like <br /><pre id="code"><font face="courier" size="2" id="code"><br />SELECT REPLACE(STR(@val1, <img src='/community/emoticons/emotion-11.gif' alt='8)' />, ' ', '0')<br /></font id="code"></pre id="code"><br />But the more interesting question is, why you would want to store such data as you can easily derive that at run-time?<br /><br />--<br />Frank Kalis<br />Moderator<br />Microsoft SQL Server MVP<br />Webmaster:<a target="_blank" href=http://www.insidesql.de>http://www.insidesql.de</a><br /><hr height="1" noshade id="quote"></font id="quote"></blockquote id="quote"><br />Did you know why this gives strange result without any truncation or Error message?<br />Select Replace(str(33247448,7),' ',0)<br /><br /><br />Madhivanan<br /><br />Failing to plan is Planning to fail
<blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote"><i>Originally posted by FrankKalis</i><br /><br />For the records, you can also do something like <br /><pre id="code"><font face="courier" size="2" id="code"><br />SELECT REPLACE(STR(@val1, <img src='/community/emoticons/emotion-11.gif' alt='8)' />, ' ', '0')<br /></font id="code"></pre id="code"><br />But the more interesting question is, why you would want to store such data as you can easily derive that at run-time?<br /><br />--<br />Frank Kalis<br />Moderator<br />Microsoft SQL Server MVP<br />Webmaster:<a target="_blank" href=http://www.insidesql.de>http://www.insidesql.de</a><br /><hr height="1" noshade id="quote"></font id="quote"></blockquote id="quote"><br />Did you know why this gives strange result without any truncation or Error message?<br />Select Replace(str(33247448,7),' ',0)<br /><br /><br />Madhivanan<br /><br />Failing to plan is Planning to fail
You're trying to push an 8 bytes input expression (that evaluates to an INT value) into a 7 seven bytes character result expression. AS you can see, obviously this doesn't work. By default doesn't SQL Server throw an error message when to try to convert an INT expression into a VARCHAR expression, but rather displays the '*'. -- Frank Kalis Moderator Microsoft SQL Server MVP Webmaster:http://www.insidesql.de
Thanks. I put some more examples in this regard Declare @s varchar(7) set @s=33247448 select @s set @s='33247448' select @s But I always expect "Error Messages" Madhivanan Failing to plan is Planning to fail
Quote from CAST and CONVERT in BOL. [<img src='/community/emoticons/emotion-5.gif' alt='' />]<br /><blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote"><br />When converting character or binary expressions (char, nchar, nvarchar, varchar, binary, or varbinary) to an expression of a different data type, data can be truncated, only partially displayed, or an error is returned because the result is too short to display. Conversions to char, varchar, nchar, nvarchar, binary, and varbinary are truncated, except for the conversions shown in this table.<br /><br />From data type To data type Result <br />int, smallint, or tinyint char * <br /> varchar * <br /> nchar E <br /> nvarchar E <br />money, smallmoney, numeric, decimal, float, or real char E <br /> varchar E <br /> nchar E <br /> nvarchar E <br /><br /><br />* Result length too short to display.<br />E Error returned because result length is too short to display.<br /><br />Microsoft SQL Server guarantees that only roundtrip conversions, conversions that convert a data type from its original data type and back again, will yield the same values from release to release. This example shows such a roundtrip conversion:<br /><br />DECLARE @myval decimal (5, 2)<br />SET @myval = 193.57<br />SELECT CAST(CAST(@myval AS varbinary(20)) AS decimal(10,5))<br />-- Or, using CONVERT<br />SELECT CONVERT(decimal(10,5), CONVERT(varbinary(20), @myval))<br /><hr height="1" noshade id="quote"></font id="quote"></blockquote id="quote"><br /><br />--<br />Frank Kalis<br />Moderator<br />Microsoft SQL Server MVP<br />Webmaster:<a target="_blank" href=http://www.insidesql.de>http://www.insidesql.de</a>
Sorry, the tabular format got lost. [<img src='/community/emoticons/emotion-6.gif' alt='' />]<br />Read about in in BOL. <br />...and don't try too hard to figure out, why things happen the way they do. It's "by design". So, if you can't fix it, feature it. [<img src='/community/emoticons/emotion-5.gif' alt='' />]<br /><br />--<br />Frank Kalis<br />Moderator<br />Microsoft SQL Server MVP<br />Webmaster:<a target="_blank" href=http://www.insidesql.de>http://www.insidesql.de</a>