Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Server Computed Column Specification for Age

Posted on 2014-01-02
6
Medium Priority
?
414 Views
Last Modified: 2014-01-02
Can anyone see my syntax error?

I get following message: Error validating the formula for column 'Age'.

(case when dayofyear([BirthDate]) < dayofyear(getdate()) then datediff(year,[BirthDate],getdate())-1 else  datediff(year,[BirthDate],getdate()) end)

Thanks,
ScreenShot.pdf
0
Comment
Question by:CloudApps
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 20

Accepted Solution

by:
dsacker earned 2000 total points
ID: 39751765
DayOfYear is not a function (unless you wrote one). Use this definition for your computed column:

CASE
    WHEN DATEPART(dayofyear, BirthDate) < DATEPART(dayofyear, GETDATE())
         THEN DATEDIFF(YEAR, BirthDate, GETDATE()) - 1
    ELSE DATEDIFF(YEAR, BirthDate, GETDATE())
END

Open in new window

I separated it out for readability (and to verify it worked). Return it to one line or however you wish to lower-case it.
0
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 39751777
select DATEDIFF(dayofyear, BirthDate, getdate()) -1 

Open in new window

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39751782
I presume dayofyear is a customer function, in which case you have to prefix it with the functions' owner name:

dbo.dayofyear(...)
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 29

Expert Comment

by:sammySeltzer
ID: 39751815
Is dayofyear a sql server 2012 function - a new feature in sql server?
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 39751908
You may find this thread helpful:
http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SQL-Server-2005/Q_23808219.html

Scott's approach is similar to dsacker's above, except he eliminated the repetition of the DATEDIFF by putting the CASE on the subtraction.

In other words, you can do this:
DATEDIFF(YEAR, BirthDate, GETDATE()) - CASE WHEN DATEPART(dayofyear, BirthDate) < DATEPART(dayofyear, GETDATE()) THEN  1 ELSE 0 END
0
 

Author Closing Comment

by:CloudApps
ID: 39751963
dsacker,

Thanks for figuring out what I was trying to accomplish. Your syntax worked to solve the problem.

Sorry, to very one else for the poor presentation of my problem.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question