Click here to Skip to main content
15,887,596 members
Home / Discussions / Database
   

Database

 
GeneralRe: SQL vs. Access and ODBC Pin
-- NA --8-Jul-05 22:25
-- NA --8-Jul-05 22:25 
GeneralRe: SQL vs. Access and ODBC Pin
Anonymous9-Jul-05 7:41
Anonymous9-Jul-05 7:41 
GeneralRe: SQL vs. Access and ODBC Pin
-- NA --9-Jul-05 12:51
-- NA --9-Jul-05 12:51 
GeneralRe: SQL vs. Access and ODBC Pin
-- NA --9-Jul-05 12:50
-- NA --9-Jul-05 12:50 
GeneralDataset Pin
skrishnasarma7-Jul-05 18:07
skrishnasarma7-Jul-05 18:07 
GeneralRe: Dataset Pin
Christian Graus7-Jul-05 18:56
protectorChristian Graus7-Jul-05 18:56 
Generalsql 2005 installation Pin
Anonymous7-Jul-05 17:02
Anonymous7-Jul-05 17:02 
GeneralPrinting Reports Pin
OMalleyW7-Jul-05 8:00
OMalleyW7-Jul-05 8:00 
Hello,
We have several reports on the web that require printing. Now as I am sure all of you know it can be a pain to write all the code required to format the lines correctly so they dont wrap.

There is one report that I am thinking of that has caused me much pain in the past. It prints out all the operations and instructions to create a part. Well the operation descriptions can be quite long so I needed a way to accuratly print and count lines of text. I had written a class that would formatt the information breaking at the correct length for printing but the problem was the ammount of time it was costing me when the report would render. So I thought to my self.. Self.. wouldn't it be great if I had a function that would formatt the information for me so when I pulled it from the database all I had to do was display and count?

So I wrote a little function that does just that:
<br />
---------------------------------------------------------------------------<br />
CREATE  FUNCTION F_FormatRevisionDescForPrint(@Desc VARCHAR(1000), @DisplayLenghOfString INT)<br />
RETURNS VARCHAR(2000)<br />
AS<br />
<br />
BEGIN<br />
 DECLARE<br />
   @CharacterCounter  INT<br />
  ,@DescLen   INT<br />
  ,@WhenToBreak   INT<br />
  ,@FormattedDesc   VARCHAR(2000)<br />
  ,@NewLine   VARCHAR(2)<br />
  ,@Space    VARCHAR(1)<br />
  ,@NextCharacter   VARCHAR(1)<br />
  ,@PreviousCharacter  VARCHAR(1)<br />
  ,@CurrentCharacter  VARCHAR(1)<br />
  ,@NewString   VARCHAR(2000)<br />
  ,@NumberOfCharactersToCut INT<br />
--<br />
SELECT   @CharacterCounter = 0<br />
 ,@DescLen     = LEN(@Desc)<br />
 ,@WhenToBreak      = 0<br />
 ,@FormattedDesc    = ''<br />
 ,@NewString    = ''<br />
 ,@NewLine     = CHAR(13) + CHAR(10)<br />
 ,@Space     = ' '<br />
--<br />
IF (@DescLen > 0) BEGIN<br />
 WHILE @CharacterCounter <= @DescLen BEGIN<br />
  --<br />
  SELECT   @FormattedDesc  = @FormattedDesc + SUBSTRING(@Desc,@CharacterCounter,1)<br />
   ,@CurrentCharacter = SUBSTRING(@Desc,@CharacterCounter,1)<br />
   ,@NextCharacter  = SUBSTRING(@Desc,(@CharacterCounter+1),1)<br />
   ,@PreviousCharacter  = SUBSTRING(@Desc,(@CharacterCounter-1),1)<br />
   ,@WhenToBreak   = @WhenToBreak + 1<br />
  --<br />
  IF (@WhenToBreak = @DisplayLenghOfString) BEGIN<br />
   -- I need to check and make sure that I am not breaking on<br />
   -- a word.<br />
   IF (@CurrentCharacter = @Space) BEGIN<br />
    -- There is a space here so it is ok to break<br />
    -- I know that I am not in the middle of a word<br />
    SELECT   @FormattedDesc  = @FormattedDesc + '<BAR />'<br />
     ,@WhenToBreak   = 0<br />
     ,@CharacterCounter  = @CharacterCounter + 1<br />
    -- <br />
   END ELSE IF (@CurrentCharacter != @Space) BEGIN<br />
    -- houston we have a problem I am somewhere in a word<br />
    -- I have to go back in the string till I find a space.<br />
    -- When I find a space I am going to remove the rest<br />
    -- of the characters from the string.<br />
    -- EX: String: A T<BR>OOL New String: A<BR> The word tool will be cut off.<br />
    -- XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX<br />
    -- reverse the string that way it will be<br />
    -- easier to find the space<br />
    SET @NewString    = REVERSE(@FormattedDesc)<br />
    -- get the first index of a space<br />
    SET @NumberOfCharactersToCut  = PATINDEX('% %',@NewString)-1<br />
    -- now reverse the string again so it is no longer backwords<br />
    SET @NewString    = REVERSE(@NewString)<br />
    --<br />
    -- get the new substring<br />
    SELECT   @FormattedDesc    = '' + SUBSTRING(@NewString,0,(LEN(@NewString)-@NumberOfCharactersToCut)) + '<BAR />'<br />
     ,@WhenToBreak      = 0<br />
     ,@CharacterCounter = @CharacterCounter-@NumberOfCharactersToCut<br />
   END<br />
