Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

How to select specific line in select statement from a giv..

 
   Database Help (Home) -> Programming RSS
Next:  Creating parameterized query in SSIS package  
Author Message
nlulla

External


Since: Jul 16, 2008
Posts: 5



(Msg. 1) Posted: Wed Jul 16, 2008 9:56 am
Post subject: How to select specific line in select statement from a give multiline
Archived from groups: microsoft>public>sqlserver>programming (more info?)

TableName : TableX
Column 1 : name (single line)
Column 2 : Comments (multiple line)

In "Column2" i have comments which are all about roughtly 20 lines but
can vary. What i want to do is for example i want to write a select
query which will display both columns but only show line 13 (or
whichever i want) from column2.

How do i do this in one T-SQL statement (I am on sql 2005)

Many thanks

 >> Stay informed about: How to select specific line in select statement from a giv.. 
Back to top
Login to vote
Plamen

External


Since: Jul 14, 2008
Posts: 52



(Msg. 2) Posted: Wed Jul 16, 2008 10:55 am
Post subject: Re: How to select specific line in select statement from a give [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

You can split the comments column to lines and select the line of
interest. Here is one example (the table master..spt_values simulates
auxiliary table with numbers):

CREATE TABLE TableX (
foo_key INT PRIMARY KEY,
foo_name VARCHAR(30),
foo_comment VARCHAR(250));

INSERT INTO TableX
VALUES(1, 'name1', 'line1' + CHAR(13) + CHAR(10) +
'line2' + CHAR(13) + CHAR(10) +
'line3');

SELECT foo_key, foo_name, comment_line_text, comment_line
FROM (
SELECT SUBSTRING(foo_comment, n,
CHARINDEX(CHAR(13) + CHAR(10),
foo_comment + CHAR(13) + CHAR(10), n)
- n) AS
comment_line_text,
n + 1 - LEN(REPLACE(LEFT(foo_comment, n), CHAR(13) +
CHAR(10), '
' )) AS comment_line,
foo_key, foo_name
FROM TableX AS F
CROSS JOIN
(SELECT number
FROM master..spt_values
WHERE type = 'P'
AND number BETWEEN 1 AND 250) AS Nums(n)
WHERE SUBSTRING(CHAR(13) + CHAR(10) + foo_comment, n, 2) = CHAR(13) +
CHAR(10)
AND n < LEN(foo_comment) + 2) AS T
WHERE comment_line = 2;

HTH,

Plamen Ratchev
http://www.SQLStudio.com

 >> Stay informed about: How to select specific line in select statement from a giv.. 
Back to top
Login to vote
nlulla

External


Since: Jul 16, 2008
Posts: 5



(Msg. 3) Posted: Thu Jul 17, 2008 2:33 am
Post subject: Re: How to select specific line in select statement from a give [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Hi Plamen,

thanks for your quick reply, i tried your solution as you posted, and
i gives me following when tried in SQL 2005 DB

1) when i try "WHERE comment_line = 1 " i get one row back with
text " line1"
2) when i try "WHERE comment_line = 2 " i get no rows back
3) when i try "WHERE comment_line = 3 " i get no rows back

I does not give any error messages but somehow the solution only works
for line 1 but not for others.

Regards
nlulla
 >> Stay informed about: How to select specific line in select statement from a giv.. 
Back to top
Login to vote
Plamen Ratchev

External


Since: Jan 10, 2008
Posts: 1007



(Msg. 4) Posted: Thu Jul 17, 2008 9:06 am
Post subject: Re: How to select specific line in select statement from a give multiline text field/column of a table [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Seems the post broke into multiple lines and copy/paste eliminates one
space. Make sure in the line that calculates 'comment_line' there is a space
in the last REPLACE parameter.

n + 1 - LEN(REPLACE(
LEFT(foo_comment, n),
CHAR(13) + CHAR(10),
' ' )) AS comment_line,

HTH,

Plamen Ratchev
http://www.SQLStudio.com
 >> Stay informed about: How to select specific line in select statement from a giv.. 
Back to top
Login to vote
nlulla

External


Since: Jul 16, 2008
Posts: 5



(Msg. 5) Posted: Fri Jul 18, 2008 9:33 am
Post subject: Re: How to select specific line in select statement from a give [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

hi plamen,

it does work now. thanks a lot. but i still have one question, when i
ran the sub select part of your solution, it basically creates 1 row
for every character in the comment, is there a way this can be
narrowed down to just the line rather then for every letter as
otherwise it takes a lot of time if i am going to do this on all rows
in a table.

so basically your example creates below rows in the sub select . Don't
get me wrong, your's is the only solution i have managed to find so
far hence it will surely be useful but if there is any chance we can
reduce the overhead as i will have to run this on good 20K rows.

line1 1 1 name1
ine1 1 1 name1
ne1 1 1 name1
e1 1 1 name1
1 1 1 name1
1 1 name1
line2 3 1 name1
line2 2 1 name1
ine2 2 1 name1
ne2 2 1 name1
e2 2 1 name1
2 2 1 name1
2 1 name1
line3 4 1 name1
line3 3 1 name1
ine3 3 1 name1
ne3 3 1 name1
e3 3 1 name1
3 3 1 name1
4 1 name1

Many thanks
lulla
 >> Stay informed about: How to select specific line in select statement from a giv.. 
Back to top
Login to vote
Plamen Ratchev

External


Since: Jan 10, 2008
Posts: 1007



(Msg. 6) Posted: Fri Jul 18, 2008 12:56 pm
Post subject: Re: How to select specific line in select statement from a give multiline text field/column of a table [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

This is the way this method works. I do not think it will be a problem with
20K, but you can test.

Here are some alternatives in the following article by Erland Sommarskog.
You just need to change the code in the methods to use new line instead of
comma as delimiter for the split.
http://www.sommarskog.se/arrays-in-sql-2005.html

Here is the performance analysis of the methods used:
http://www.sommarskog.se/arrays-in-sql-perftest.html

HTH,

Plamen Ratchev
http://www.SQLStudio.com
 >> Stay informed about: How to select specific line in select statement from a giv.. 
Back to top
Login to vote
nlulla

External


Since: Jul 16, 2008
Posts: 5



(Msg. 7) Posted: Thu Jul 24, 2008 9:52 am
Post subject: Re: How to select specific line in select statement from a give [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Thanks Plamen, very userful link i will go through it in details later
on.

thanks for all your help.

regards
lulla
 >> Stay informed about: How to select specific line in select statement from a giv.. 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
select statement to break a 1000 line table - Hi, Could you help to provide me query that can break a ~1000 line table to four ~250 line file? Is it possible to make it base on the number of files that I pick? Regards

is it possible to select within a select statement - i'm trying to select certain records from a previous select statement here's the following: (Select * FROM tbProduct WHERE StyleN = ImageRef AND LNKorDNL='LNK' AND ImageType='Silhouette') that gives me the records i need first, then want to select..

Help with a Select statement - hi there, for an expample: i've got two tables, with 1 and 3 fields on them. like this Forum.ForumID ForumLanguage.ForumID ForumLanguage.ForumLanguage ForumLanguage.ForumTitle Forum contains all Forum records, ForumLanguage contains for each forum a...

Need help with select statement - Hello, I would like to know if it is possible to create a view from the following 2 tables where each client row has the corresponding group codes (CLIENT_GROUP_CODE) for group id's 1,2 and 3. The objective is to have one row for each client and I don'...

Need Help with Select Statement - I need to write a SQL statement to return only orders that share part ID's with other orders from a database. Example: OrderID PartID 12345 12a 12456 12a 23455 11l The statement would need to return the 12345 and 12456 OrderI...
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]