Wednesday, January 8, 2014

Practicing part 1?

A few simple exercises that I forced myself to work on tonight:

Use TSQL2012, hr.employee table:

Find all employees that were born after 1963

Born before 1963

Born between Feb. 1 1965 and today

Find employees who's first name starts with 'D' and is born after 1960

Create column "astrology", sort employees, if they are born in months Jan-Jun label them "libra" in 'astrology' column, if born between July-December "taurus"

Here are my results:

SELECT firstname, lastname, birthdate
FROM hr.employees
WHERE birthdate >= '19630101'
ORDER BY birthdate

SELECT firstname, lastname, birthdate
FROM hr.employees
WHERE birthdate < '19630101'

SELECT firstname, lastname, birthdate
FROM hr.employees
WHERE birthdate BETWEEN '19650201' AND GETDATE()

ONLY SHOW YEAR BORN

SELECT firstname, lastname, YEAR(birthdate) as birthyear
FROM hr.employees
ORDER BY birthyear

SELECT firstname, lastname, birthdate
FROM hr.employees
WHERE birthdate > '19600101' AND firstname LIKE 'D%'

SELECT firstname, lastname, MONTH(birthdate) as BirthMonth, "Astrology" =
CASE
WHEN MONTH(birthdate) <= 6 THEN 'libra'
WHEN MONTH(birthdate) >= 7 THEN 'taurus'
ELSE 'unknown'
END
FROM hr.employees
ORDER BY Astrology, BirthMonth

2 comments:

  1. you should be careful with dates. after 1963 would mean >='19640101' and not > '19630101'. also use >= and < instead of between.

    ReplyDelete
  2. Thank you for the comment, and good catch! I'm still learning these things, I appreciate the feed back!

    ReplyDelete