Solved

Find trailing comma and space in sql query

Posted on 2006-07-06
9
2,122 Views
Last Modified: 2012-05-05
When using this line of code I get an intermittant CF error:

keyword= listGetAt(meta_keywords,1)

CF error was:
"In function ListGetAt(list, index [, delimiters]) the value of index, which is 1, is not a valid index for the list given as a the first argument (this list has 0 elements). Valid indexes are in the range 1 through the number of elements in the list."

Meta_keywords is a comma delimited list of keywords. I believe the error is thrown from a trailing comma at the end of the last meta_keyword, sometimes with an extra space,  such as:

'one, two, three, '

How can I indentify a trailing comma in the sql 2000 database using an sql query? I'm not sure how to construct such an sql query and remove it.

To find the trailing comma:
SELECT  META_KEYWORDS
FROM AREAS
WHERE (META_KEYWORDS LIKE '%,')

To find the trailing space:
SELECT  META_KEYWORDS
FROM AREAS
WHERE (META_KEYWORDS LIKE '% ')

That seems to work but I'd like to be certain. It seems too simple.
0
Comment
Question by:RollinNow
  • 5
  • 4
9 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17049076
RollinNow,
>How can I indentify a trailing comma in the sql 2000 database using an sql query?

SELECT  META_KEYWORDS
FROM AREAS
WHERE (META_KEYWORDS LIKE '%, ')   --- put a space after the comma
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17049118
here is an example how to update

declare @char varchar(2000)
SET @char = 'one, two, three '
SELECT @char =  SUBSTRING(@char, 1, CASE WHEN charindex( ',',RTRIM(@char),len(RTRIM(@char))) > 0 THEN  charindex( ',',RTRIM(@char),len(RTRIM(@char)))-1 ELSE LEN(@char) END)
SELECT @char
0
 

Author Comment

by:RollinNow
ID: 17053040
I didn't understand any of your update example. My fault, not yorus. I wouldn't want to update with something I don't understand. But that's not important. What I wanted to know is if you believe the approach I used was the best to find the trailing comma. If it is then I'll use it to find the space. I know it works but is it the best method for a simple query in enterprise manager?
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17055977
If you have the option to handle this at the front end, then that will be fine, because string operations will need so much resources..

Now on the above query, it wont do anyupdate, it just removes the trailing commas, did you run it on the Query Analyzer ? Check it ?

Now if you want only a search , then your approch is ok. but you need to put a comma after the % sign in your query, as i shown in my first post...
 

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:RollinNow
ID: 17060784
Yes, I understand the space, the comma, etc.

WHERE (META_KEYWORDS LIKE '% ')
WHERE (META_KEYWORDS LIKE '%, ')

But I'm not getting the response I need, which is a real commitment on the method, instead of just an "okay".

> your approch is ok

I know it's okay because it works. What I was hoping from you, or someone, was to tell me if using that query was the best or only approach. Just saying ok doesn't give me anything new for this question. It just leaves me where I was before I asked the question.

So, what do you think? Is the approach, the idea, is it the best method, the only method, both? That's all I need to know. And again, I udnerstand what needs to come after the % character.

I don't need to run an update because I manually removed the trailing commas and spaces and they won't be generated again.

Thanks for your help.
0
 

Author Comment

by:RollinNow
ID: 17084544
I've not heard back from you so thought I'd ask once more:

Is the approach the best method you can think of, or the only method? I need to know how much to trust this method so I can consider the answer. Is the query to find the comma and space a true and trustable method. A yes or no would be good, or a percentage.  Please answer and I'll close this question and give the points.
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 250 total points
ID: 17084629
Sorry for the delayed reply, actually i am getting nearly 150 mails from EE, some i accidently deletes, that's why i couldn't reply you in time

>Is the approach the best method you can think of, or the only method?
The best thing will be to handle this at the front end, so that no unnecessary commas and spaces will passed from the front end. Also remember String operations in sql server are costly in sql server

If you use the like statement like   'LIKE '%, '   as in your query, it wont use any kinds of indexes, it will use a table scan to fetch the records...

>Is the query to find the comma and space a true and trustable method

Yes, since  all the records will be inserteed in this way, it is trustable ..

Its nearly 1.00 am here, i am going to bed now.. In case you have any further queries , post here ; i will reply after 7 hours ..

0
 

Author Comment

by:RollinNow
ID: 17087830
No, that does it. Just wanted to clean up this question, get the answer, and get you the points. And let you get back to sleep. Sweet dreams...
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17087943
thnaks ... now i am in the office
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Suggested Solutions

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now