Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Bussiness Logic Need in SQL Statement

Posted on 2010-08-24
9
Medium Priority
?
227 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
Comment
Question by:ASPDEV
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
9 Comments
 
LVL 16

Accepted Solution

by:
vdr1620 earned 2000 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

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

Exists(      
SELECT X FROM X WHERE X)
0
 

Author Comment

by:ASPDEV
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 5

Expert Comment

by:almander
ID: 33513445
See snippet
declare @userDate datetime
select @userDate = GetDate()

select top 1 id 
from yourtablename 
order by abs(datediff(n, [datetime], @userDate))

Open in new window

0
 
LVL 5

Expert Comment

by:almander
ID: 33513463
Just noticed table name
declare @userDate datetime
select @userDate = GetDate()

select top 1 *
from Quantity
order by abs(datediff(n, [datetime], @userDate))

Open in new window

0
 
LVL 5

Expert Comment

by:almander
ID: 33773997
Provided resolution to question.
0
 

Author Closing Comment

by:ASPDEV
ID: 33834980
Thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

610 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