0

What I want to Achieve?

I am trying to generate Customer details into one single table. The customer Names and Addresses are in 2 different tables so I have created the below VIEW

SELECT dbo.Customer.CustomerId, dbo.Customer.GivenName + ' ' + dbo.Customer.FamilyName AS [CustomerName], dbo.Customer.DOB, dbo.Customer.Gender, 
                  dbo.Address.PostCode AS [Post Code], dbo.Address.Town AS Suburb, dbo.State.Code AS State

FROM    dbo.Address INNER JOIN
                  dbo.Cusotmer ON dbo.Address.AddressId = dbo.Customer.AddressID INNER JOIN
                  dbo.State ON dbo.Address.StateId = dbo.State.StateId

Question

How can I turn DOB into Age in the same view table ?

DOB is a Date Column in this formate : 2015-03-25

Lokapedia
  • 105
  • 1
  • 4
  • 12
  • What data type is `dob`? Also, are you using `mysql` or `sql-server` -- you tagged them both? – sgeddes Oct 30 '15 at 00:47
  • This format : 2015-03-03 – Lokapedia Oct 30 '15 at 00:48
  • Hi I am using SQL SERVER management studio – Lokapedia Oct 30 '15 at 00:50
  • Just add an expression to the `SELECT` list that calculates "age in years", for example, you could use something like this: `DATEDIFF(YEAR,c.DOB,GETDATE()) + CASE WHEN ((MONTH(c.DOB)*100) + DAY(c.DOB)) > ((MONTH(GETDATE())*100)+DAY(GETDATE())) THEN -1 ELSE 0 END AS [Age]`. This expression assumes that `dob` is a DATE, DATETIME, SMALLDATETIME, or a character string in a format that can be implicitly converted to DATE. It doesn't matter whether the SELECT statement is used in a view definition, the expression is the same,) – spencer7593 Oct 30 '15 at 01:21

0 Answers0