Unix Technical Forum

How to find the maximum possible value of a given datatype

This is a discussion on How to find the maximum possible value of a given datatype within the SQL Server forums, part of the Microsoft SQL Server category; --> Does anyone know of a built-in function to return the maximum possible value of a given datatype? I have ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2008, 03:22 PM
jshunter@waikato.ac.nz
 
Posts: n/a
Default How to find the maximum possible value of a given datatype

Does anyone know of a built-in function to return the maximum possible
value of a given datatype? I have to return the biggest value for a
smalldatetime or datetime in a view if the field is null, but can't
find such a function. The closest I've come is:

select datalength(cast(getdate() as smalldatetime))

....but that only return the number of bytes, not the value itself,
which is '6-6-2079 11:59'

I know I could create my own lookup table and function, but I was
hoping that Transact-SQL would have a built-in solution

--John Hunter

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-01-2008, 03:22 PM
Roy Harvey
 
Posts: n/a
Default Re: How to find the maximum possible value of a given datatype

How about just hardcoding what is in the documentation? DATETIME, for
example says "Date and time data from January 1, 1753 through December
31, 9999, to an accuracy of one three-hundredth of a second
(equivalent to 3.33 milliseconds or 0.00333 seconds)." Which after
consulting the accompanying chart comes to 9999-12-31 23:59:59.997.

Roy Harvey
Beacon Falls, CT

On Wed, 27 Jun 2007 01:39:49 -0000, "jshunter@waikato.ac.nz"
<jshunter@waikato.ac.nz> wrote:

>Does anyone know of a built-in function to return the maximum possible
>value of a given datatype? I have to return the biggest value for a
>smalldatetime or datetime in a view if the field is null, but can't
>find such a function. The closest I've come is:
>
>select datalength(cast(getdate() as smalldatetime))
>
>...but that only return the number of bytes, not the value itself,
>which is '6-6-2079 11:59'
>
>I know I could create my own lookup table and function, but I was
>hoping that Transact-SQL would have a built-in solution
>
>--John Hunter

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-01-2008, 03:22 PM
jshunter@waikato.ac.nz
 
Posts: n/a
Default Re: How to find the maximum possible value of a given datatype

On Jun 27, 1:56 pm, Roy Harvey <roy_har...@snet.net> wrote:
> How about just hardcoding what is in the documentation? DATETIME, for
> example says "Date and time data from January 1, 1753 through December
> 31, 9999, to an accuracy of one three-hundredth of a second
> (equivalent to 3.33 milliseconds or 0.00333 seconds)." Which after
> consulting the accompanying chart comes to 9999-12-31 23:59:59.997.
>
> Roy Harvey
> Beacon Falls, CT


I've had to do just that, but it would be nice if I could have the
view do the job via a function. For example, if the underlying table
changed from a smalldatetime to a datetime, then all views which use
it would automatically return the correct maximum value

--John Hunter


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-01-2008, 03:22 PM
Roy Harvey
 
Posts: n/a
Default Re: How to find the maximum possible value of a given datatype

On Wed, 27 Jun 2007 02:04:39 -0000, "jshunter@waikato.ac.nz"
<jshunter@waikato.ac.nz> wrote:

>On Jun 27, 1:56 pm, Roy Harvey <roy_har...@snet.net> wrote:
>> How about just hardcoding what is in the documentation? DATETIME, for
>> example says "Date and time data from January 1, 1753 through December
>> 31, 9999, to an accuracy of one three-hundredth of a second
>> (equivalent to 3.33 milliseconds or 0.00333 seconds)." Which after
>> consulting the accompanying chart comes to 9999-12-31 23:59:59.997.
>>
>> Roy Harvey
>> Beacon Falls, CT

>
>I've had to do just that, but it would be nice if I could have the
>view do the job via a function. For example, if the underlying table
>changed from a smalldatetime to a datetime, then all views which use
>it would automatically return the correct maximum value
>
>--John Hunter


One alternative is to write your own function that encapsulates the
hardcode. Pass the table and column names, query the system tables
for the type and related information such as max length of a varchar,
and then return it. Clumsy, no doubt.

Or reconsider the design decision of using maximum values in place of
NULLs.

Roy Harvey
Beacon Falls, CT
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-01-2008, 03:22 PM
Roy Harvey
 
Posts: n/a
Default Re: How to find the maximum possible value of a given datatype

>One alternative is to write your own function that encapsulates the
>hardcode. Pass the table and column names, query the system tables
>for the type and related information such as max length of a varchar,
>and then return it. Clumsy, no doubt.


My apologies, it is nowhere near as simple as I described as, at a
minimum, one function for each datatype group (dates, character
strings, etc) would be required.

Roy Harvey
Beacon Falls, CT
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 03-01-2008, 03:22 PM
Ed Murphy
 
Posts: n/a
Default Re: How to find the maximum possible value of a given datatype

jshunter@waikato.ac.nz wrote:

> Does anyone know of a built-in function to return the maximum possible
> value of a given datatype? I have to return the biggest value for a
> smalldatetime or datetime in a view if the field is null,


If you (and the recipient of this return value, if it isn't another
piece of T-SQL) can wrap your head around ternary logic, then you
can use the null value as is:

where not (some_date > end_date)

Or, depending on the expected longevity of the system, you can (a)
hard-code June 6, 2079 (max smalldatetime) or (b) avoid smalldatetime
and hard-code December 31, 9999 (max datetime). I would personally
go with (b), as I can't imagine actually designing a system that had
a good reason to use smalldatetime for anything.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 03-01-2008, 03:23 PM
Gert-Jan Strik
 
Posts: n/a
Default Re: How to find the maximum possible value of a given datatype

No there isn't.

But there are solutions. You can read more at
http://groups.google.nl/group/micros...43f4aea485c6d1
(url may wrap)

HTH,
Gert-Jan


"jshunter@waikato.ac.nz" wrote:
>
> Does anyone know of a built-in function to return the maximum possible
> value of a given datatype? I have to return the biggest value for a
> smalldatetime or datetime in a view if the field is null, but can't
> find such a function. The closest I've come is:
>
> select datalength(cast(getdate() as smalldatetime))
>
> ...but that only return the number of bytes, not the value itself,
> which is '6-6-2079 11:59'
>
> I know I could create my own lookup table and function, but I was
> hoping that Transact-SQL would have a built-in solution
>
> --John Hunter

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:33 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com