Showing posts with label field. Show all posts
Showing posts with label field. Show all posts

Wednesday, March 7, 2012

Closest match SQL query

I have a need to execute a query in T-SQL on a numeric field in a SQL table.

However if there is no exact match I'd likea query that will return the row that is just below my value.

As an example if the table has values: 1,2,4,5,7,9, 15 and 20 for instance and I am matching with a varibale containing the value 9 then I'd like it to return that row. however if my variable has the value 19 I want the row containign 15 returned.

Is this possible? Can anyone help me with such a query?

Regards

Clive

If you use <= in your WHERE clause, and an Order By ... DESC, you will get the results you want. When you use ExecuteScalar(), it will only return the first value in the resultset.|||

Here is one (SQL Server 2005):

SELECT NumFROM(SELECT Num, Row_Number()over(orderby NumDESC)as rNum

FROM yourTable

WHERE num<=19) t

WHERE rNum=1

Another one (should work with 2000):

SELECTTop 1 NumFROM(SELECT Num,(SELECTCOUNT(*)FROM yourTable bWHERE a.Num<=b.Num)as rNum

FROM yourTable a

WHERE Num<=19) t

OrderBY rNum

|||

SELECT TOP 1 *

FROM MyTable

WHERE Num<=19

ORDER BY Num DESC

|||

Thanks for your help. However I'm not doign too well, i've not managed to get these to work. I shoudl clarify that I am useing SQL 2005.

The cloest seems to be:

SELECT TOP 1 *

FROM table

WHERE id <=15

That does indeed return one row however since I can't guarantee the order of the data in the table I need to sort it first I think.

I have tried :

SELECT top 1 *

from (SELECT * from table ORDER BY id)

WHERE ID <=15 but also this fails.

Any help appreciated.

Regards

Clive

|||

Use Motley's code. You will see that it includes an order by clause. You need to Order By id DESC.

SELECT TOP 1 *

FROM table

WHERE id <=15

ORDER BY id DESC

|||

Thanks Mike,

That works perfectly - I was being dumb and putting the ORDER BY clause in the wrong place.

Clive

Saturday, February 25, 2012

Clob fields

Hi folks,
I try to fetch some data which contains a clob field, nut
infortnatly I always get this error message:
[Microsoft][SQLServer 2000 Driver for JDBC]Unsupported data
conversion
Anyone got an idea why and what I could do?
Storing data with my program and creating some clob data
works fine
Thx mates
- MAXX
| Content-Class: urn:content-classes:message
| From: "MAXX" <anonymous@.discussions.microsoft.com>
| Sender: "MAXX" <anonymous@.discussions.microsoft.com>
| Subject: Clob fields
| Date: Wed, 16 Jun 2004 00:46:09 -0700
| Lines: 15
| Message-ID: <1cce401c45375$fd39d000$a601280a@.phx.gbl>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| thread-index: AcRTdf05/QBYPISzRweeDMzhVrkd7g==
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Newsgroups: microsoft.public.sqlserver.jdbcdriver
| Path: cpmsftngxa10.phx.gbl
| Xref: cpmsftngxa10.phx.gbl microsoft.public.sqlserver.jdbcdriver:6108
| NNTP-Posting-Host: tk2msftngxa14.phx.gbl 10.40.1.166
| X-Tomcat-NG: microsoft.public.sqlserver.jdbcdriver
|
| Hi folks,
|
| I try to fetch some data which contains a clob field, nut
| infortnatly I always get this error message:
| [Microsoft][SQLServer 2000 Driver for JDBC]Unsupported data
| conversion
|
| Anyone got an idea why and what I could do?
|
| Storing data with my program and creating some clob data
| works fine
|
| Thx mates
|
| - MAXX
|
Hello,
The Microsoft JDBC driver does not support getBlob/getClob. Unfortunately,
the error message does not accurately reflect this point. We are aware of
this problem and plan to address this sometime in the future. You will
have to use a different JDBC driver in order to retrieve BLOB/CLOB data.
Carb Simien, MCSE MCDBA MCAD
Microsoft Developer Support - Web Data
Please reply only to the newsgroups.
This posting is provided "AS IS" with no warranties, and confers no rights.
Are you secure? For information about the Strategic Technology Protection
Program and to order your FREE Security Tool Kit, please visit
http://www.microsoft.com/security.

CLOB & BLOB with microsoft JDBC driver ?

The microsoft JDBC driver doesn't accept CLOB & BLOB field.
Does anyone know about this problem ?
Is it possible to found another driver that works correctly and that is free ?
Thank'swhich driver u r using ?
is it jdbc-odbc.|||Sorry, I'm talking about TEXT object, and if i use the term BLOB it is for large binary object.
Do you know about my problem ?

Friday, February 10, 2012

Cleartext - > cipher text. Field lengths?

Suppose I store cleartext strings in a field declared as varchar(100). Is there any way to know the minimum varbinary column sze to use for the encrypted data? (e.g. should it be varbinary(100) or (200)?, (8000)?). I'm sure it's algorithm specific but I don't know what factors influence the final length.

TIA,

Barkingdog

For SQL Server 2005 you can find the information in the following article “SQL Server 2005 Encryption – Encryption and data length limitations” (http://blogs.msdn.com/yukondoit/archive/2005/11/24/496521.aspx).

As I described in the article, the formula to estimate the ciphertext length is based on the current implementation overhead (headers, key GUID & padding). I would personally recommend having some extra room (1-2 blocks) in case the implementation changes in future releases or in case your plaintext grows enough overtime to require 1 more block of space.

-Raul Garcia

SDE/T

SQL Server Engine