May 14, 2012 at 6:45 am
I have a table such as the following.
Aşağıdaki gibi bir tablom var .(Turkish)
BIRTHDAY NAME SURNAME
1977-09-21 00:00:00.000Ali Gültiken
1978-01-05 00:00:00.000Veli Kavlak
1979-01-08 00:00:00.000KırkdokuzElli
1980-01-09 00:00:00.000Naim Süleymanoğlu
1981-06-05 00:00:00.000Eleni Kalçenzynk
1982-05-08 00:00:00.000Büşra Koca
1983-09-21 00:00:00.000AmedeusFofana
1984-08-21 00:00:00.000TitanicsBatiyor
1985-04-01 00:00:00.000Jemal Develioğlu
1986-01-01 00:00:00.000GaffarOkan
1987-01-09 00:00:00.000LibadiyeKarakol
1988-02-28 00:00:00.000EneramoSivas
1989-01-01 00:00:00.000Tanju Çolak
1990-01-01 00:00:00.000PrekaziCevad
There are 3 Column. BIRTHDAY , NAME , SURNAME
3 adet sütun var. BIRTHDAY , NAME , SURNAME (Turkish)
I want to list the students according to the range of the age.
Ben yaş aralığına göre öğrencileri listelemek istiyorum.(Turkish)
As an example, I want to list the students between the ages of 20-25. Then it must be the result as follows. The result of the following T-SQL code that is called how to?
Örnek olarak 20-25 yaş arasındaki öğrencileri listelemek istiyorum. O zaman sonucun aşağıdaki gibi olması gerekir. Aşağıdaki sonucu verecek T-SQL kodunu nasıl yazmalıyım ? (Turkish)
BIRTHDAY NAMESURNAME
1987-01-09 00:00:00.000LibadiyeKarakol
1988-02-28 00:00:00.000EneramoSivas
1989-01-01 00:00:00.000TanjuÇolak
1990-01-01 00:00:00.000PrekaziCevad
May 14, 2012 at 6:49 am
This should get you what you need for that particular age range.
SELECT
BIRTHDAY,
NAME,
SURNAME
FROM
TABLE
WHERE
BIRTHDAY >= DATEADD(YEAR,-25,GETDATE())
AND
BIRTHDAY <= DATEADD(YEAR,-20,GETDATE())
May 14, 2012 at 7:05 am
Thank you so much.If your answer is not bad.But 1 person missing.
How many years old is a person born in 09.01.1987?
09.01.1987 de dogan bir kisi kaç yasindadir ?
your result is as follows.
BIRTHDAY NAME SURNAME
1988-02-28 00:00:00.000EneramoSivas
1989-01-01 00:00:00.000Tanju Çolak
1990-01-01 00:00:00.000PrekaziCevad
In my opinion, must be listed in at least 4 people. Can you explain why your solution is the correct listing is doing?
BIRTHDAY NAME SURNAME
1987-01-09 00:00:00.000LibadiyeKarakol
1988-02-28 00:00:00.000EneramoSivas
1989-01-01 00:00:00.000Tanju Çolak
1990-01-01 00:00:00.000PrekaziCevad
May 14, 2012 at 7:12 am
That is due to DATEADD(YEAR,-25,GETDATE()) returning 1987-05-14, so it is excluded from the list.
The below should now work.
DECLARE @tbl TABLE (BIRTHDAY DATETIME, NAME NVARCHAR(50), SURNAME NVARCHAR(50))
INSERT INTO @tbl (BIRTHDAY, NAME,SURNAME) VALUES
('1977-09-21 00:00:00.000','Ali','Gültiken'),
('1978-01-05 00:00:00.000','Veli','Kavlak'),
('1979-01-08 00:00:00.000','Kirkdokuz','Elli'),
('1980-01-09 00:00:00.000','Naim','Süleymanoglu'),
('1981-06-05 00:00:00.000','Eleni','Kalçenzynk'),
('1982-05-08 00:00:00.000','Büsra','Koca'),
('1983-09-21 00:00:00.000','Amedeus','Fofana'),
('1984-08-21 00:00:00.000','Titanics','Batiyor'),
('1985-04-01 00:00:00.000','Jemal','Develioglu'),
('1986-01-01 00:00:00.000','Gaffar','Okan'),
('1987-01-09 00:00:00.000','Libadiye','Karakol'),
('1988-02-28 00:00:00.000','Eneramo','Sivas'),
('1989-01-01 00:00:00.000','Tanju','Çolak'),
('1990-01-01 00:00:00.000','Prekazi','Cevad')
SELECT
BIRTHDAY,
NAME,
SURNAME
FROM
@tbl
WHERE
DATEPART(YEAR,BIRTHDAY) >= DATEPART(YEAR,DATEADD(YEAR,-25,GETDATE()))
AND
BIRTHDAY <= DATEADD(YEAR,-20,GETDATE())
May 14, 2012 at 7:16 am
Thank you so much, thanks to you, we have the solution.God you get satisfied.
May 14, 2012 at 7:18 am
Not to worry, happy to help as always. Glad its working for you.
May 14, 2012 at 7:34 am
anthony.green (5/14/2012)
Not to worry, happy to help as always. Glad its working for you.
Unfortunately, it still isn't accurate. Today is 2012-05-14, how old is someone today that was born on or after 1986-05-15 and before 1987-01-01? They are also 25.
This is what you are actually looking for:
DECLARE @tbl TABLE (BIRTHDAY DATETIME, NAME NVARCHAR(50), SURNAME NVARCHAR(50))
INSERT INTO @tbl (BIRTHDAY, NAME,SURNAME) VALUES
('1977-09-21 00:00:00.000','Ali','Gültiken'),
('1978-01-05 00:00:00.000','Veli','Kavlak'),
('1979-01-08 00:00:00.000','Kirkdokuz','Elli'),
('1980-01-09 00:00:00.000','Naim','Süleymanoglu'),
('1981-06-05 00:00:00.000','Eleni','Kalçenzynk'),
('1982-05-08 00:00:00.000','Büsra','Koca'),
('1983-09-21 00:00:00.000','Amedeus','Fofana'),
('1984-08-21 00:00:00.000','Titanics','Batiyor'),
('1985-04-01 00:00:00.000','Jemal','Develioglu'),
('1986-01-01 00:00:00.000','Gaffar','Okan'),
('1987-01-09 00:00:00.000','Libadiye','Karakol'),
('1988-02-28 00:00:00.000','Eneramo','Sivas'),
('1989-01-01 00:00:00.000','Tanju','Çolak'),
('1990-01-01 00:00:00.000','Prekazi','Cevad')
SELECT
BIRTHDAY,
NAME,
SURNAME
FROM
@tbl
WHERE
BIRTHDAY >= DATEADD(yy, -26, DATEADD(dd, DATEDIFF(dd, 0, GETDATE()) + 1, 0))
AND
BIRTHDAY < DATEADD(yy, -20, DATEADD(dd, DATEDIFF(dd, 0, GETDATE()) + 1, 0));
May 14, 2012 at 7:37 am
Thanks for that Lynn much appreciated.
All I can say is fail on my behalf.
May 14, 2012 at 7:56 am
Hmmm, great answer to your schedule is very good.I guess, few can do better.If you have it, this is where the challenge 🙂
May 14, 2012 at 8:03 am
the first reply was not bad. The first correct answer was yours.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply