July 25, 2013 at 1:49 pm
Hi,
i have table named PersonalData.In this table i have a column DateOfBirth.I want to get the person who is the youngest of all.
For this what condition i should put on DateOfBirth column in Where clause.
Please help.
July 25, 2013 at 1:59 pm
SELECT * FROM myTable
WHERE BirthDate = (SELECT MIN(BirthDate) AS EarliestDate FROM myTable)
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
July 25, 2013 at 1:59 pm
armaandani (7/25/2013)
Hi,i have table named PersonalData.In this table i have a column DateOfBirth.I want to get the person who is the youngest of all.
For this what condition i should put on DateOfBirth column in Where clause.
Please help.
MIN(DateOfBirth)???
There are a number of ways to do this. Why does it have to be in the where clause?
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
July 25, 2013 at 4:05 pm
Explain what it means to "get the person"; do you want to return the entire person row, just the person's ID, etc. ?
"Do not seek to follow in the footsteps of the wise. Instead, seek what they sought." - Matsuo Basho
July 25, 2013 at 4:14 pm
How about this?
SELECT TOP 1 *
FROM MyTable
ORDER BY DateOfBirth DESC
July 25, 2013 at 4:45 pm
Luis Cazares (7/25/2013)
How about this?
SELECT TOP 1 *
FROM MyTable
ORDER BY DateOfBirth DESC
We also can't assume that DateOfBirth isn't nullable.
SELECT TOP 1 *
FROM MyTable
WHERE DateOfBirth is not null
ORDER BY DateOfBirth DESC
"Do not seek to follow in the footsteps of the wise. Instead, seek what they sought." - Matsuo Basho
July 25, 2013 at 5:15 pm
Eric M Russell (7/25/2013)
Luis Cazares (7/25/2013)
How about this?
SELECT TOP 1 *
FROM MyTable
ORDER BY DateOfBirth DESC
We also can't assume that DateOfBirth isn't nullable.
SELECT TOP 1 *
FROM MyTable
WHERE DateOfBirth is not null
ORDER BY DateOfBirth DESC
Because some people still don't know when they were born :hehe:
July 26, 2013 at 12:52 am
Luis Cazares (7/25/2013)
Eric M Russell (7/25/2013)
Luis Cazares (7/25/2013)
How about this?
SELECT TOP 1 *
FROM MyTable
ORDER BY DateOfBirth DESC
We also can't assume that DateOfBirth isn't nullable.
SELECT TOP 1 *
FROM MyTable
WHERE DateOfBirth is not null
ORDER BY DateOfBirth DESC
Because some people still don't know when they were born :hehe:
In some underdeveloped countries this is actually the case.
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
July 26, 2013 at 7:29 am
Eric M Russell (7/25/2013)
Luis Cazares (7/25/2013)
How about this?
SELECT TOP 1 *
FROM MyTable
ORDER BY DateOfBirth DESC
We also can't assume that DateOfBirth isn't nullable.
SELECT TOP 1 *
FROM MyTable
WHERE DateOfBirth is not null
ORDER BY DateOfBirth DESC
This is why I suggested MIN in my first post.
SELECT MIN(DateOfBirth)
FROM MyTable
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
July 26, 2013 at 7:56 am
The problem with MIN(DateFoBirth) is that is getting the oldest person, not the youngest. :hehe:
July 26, 2013 at 7:57 am
Luis Cazares (7/26/2013)
The problem with MIN(DateFoBirth) is that is getting the oldest person, not the youngest. :hehe:
ROFL. Between all of us maybe we can put together a simple query that actually gets it right. 😉
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
July 26, 2013 at 8:42 am
🙂
"Do not seek to follow in the footsteps of the wise. Instead, seek what they sought." - Matsuo Basho
July 26, 2013 at 8:43 am
Let's get some coffee before we answer in the morning 🙂
July 26, 2013 at 11:43 am
DECLARE @date [datetime]
SELECT @date = MAX([datecolumn]) FROM [your_table]
SELECT [required column | * ]
FROM [your_table]
WHERE [datecolumn]=@date
Thanks,
Prabhu
July 28, 2013 at 1:25 pm
Luis Cazares (7/26/2013)
The problem with MIN(DateFoBirth) is that is getting the oldest person, not the youngest. :hehe:
Barf. Brainfart! :-D:hehe:
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
Viewing 15 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply