473,387 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

SQL Select Command - Partial Variable String

I'll start off by saying I'm not very experienced in writing SQL commands. I am familiar with vbscript though, and I can do a similar thing in that, but can't seem to get it to work in SQL.

What I need is to use Excel to connect to an SQL database and pull down information periodically, which I've been able to mostly do successfully. The problem now is that the information in some of the fields has extra information that makes it hard to read for the end user that needs this information. For example, the raw data in one field is "1234567.8901-1^101010-1111^L", but I want to configure Microsoft Query via the SQL Select command to cut out part of that string and only show the "101010-1111". The other thing is that this string is not always 11 characters as in this example. It might be only 10 sometimes too. I have managed to get it to cut the first part, but have been unsuccessful in trimming parts off both the beginning and the end.

Here is what I have for the command that pulls this specific column of data:

SUBSTRING(accession.id,CHARINDEX('^',accession.id) +1, (CHARINDEX('L',accession.id)-1) - (CHARINDEX('^',accession.id) +1)) AS Accession

Now I have done something similar to this in vbscript, where I calculated the length for each entry and it would determine the correct number of characters to return. When I run it with this command to return it to Excel, it gives me the error "[Microsoft][ODBC SQL Server Driver][SQL Server]Invalid length parameter passed to the substring function". This happens when I right click on the Query that I have saved as a .dqy file and choose "Open in Microsoft Office Excel". If I choose "Edit With Microsoft Query", it will actually open the tables and the query and the results in the column show the correct data that I want. I just can't get it to where I can open that in Excel.

Any ideas anyone?
Apr 9 '07 #1
6 10810
iburyak
1,017 Expert 512MB
Did you try to use this where condition ?

[PHP]where accession.id like '%^%^L"[/PHP]

It looks like that some of the records don't find something in a string and you get something like this:

[PHP]SUBSTRING(accession.id, 0, -1) [/PHP]

and this is why server complains.
Apr 9 '07 #2
iburyak
1,017 Expert 512MB
Try to do this:


[PHP]Select accession.id, CHARINDEX('^',accession.id) + 1 Start, (CHARINDEX('L',accession.id)-1) - (CHARINDEX('^',accession.id) +1) length
from table_name
order by 3[/PHP]

See if you'll get some answers to your questions.

Good Luck.
Apr 9 '07 #3
I tried to implement what you suggested, but it gave me an error stating "Line 1: Incorrect syntax near 'Start'." Here is what my entire query command looks like after incorporating your suggestion when I right click on the .dqy file and do Edit with Notepad. Hopefully this will help you understand what I'm trying to do. As I said, I don't know much about the SQL querying...this is just what was there by default after setting up the query in Excel and then what I've changed for the one accession.id column but have been unsuccessful in limiting the string. Also, the accession.id column ALWAYS has data, and is always in the format that I mentioned. It won't vary other than the string length.

Expand|Select|Wrap|Line Numbers
  1. XLODBC
  2. 1
  3. DRIVER=SQL Server;SERVER=server;UID=user1;APP=Microsoft Office 2003;WSID=myworkstation;DATABASE=DB1;Trusted_Connection=Yes
  4. SELECT accession.complete_date, SUBSTRING(accession.id, CHARINDEX('^',accession.id) + 1 Start, (CHARINDEX('L',accession.id)-1) - (CHARINDEX('^',accession.id) +1) length) AS Accession, patient.ssn, patient.last, patient.first, procedures.description, report_to_user.user_id  FROM DB1.dbo.accession accession, DB1.dbo.encounter encounter, DB1.dbo.patient patient, DB1.dbo.procedures procedures, DB1.dbo.report_to_user report_to_user  WHERE encounter.patient_id = patient.id AND report_to_user.report_id = accession.report_id AND procedures.procedure_code = accession.procedure_code AND accession.encounter_id = encounter.id  ORDER BY accession.complete_date
  5.  
  6.  
  7. complete_date    id    ssn    last    first    description    user_id
  8.  
Try to do this:


