Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2008
»
SQL Server 2008 - General
»
Error in max(substring)
16 posts, Page 1 of 2
1
2
»»
Error in max(substring)
Rate Topic
Display Mode
Topic Options
Author
Message
yuvipoy
yuvipoy
Posted Thursday, November 01, 2012 4:27 AM
SSC-Enthusiastic
Group: General Forum Members
Last Login: Today @ 1:06 AM
Points: 111,
Visits: 516
can we add
startid, endid SplitLayout
startid, endid
1,1
2,1
3,1
4,2
6,3
9,1
10,5
15,4
19,3
22,4
Startid = will start from 1 and will add endpoint for second row 1+1 =2 will be starting point for second row 2+1=3 will be starting point for third row 3+1=4 will be starting point for fourth row 4+2=6 will be starting point for fifth row and so on ........
;with t1 as( select '1010' st ),
t2 as( Select startid, endid from SplitLayout),
t3 as ( select st,startid s,Endid e, row_number() over(order by startid) rn from t1,t2)
select * from t3
pivot
(
max(substring(st,s,e)) str for rn in ([1],[2],[3])
) AS pvt
i am getting error near substring
(
open bracket;
Thanks!
Post #1379717
Sean Lange
Sean Lange
Posted Thursday, November 01, 2012 7:40 AM
SSCrazy Eights
Group: General Forum Members
Last Login: Yesterday @ 10:26 PM
Points: 8,606,
Visits: 8,247
ddl, sample data and desired output please. See the first link in my signature.
_______________________________________________________________
Need help? Help us help you.
Read the article at
http://www.sqlservercentral.com/articles/Best+Practices/61537/
for best practices on asking questions.
Need to split a string? Try Jeff Moden's
splitter
.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1379810
ChrisM@Work
ChrisM@Work
Posted Thursday, November 01, 2012 7:53 AM
SSCertifiable
Group: General Forum Members
Last Login: Today @ 1:15 AM
Points: 5,615,
Visits: 10,983
yuvipoy (11/1/2012)
can we add
startid, endid SplitLayout
startid, endid
1,1
2,1
3,1
4,2
6,3
9,1
10,5
15,4
19,3
22,4
Startid = will start from 1 and will add endpoint for second row 1+1 =2 will be starting point for second row 2+1=3 will be starting point for third row 3+1=4 will be starting point for fourth row 4+2=6 will be starting point for fifth row and so on ........
;with t1 as( select '1010' st ),
t2 as( Select startid, endid from SplitLayout),
t3 as ( select st,startid s,Endid e, row_number() over(order by startid) rn from t1,t2)
select * from t3
pivot
(
max(substring(st,s,e)) str for rn in ([1],[2],[3])
) AS pvt
i am getting error near substring
(
open bracket;
Thanks!
Use a delimiter, you know how hard this excercise can become without one.
“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.”
- Gail Shaw
For fast, accurate and documented assistance in answering your questions, please read
this article
.
Understanding and using APPLY, (I)
and
(II)
Paul White
Hidden RBAR: Triangular Joins
/
The "Numbers" or "Tally" Table: What it is and how it replaces a loop
Jeff Moden
Exploring Recursive CTEs by Example
Dwain Camps
Post #1379822
yuvipoy
yuvipoy
Posted Thursday, November 01, 2012 10:31 PM
SSC-Enthusiastic
Group: General Forum Members
Last Login: Today @ 1:06 AM
Points: 111,
Visits: 516
'1010' is the input i need to split the input based on the startid, endid
startid, endid
1,1
2,1
3,1
4,2
6,3
so i need something like
1,0,1,0,11,123,........
need to split the string and insert into the table.
Post #1380135
yuvipoy
yuvipoy
Posted Saturday, November 03, 2012 3:43 AM
SSC-Enthusiastic
Group: General Forum Members
Last Login: Today @ 1:06 AM
Points: 111,
Visits: 516
Any suggestions!
Post #1380692
Jeff Moden
Jeff Moden
Posted Saturday, November 03, 2012 11:41 AM
SSC-Dedicated
Group: General Forum Members
Last Login: Yesterday @ 2:32 PM
Points: 32,906,
Visits: 26,792
yuvipoy (11/3/2012)
Any suggestions!
I can't speak for the others but I don't understand your narrative for this problem.
I recommend that you take a look at the article at the first link in my signature line to post some readily consumable data and try asking the question again. For example, I don't see how the narrative matches up with the data you posted for the following...
startid, endid
1,1
2,1
3,1
4,2
6,3
9,1
10,5
15,4
19,3
22,4
Startid = will start from 1 and will add endpoint for second row 1+1 =2 will be starting point for second row 2+1=3 will be starting point for third row 3+1=4 will be starting point for fourth row 4+2=6 will be starting point for fifth row and so on ........
--Jeff Moden
"
RBAR
is pronounced "ree-bar" and is a "Modenism" for "
R
ow-
B
y-
A
gonizing-
R
ow".
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1380733
yuvipoy
yuvipoy
Posted Sunday, November 04, 2012 10:21 PM
SSC-Enthusiastic
Group: General Forum Members
Last Login: Today @ 1:06 AM
Points: 111,
Visits: 516
Say i'm having a string like this '101011123'
i need to split the sting based on my statid & endid
startid, endid
1,1
2,1
3,1
4,2
6,3
which needs to split something like
1,1---->1 starting position 1 and ending position 1 ,next starting id will be 1+1
2,1---->0 starting position 2 and ending position 1 ,next starting id will be 2+1
3,1---->1 starting position 3 and ending position 1 ,next starting id will be 3+1
4,2---->01 starting position 4 and ending position 2 ,next starting id will be 4+2
6,3---->112 starting position 6 and ending position 1 ,next starting id will be 6+3
9,.
.
.
.
Post #1380873
ChrisM@Work
ChrisM@Work
Posted Monday, November 05, 2012 2:13 AM
SSCertifiable
Group: General Forum Members
Last Login: Today @ 1:15 AM
Points: 5,615,
Visits: 10,983
yuvipoy (11/4/2012)
Say i'm having a string like this '101011123'
i need to split the sting based on my statid & endid
startid, endid
1,1
2,1
3,1
4,2
6,3
which needs to split something like
1,1---->1 starting position 1 and ending position 1 ,next starting id will be 1+1
2,1---->0 starting position 2 and ending position 1 ,next starting id will be 2+1
3,1---->1 starting position 3 and ending position 1 ,next starting id will be 3+1
4,2---->01 starting position 4 and ending position 2 ,next starting id will be 4+2
6,3---->112 starting position 6 and ending position 1 ,next starting id will be 6+3
9,.
.
.
.
That's startposition and length, and it's significantly easier to resolve the elements out of your target string using this than the original method you proposed on another thread. Elements would initially resolve into rows of a column, which leaves you with the same problem as you had on that other thread - the maximum number of columns in a row when you pivot. Have you decided what to do about this?
“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.”
- Gail Shaw
For fast, accurate and documented assistance in answering your questions, please read
this article
.
Understanding and using APPLY, (I)
and
(II)
Paul White
Hidden RBAR: Triangular Joins
/
The "Numbers" or "Tally" Table: What it is and how it replaces a loop
Jeff Moden
Exploring Recursive CTEs by Example
Dwain Camps
Post #1380926
yuvipoy
yuvipoy
Posted Monday, November 05, 2012 5:38 AM
SSC-Enthusiastic
Group: General Forum Members
Last Login: Today @ 1:06 AM
Points: 111,
Visits: 516
No, i am looking alternate method for pivot.
i have used another way like
DECLARE @input varchar(4000);
DECLARE @SQL NVARCHAR(MAX)
select @input='101011123'
SELECT @SQL = ISNULL(@SQL+N','+CHAR(10),'') +
+ N'Col' + RIGHT('0'+CAST(Rid AS NVARCHAR(10)),3) + N'='+''''+CONVERT(varchar(50),
SUBSTRING(@input,(Startid ),(Endid )))+'''' FROM mytable
SELECT @SQL = N'SELECT' + CHAR(10) + @SQL
EXEC (@SQL)
where as it is taking more time ,so looking for alternate way!
Post #1381025
Jeff Moden
Jeff Moden
Posted Monday, November 05, 2012 5:56 AM
SSC-Dedicated
Group: General Forum Members
Last Login: Yesterday @ 2:32 PM
Points: 32,906,
Visits: 26,792
yuvipoy (11/5/2012)
No, i am looking alternate method for pivot.
i have used another way like
DECLARE @input varchar(4000);
DECLARE @SQL NVARCHAR(MAX)
select @input='101011123'
SELECT @SQL = ISNULL(@SQL+N','+CHAR(10),'') +
+ N'Col' + RIGHT('0'+CAST(Rid AS NVARCHAR(10)),3) + N'='+''''+CONVERT(varchar(50),
SUBSTRING(@input,(Startid ),(Endid )))+'''' FROM mytable
SELECT @SQL = N'SELECT' + CHAR(10) + @SQL
EXEC (@SQL)
where as it is taking more time ,so looking for alternate way!
How many different record layouts would you like to handle automatically?
And, no... this actually isn't a pivot problem. It's simply a split of fix field format data. I can help you after I get home from work tonight.
In the meantime, it would be helpful if you could provide some readily consumable test data. Please see the first link in my signature line below for the best way to do that. Thanks.
--Jeff Moden
"
RBAR
is pronounced "ree-bar" and is a "Modenism" for "
R
ow-
B
y-
A
gonizing-
R
ow".
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1381034
« Prev Topic
|
Next Topic »
16 posts, Page 1 of 2
1
2
»»
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.