|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, September 28, 2010 11:59 PM
Points: 141,
Visits: 69
|
|
How to PRINT below string pattern in T-SQL 2005:
Input: SET @str = 'HELLO WORLD'
Output:
H HE HEL HELL HELLO HELLO HELLO W HELLO WO HELLO WOR HELLO WORL HWLLO WORLD
Thanks in advance.
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Monday, January 21, 2013 11:15 AM
Points: 73,
Visits: 397
|
|
declare @str varchar(20) set @str = 'HELLO WORLD'
select SUBSTRING(@str, 1,n) from Tally t where N<=LEN(@str)
The 'Tally' table is simple table of numbers.... pls look for articles on Tally table here... My Tally Table has only a single column called N.
How To Post
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Monday, December 10, 2012 11:36 PM
Points: 6,
Visits: 241
|
|
Try this one: Declare @str varchar(20),@i as int Set @str='HELLOW WORLD' Set @i=1 While @i <= len(@str) Begin print left(@str,@i) Set @i=@i+1 End
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, September 28, 2010 11:59 PM
Points: 141,
Visits: 69
|
|
| Thanks nil...prob solved!!
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Monday, January 21, 2013 11:15 AM
Points: 73,
Visits: 397
|
|
Part of my point here is to turn the OP to the direction of tally tables also. I purposely have asked him to look at them.
Here is a simple way to create a Tally Table (Shamelessly copied from Jeff's article).
--============================================================================= -- Create and populate a Tally table --=============================================================================
--===== Conditionally drop and create the table/Primary Key IF OBJECT_ID('dbo.Tally') IS NOT NULL DROP TABLE dbo.Tally
CREATE TABLE dbo.Tally (N INT, CONSTRAINT PK_Tally_N PRIMARY KEY CLUSTERED (N))
--===== Create and preset a loop counter
DECLARE @Counter INT SET @Counter = 1
--===== Populate the table using the loop and couner WHILE @Counter <= 11000 BEGIN INSERT INTO dbo.Tally (N) VALUES (@Counter) SET @Counter = @Counter + 1 END
http://www.sqlservercentral.com/articles/TSQL/62867/
How To Post
|
|
|
|