Submit your solution! When answering a question please: Read the question carefully. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. Let's work to help developers, not make them feel stupid. Related Questions.
How to calculate age from date of birth and display in a textbox. Calculate age in textbox. Calculate age with date of birth from database. Want to calculate age when user enter his date of birth in text box using excel. Age calculation in SQL. To calculate age in SQL. How to calculate age from date of birth. Layout: fixed fluid.
Web02 2. Strip HTML. Encode HTML. Paste as-is. Code block. Quoted Text. Best guess. Optional Password. Richard Deeming. Select name , surname from students. Select top 1 name,surname, year takenDate -year birthDate from students join borrows on students.
Select top 1 name , surname , year takenDate - year birthDate. Select top 1 name,surname, datediff YY,birthDate,takenDate from students join borrows on students. Select top 1 name , surname , datediff YY , birthDate , takenDate. Result: 1 row s listed.
Like this: Like Loading Sql Random Number Between 1 and You may also like. View all posts. Loading Comments Here the date range doesn't include a leap year so we're inaccurately reporting the age as a year younger than it should be. The problem with this method is that smaller date ranges will give us a less accurate answer, as in the example shown above.
Fortunately, there's one further method that we can use to calculate age in years correctly. This is the most accurate way to calculate an age in years, but it's also the most complex expression to write. The starting point is to use the first calculation we demonstrated at the top of the page to calculate the difference in years between two dates:. We can use the CASE statement to do this as follows:.
The expression is quite difficult to read, but it is the most accurate way to calculate an age in years in SQL Server. If you've learnt something from this blog, think how much more we could teach you about SQL! Hello, I have used your query to calculate age. I am wondering how to sort the newly created AS field. The expression I used is the same formula you provided. At the end of my query I wanted to sort the results using.
The Age field can be referenced without punctuation, as it doesn't contain a space. Alternatively, you can put square brackets round it like this: [Age]. What you can't do is put quotation marks, as what you're doing is sorting by the word "Age". Because this is a string of text, SQL can't compare it to an integer.
0コメント