I have a list of items like so:
Field: Name & Field: DOB
- David - 1962
- Robert - 1955
- Tracy - 1980
- Bono - 1964
- Betty - 1968
I need a simple SQL query that will return all name whose birth years ate in the 1960s (1960-1969). something along the lines of this logic:
$sql = "SELECT * FROM employees WHERE DOB => '1960' AND DOB =< '1969' AND ORDER BY name";
FYI: Currently the DOB field in the employee database is a VARCHAR field with 4 character designation.
Can someone point me to the correct syntax on such a query?
You should not be storing date/time information as varchar
, but since you have one way you can screen for 1960s birthdays is to just check the first three characters of the DOB
field:
SELECT *
FROM employees
WHERE SUBSTRING(DOB, 1, 3) = '196'
ORDER BY name
You could also use your original approach, with the following WHERE
clause:
WHERE DOB >= '1960' AND DOB <= '1969'
However, I don't like treating strings as numbers, because you get weird things happening if the strings should ever change length. I would advise you to use a datetime
to store your DOB
information.
Change the query from:
"SELECT * FROM employees WHERE DOB => '1960' AND DOB =< '1969' AND ORDER BY name";
to
"SELECT * FROM employees WHERE DOB >= '1960' AND DOB <= '1969' AND ORDER BY name";
// Its >= or <= not => or =<
and try again
Assuming your varchar DOB column contains only numbers:
SELECT *
FROM employees
WHERE CONVERT(DOB, INTEGER) >= 1960
AND CONVERT(DOB, INTEGER) <= 1969
Did You try this ?
$sql = "SELECT * FROM employees WHERE DOB Like '%YYY%'AND ORDER BY name";
Consider yyy is 196X so all dob starting with 196 will result.
But this can't be specific enough
select * from tt1 where year >='1960' and year <='1969' order by name
希望这会有所帮助。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.