[PHP]Select accession.id, CHARINDEX('^',accession.id) + 1 Start, (CHARINDEX('L',accession.id)-1) - (CHARINDEX('^',accession.id) +1) length
from table_name
order by 3[/PHP]

See if you'll get some answers to your questions.

Good Luck.
Apr 9 '07 #4
iburyak
1,017 Expert 512MB
What I suggested is not to incorporate into your query in no way.
It is just to overview your data and make sure all records have valid pattern and both [^] and [^L] are present in each record.
I thought if you would be able overview it you would be able to see that data does have invalid records and it is a reason your query doesn't work.

Your original query looked correct just it looks like data is not always good to find a substring.

Also I suggested to use where condition to eliminate records with data that doesn’t satisfy this pattern.

[PHP] where accession.id like '%^%^L" [/PHP]
Apr 9 '07 #5
iburyak,

I'm sorry I misunderstood what you were telling me. I put my original SELECT statement back in, and added your WHERE command in and it worked! That made me very curious, since I couldn't see why it would make a difference. The sample data I was looking at was over 37,000 rows. I did a more in depth check of the accession.id column and I did find a row that had "temporary <date>" in it. I was wrong in my statement that it will always have that data format in that column.

Thank you very much for your help!

What I suggested is not to incorporate into your query in no way.
It is just to overview your data and make sure all records have valid pattern and both [^] and [^L] are present in each record.
I thought if you would be able overview it you would be able to see that data does have invalid records and it is a reason your query doesn't work.

Your original query looked correct just it looks like data is not always good to find a substring.

Also I suggested to use where condition to eliminate records with data that doesn’t satisfy this pattern.

[PHP] where accession.id like '%^%^L" [/PHP]
Apr 9 '07 #6
iburyak
1,017 Expert 512MB
This is why I suggested statement below so you would be able to overview invalid values in a column. Start and end it is what you were about to pass to Substring function and it couldn't handle numbers less then 1.

[PHP]Select accession.id, CHARINDEX('^',accession.id) + 1 Start, (CHARINDEX('L',accession.id)-1) - (CHARINDEX('^',accession.id) +1) length
from table_name
order by 3 [/PHP]

It is common problem and I just know this from experience that data is not always perfect... :)


I am glad it helped.
Good Luck.
Apr 9 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: rg | last post by:
Hello, I use invisionboard on a bid web site (20 000 visitors each day) But my server has high load average. Could you help me to optimize these select command. 1) SELECT p.*,...
2
by: Kevin | last post by:
Hi All, I am new to MS SQL Server.I am using MS SQL 2000.I have a problem in creating a table by using Select command.I have table called "test" and i want to create another table with the same...
1
by: Kevin | last post by:
Hi All I have two questions, if someone can help me or point me in the right direction. 1. I have an OleDbDataadapter, OledbConnection and DataSet. Now the select command for my DataAdapter...
1
by: RBirney | last post by:
I am trying to set the 'select command' in the code rather than have it permanently set. I am using an Oracle data adapter and connection (don't know if this makes a difference) and i have the...
1
by: toby989 | last post by:
Hi Does any concise-conscious programmer (spss) know how to shorten that code preferably including the execute statement in the select command. Thanks. Toby
1
by: misiek | last post by:
Hi all. I have a problem with setting value of some parameter in select command in ObjectDataSource. This is what I did: 1. I created DataSet in designer using some stored procedure, which...
1
by: Tammy B | last post by:
Hi! I have the following select command: SelectCommand="SELECT , , , , , , , from WHERE ( = @txtAddress1)" I would like to be able to use a LIKE operator rather than EQUALS I can't...
2
by: Birky | last post by:
Assign the output of a select to a variable? I am unable to find a way to assign the results of an SQL statement to a variable. I know how to assign the SQL statement to a variable but again no...
2
by: =?Utf-8?B?U2FpbXZw?= | last post by:
Hi and Hello. Good Day. I have a problem in retrieving time in database. I have Table name "SAMPLE" and have field name "DATES" DATES -------------------------------------| 5/9/2008...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.