<br />
  END ELSE BEGIN<br />
   --<br />
   SELECT @CharacterCounter = @CharacterCounter + 1<br />
   --<br />
  END<br />
 END<br />
END ELSE BEGIN<br />
 SET @FormattedDesc = ''<br />
END<br />
<br />
RETURN @FormattedDesc<br />
<br />
END<br />
-------------------------------------------------------------------------<br />


Example of use:
<br />
SELECT REPLACE(LTRIM(RTRIM(dbo.F_FormatRevisionDescForPrint(Routing_Revisions_Edit.[Description],47))),'<BAR />','<BR>') AS Description<br />
FROM MyTable<br />


--------------------------------------------------------------------------


Well any thoughts are welcome.

Thanks

Will
Generalmysql table problem Pin
jetset327-Jul-05 6:57
jetset327-Jul-05 6:57 
GeneralRestore Database Pin
abo el ror7-Jul-05 4:24
abo el ror7-Jul-05 4:24 
GeneralRe: Restore Database Pin
Michael Potter7-Jul-05 9:21
Michael Potter7-Jul-05 9:21 
GeneralRe: Restore Database Pin
Anonymous7-Jul-05 20:36
Anonymous7-Jul-05 20:36 
GeneralProblem inserting record in Access database Pin
shapper7-Jul-05 1:54
shapper7-Jul-05 1:54 
GeneralImporting the data from xls to SQL server table through asp.net Pin
7-Jul-05 1:31
suss7-Jul-05 1:31 
GeneralMonitoring Sql Server2000............... Pin
under28117-Jul-05 1:30
under28117-Jul-05 1:30 
GeneralRe: Monitoring Sql Server2000............... Pin
under28118-Jul-05 1:54
under28118-Jul-05 1:54 
QuestionHow to change Date Format in SQL Server 2000 Pin
imshally817-Jul-05 0:45
imshally817-Jul-05 0:45 
AnswerRe: How to change Date Format in SQL Server 2000 Pin
jonathan157-Jul-05 1:55
jonathan157-Jul-05 1:55 
AnswerRe: How to change Date Format in SQL Server 2000 Pin
Rob Graham7-Jul-05 10:31
Rob Graham7-Jul-05 10:31 
GeneralUpdate More than one Table using ADO in Visual C++ 6.0 under one application Pin
Wisdom20046-Jul-05 22:35
Wisdom20046-Jul-05 22:35 
GeneralInvoking Java API from SQL Stored Procedure Pin
Member 20071356-Jul-05 19:27
Member 20071356-Jul-05 19:27 
GeneralRe: Invoking Java API from SQL Stored Procedure Pin
Rob Graham8-Jul-05 17:39
Rob Graham8-Jul-05 17:39 
GeneralSYNCRONISE MYSQL WITH SQL SERVER Pin
RajithCAlwis6-Jul-05 0:47
RajithCAlwis6-Jul-05 0:47 
Generalstoresql date time related Pin
Member 4472176-Jul-05 0:29
Member 4472176-Jul-05 0:29 
GeneralRe: storesql date time related Pin
Colin Angus Mackay6-Jul-05 0:48
Colin Angus Mackay6-Jul-05 0:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.