Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Function to expand tab characters


Function to expand tab characters

Author
Message
Phil Factor
Phil Factor
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1511 Visits: 2968
Comments posted to this topic are about the item Function to expand tab characters


Best wishes,

Phil Factor
Simple Talk
Phil Factor
Phil Factor
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1511 Visits: 2968

I can't quite remember writing this. I think I must have been trying to check out the process for submitting scripts for some reason, and grabbed something I had to hand. Apologies for the awful header, which explains very little. I hurriedly tried it out and to my surprise it seems to work.


DECLARE @TabbedStuff VARCHAR(2000)
SELECT @TabbedStuff='
'
+CHAR(9)+ 'SELECT @tabsize = COALESCE(@tabsize, 4)
'
+CHAR(9)+ 'IF @string IS NULL RETURN NULL
'
+CHAR(9)+ 'DECLARE @OriginalString VARCHAR(8000),
'
+CHAR(9)+CHAR(9)+'@DetabbifiedString VARCHAR(8000), @Column INT, @Newline INT
  '
+CHAR(9)+ 'SELECT @OriginalString = @String, @DeTabbifiedString = '', @NewLine = 1,
'
+CHAR(9)+CHAR(9)+'@Column = 1
'
+CHAR(9)+'WHILE PATINDEX(''%['' + CHAR(9) + CHAR(10) + '']%'', @OriginalString) > 0'


SELECT dbo.expandTabs(@TabbedStuff,3)

The code comes from the Code Prettifier, of course, so if I extracted it properly, then I guess it works. I've updated the Prettifier since then but not this logic as it seems to work OK. Unfortunately, I haven't updated the version of the prettifier on http://extras.sqlservercentral.com/prettifier/default.aspx but I will as soon as I get a quiet week (nervous laugh)




Best wishes,

Phil Factor
Simple Talk
William Soranno
William Soranno
SSC-Enthusiastic
SSC-Enthusiastic (158 reputation)SSC-Enthusiastic (158 reputation)SSC-Enthusiastic (158 reputation)SSC-Enthusiastic (158 reputation)SSC-Enthusiastic (158 reputation)SSC-Enthusiastic (158 reputation)SSC-Enthusiastic (158 reputation)SSC-Enthusiastic (158 reputation)

Group: General Forum Members
Points: 158 Visits: 518
Phil,
Here is a much simpler function.

ALTER FUNCTION [dbo].[fnConvertTabToSpaces]
(
@String VARCHAR(8000)
,@NumSpaces INT
)
RETURNS VARCHAR(8000)
AS
BEGIN
-- Declare the return variable here
DECLARE
@Result VARCHAR(8000)
,@tab CHAR(1) = CHAR(09)

SELECT
@Result = REPLACE(@String, @tab, SPACE(@NumSpaces))

RETURN @Result

END



Bill Soranno
MCP, MCTS, MCITP DBA
Database Administrator
Winona State University
Maxwell 143

"Quality, like Success, is a Journey, not a Destination" - William Soranno '92
Phil Factor
Phil Factor
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1511 Visits: 2968
Great, but it does a much simpler operation and certainly doesn't de-tabbify. In code, you are likely to get a mixture of spaces and tabs. The tab character actually is a control character that, in a mono-spaced font, means 'take me to the next TAB setting. It does not mean 'I'm eight spaces'(or whatever). TABS may be set to every eighth character position (or whatever) and so your code will work until the point that it finds a mixture of spaces, tabs and other control characters. There is a subtle difference as you'll find out when you try your routine on code that has both tabs and spaces in it. Why does code have both tabs and spaces? Hell, the coder uses both, and just whacks either the space-bar or the TAB key until columns line up. Programmers get irritated if their columns stop lining up, which is what will happen if they use your code.


Best wishes,

Phil Factor
Simple Talk
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search