Solved

SQL return the closest matching date

Posted on 2013-02-03
8
774 Views
Last Modified: 2013-02-03
I have a date field that I want SQL to query in a optimized fashion and return the closest match, any suggestion?
0
Comment
Question by:Mazdajai
  • 5
  • 3
8 Comments
 
LVL 12

Expert Comment

by:tel2
ID: 38849726
Hi Mazdajai,

Are you saying you have a date field in a table and you want to find the record which contains the date which is closest to a date supplied by the user?

If so, how about you:
- subtract one date field from the other,
- sort the absolute value of those differences,
- and pick the lowest one (using "LIMIT 1").

Or do I misunderstand your requirements?
0
 
LVL 21

Author Comment

by:Mazdajai
ID: 38849744
Yes, I solved it with LIMIT 1, but I am getting an error with 'mybirthdate does not exist' when I try to use it in the sub query:

with mybirthdate as (select birthdate from table1 t1
where birthdate >= '1960-01-01' and id <= 1000
order by birthdate
limit 1)
select *
from table2 t2
join t1 on t1.id = t2.id
where t2.birthdate = mybirthdate

ERROR: column "mybirthdate" does not exist
0
 
LVL 12

Expert Comment

by:tel2
ID: 38849774
Q1. So are you saying you've already solved the problem outlined in your original question?

Q2. How is the above SQL query related to your original question?
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 21

Author Comment

by:Mazdajai
ID: 38849789
It is somehow related because I am trying to use it in a sub query.
0
 
LVL 12

Expert Comment

by:tel2
ID: 38849793
What is the answer to Q1, please Mazdajai.
0
 
LVL 21

Author Comment

by:Mazdajai
ID: 38849797
Q1 - yes
0
 
LVL 12

Accepted Solution

by:
tel2 earned 500 total points
ID: 38849801
Good.  Please accept my answer and open another question to cover your new problem.

Thanks Mazdajai
0
 
LVL 12

Expert Comment

by:tel2
ID: 38849815
Thanks for the points.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SYNTAX PROBLEM -- adding another column into the stored procedure output 30 38
Help Extract Specific in SQL 8 34
Help Parsing a String with SQL Syntax 23 42
SQL Query 26 60
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

813 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now