Ok so I have a table where we have custid, lastname, firstname, and DOB.
My task is to create one more column with the string that gives the decade the customer was born…
i.e. if the cust was born in 1950, the new colum for that record should say, Fifties,
If the cust was born in 1974, the new column for that record should say, Seventies. and so on.
I think I have to group by each decade and make a new column that gives the “string” representation of the decade the cust was born.
This is my try…
Select DateOfBirth, COUNT(DateOfBirth) as 'DOBYEAR' From Customers
Group by DateOfBirth
Order By DateOfBirth asc
If (Customers.DateOfBirth <= '1979')
begin
set DOBYEAR = 'Seventies and Before'
end
If (Customers.DateOfBirth between '1980' and '1989)'
begin
set DOBYEAR = 'Eighties'
end
If (Customers.DateOfBirth between '1990' and '1999')
begin
set DOBYEAR = 'Nineties'
end
If Customers.DateOfBirth >= '2000'
Begin
set DOBYEAR = '20th century'
end
Go
I am creating DOBYEAR as the new column and trying to input strings like, “seventies and before, eighties, nineties, and 20th century….
gives me the following error.
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near '='.
Msg 102, Level 15, State 1, Line 10
Incorrect syntax near '='.
Msg 102, Level 15, State 1, Line 14
Incorrect syntax near '='.
Msg 102, Level 15, State 1, Line 18
Incorrect syntax near '='.
Please guide me in the right direction.
Can I use “Print ‘Seventies and Before'” instead of “set DOBYEAR = ‘Seventies and Before'”?
Maybe, I can do this without using IF statements but do not know how.
ThankX
You don’t need group by to do this. Assuming the
DateOfBirthcolumn is typedatetime(hint: it should):