Solved

# Bussiness Logic Need in SQL Statement

Posted on 2010-08-24
198 Views
Last Modified: 2012-05-10
Hello Experts,

I'm working on Reconciliation Report, where I'm missing Logic:

1. I need to Check the Record exists in given  Date Range, if no records exists. I need to search beyond another nearest date range from  the table datetime field.

0
Question by:ASPDEV
• 4
• 2
9 Comments

LVL 16

Accepted Solution

vdr1620 earned 500 total points
ID: 33512090
Try using
IF (SELECT COUNt(*) FROM TableName WHERE Date between '08-20-2010' and '08-21-2010')  > 1
BEGIN
SELECT Statement
END
ELSE
BEGIN
SELECT Statement
END

OR
you can use IF EXISTS as in the Link
http://articles.techrepublic.com.com/5100-10878_11-6180272.html

OR
you Can use CASE WHEN in where Clause of select statement depending on the requirement
0

LVL 5

Expert Comment

ID: 33512103
you could use the exists function in an If, or Join

Exists(
SELECT X FROM X WHERE X)
0

Author Comment

ID: 33513361
This is my requirement, I need to get the closet datetime value if the given date ranges doesn't get any results.

The datetime value from the table must be close eniough to get me the results.

e.g

Quantity

ID  datetime        Qty
1   08/02/2010    100
2   08/06/2010    300
3   08/21/2010    400

If user has give date = 08/07/2010  it should check the nearest datetime(Can be greater or lesser) so that it can find results.

0

LVL 5

Expert Comment

ID: 33513445
See snippet
``````declare @userDate datetime

select @userDate = GetDate()

select top 1 id

from yourtablename

order by abs(datediff(n, [datetime], @userDate))
``````
0

LVL 5

Expert Comment

ID: 33513463
Just noticed table name
``````declare @userDate datetime

select @userDate = GetDate()

select top 1 *

from Quantity

order by abs(datediff(n, [datetime], @userDate))
``````
0

LVL 5

Expert Comment

ID: 33773997
Provided resolution to question.
0

Author Closing Comment

ID: 33834980
Thanks
0

## Join & Write a Comment Already a member? Login.

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be \$37.1B.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

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

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

#### Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!