Searching for numeric values in a text field / SQL Server

Hi all,

here's a problem that I've been trying to solve for several days: I try to select rows from an MS SQL Server via JDBC that contain a certain numeric value at a certain position in a long varchar field.

I'm using queries like

select * from table where substring(field_1, 37, 7) like'1011234';

Those queries always return an empty ResultSet. If I use the same queries in WinSQL, I get correct results (several rows).

The application code is working in principle; if I search for a text instead of a numeric value, for example select * from table where substring(field_1, 4, 5) like'Paper';

, I get a complete ResultSet.

I've tried many variations (= instead of like, search in the entire field (without substring but withlike '%1011234%'), with or without', and more), but that didn't change anything.

It's also most probably not caused by the JDBC driver; I have tried JTDS first, and then the MS driver (newest versions) - no difference.

One idea was that the sort order is not correct, tried some COLLATE settings, but that didn't help so far. In this context I'd like to know how to determine the collation sequence of a certain column.

Another thing I tried was using CAST, for example SELECT * FROM table WHERE CAST(SUBSTRING(field_1, 37, 7) as bigint) = CAST('1011234' as bigint);

or SELECT * FROM table WHERE CAST(SUBSTRING(field_1, 37, 7) as bigint) = CAST(1011234 as bigint);

, but that didn't help either.

Some facts:

Server: Microsoft SQL Server, version 09.00.2047

Driver in WinSQL: SQLSRV32.DLL, version 03.85.1117

JDBC-Driver:

- JTDS, version 1.2

- Microsoft SQL Server 2005 JDBC Driver, Version 1.2

JVM: jre1.5.0_11

I didn't find anything on Google, or in this forum, or other forums. I really hope someone here can help me. Thanks in advance!

Best regards,

Uica

[2042 byte] By [Uicaa] at [2007-11-27 8:31:53]
# 1

Your note suggests to me that this is a coding problem, not a JDBC or SQL problem unless you are hitting a bug in the SQL Server JDBC driver. Others may have ideas from what you have written, but I think it would be helpful to see the actual code that you are executing that does not bring back the results that you are expecting (don't forgot to use code tags to format your code correctly).

It might also be helpful to execute a query (from within your Java code) to ensure that you are connecting to the right database, accessing the correct table and that your substring is using the correct offset. The query would look something like this (and then of course display the list of results):

SELECT substring(field_1, 37, 7) from table

WorkForFooda at 2007-7-12 20:27:32 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2
Sorry folks, all my mistake. I was bypassing the properties that determined the database instance, and thus read from an older version that didn't have my test cases.
Uicaa at 2007-7-12 20:27:32 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3
> Sorry folks, all my mistake. I was bypassing the> properties that determined the database instance, and> thus read from an older version that didn't have my> test cases.Thanks for saying what the problem was.Glad you got it working!
WorkForFooda at 2007-7-12 20:27:32 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...