Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
| Search | Today's Posts | Mark Forums Read
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 8th, 2006, 10:02 PM
Authorized User
 
Join Date: Feb 2006
Location: , , .
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I will really appreciate any help you can give me. Thanks Gert;
The format of my string is mm/dd/yyyy, I tried actually changing the field directly in the database design but I am getting the error:
Syntax error converting datetime from character string.
Quote:
quote:
Originally posted by Gert
 Hi.

If you could answer my question "What is the format of the date of birth string?", then we might be able to help.
You say it is in format "08/08/1965", but that doesn't tell us mutch. What is the date-part and what is the month-part?
In your example the result will always be august 8th 1965, but how would you write august 20th 1965 in your format?
"20/08/1965" or "08/20/1965" ?

Also: what errormessages do you get from the functions?

Gert

 
Old March 9th, 2006, 09:41 AM
Authorized User
 
Join Date: Jun 2003
Location: , , Norway.
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi again.

You should be able to use JoeFlow's function as it is. Try it out in a select. If it doesn't work, try setting the date format lik this:
SET DATEFORMAT mdy

If it works after you have done that, then maybe your default dateformat on the server is not what you think it is..

Gert


 
Old March 9th, 2006, 12:10 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Naperville, IL, USA.
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default

WARNING:

JoeFlow's CalculateAge function DOES NOT WORK

Try Rand's f_AgeCalc instead.

Test Results -- Errors marked with <<<

Test f_AgeCalc
ID Birth Date Age
----------- ---------- -----------
          1 01/01/2001 5
          2 02/29/2004 2
          3 03/01/2004 2
          4 03/02/2004 2
          5 03/01/2000 6
          6 12/31/1999 6
          7 04/01/2003 2
          8 02/07/1998 8
          9 12/24/1937 68
         10 05/08/1948 57
         11 03/03/2003 3
         12 02/29/1960 46
         13 03/09/1991 15
         14 03/10/1991 14

(14 row(s) affected)

Test CalculateAge
ID Birth Date Age
----------- ---------- -----------
          1 01/01/2001 4 <<<
          2 02/29/2004 1 <<<
          3 03/01/2004 1 <<<
          4 03/02/2004 1 <<<
          5 03/01/2000 5 <<<
          6 12/31/1999 6
          7 04/01/2003 2
          8 02/07/1998 7 <<<
          9 12/24/1937 68
         10 05/08/1948 57
         11 03/03/2003 2 <<<
         12 02/29/1960 45 <<<
         13 03/09/1991 14 <<<
         14 03/10/1991 14

(14 row(s) affected)



Rand
 
Old March 9th, 2006, 01:46 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

I think you can also get that "Syntax error..." if the varchar column in the table cannot be converted to a valid date via the CONVERT function.

Try:
Code:
SELECT birthdt, ...
  FROM yourtable
 WHERE ISDATE(birthdt) = 0
to see if there are any such values.

Your column may contain values like '12/31/2005' and '31/12/2005' which are perfectly legitimate strings, but, depending on how your server is configured, one or the other is always an invalid date. Your column could also contain total junk like 'mm/dd/yyyy', or 'HELLO WORLD'.

You need to find the person who designed this table to have dates stored as a character string, take them out back and shoot them. Next, convert this character string column to a datetime datatype. You'll find that it is much easier to manipulate dates when they are, er, dates.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old March 17th, 2006, 12:35 PM
Authorized User
 
Join Date: Oct 2005
Location: , , .
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here's a link to another user-defined function that computes for a date given the birthdate:

http://www.sql-server-helper.com/functions/get-age.aspx

SQL Server Helper
http://www.sql-server-helper.com
 
Old April 18th, 2016, 10:56 AM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2013
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Code:
CREATE FUNCTION [dbo].[ufn_GetAge]
(@pDateOfBirth DATETIME,
 @pAsOfDate    DATETIME
)
RETURNS INT
AS
     BEGIN
         DECLARE @vAge INT;
         IF @pDateOfBirth >= @pAsOfDate
             RETURN 0;
         SET @vAge = DATEDIFF(YY, @pDateOfBirth, @pAsOfDate);
         IF MONTH(@pDateOfBirth) > MONTH(@pAsOfDate)
            OR (MONTH(@pDateOfBirth) = MONTH(@pAsOfDate)
                AND DAY(@pDateOfBirth) > DAY(@pAsOfDate))
             SET @vAge = @vAge - 1;
         RETURN @vAge;
     END;
GO




Similar Threads
Thread Thread Starter Forum Replies Last Post
Age Calculation rgerald SQL Server 2000 32 October 10th, 2007 01:12 AM
age function keyvanjan ASP.NET 1.0 and 1.1 Basics 2 August 30th, 2006 08:46 AM
Vacancy Age alannoble26 Excel VBA 2 November 23rd, 2005 03:05 AM
Query birthdate by month Batusai Access 2 February 23rd, 2005 01:23 AM
calculate value on Lost Focus of text field creative_eye General .NET 1 April 12th, 2004 08:35 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.