Showing posts with label maximum. Show all posts
Showing posts with label maximum. Show all posts

Friday, March 9, 2012

Intensively used function in view needs a minimum and maximum from a table

I have a problem (who not?) with a function which i'm using in a view.
This function is a function which calculates a integer value of a
date. For example: '12/31/2004 00:00:00" becomes 20041231. This is
very handy in a datawarehouse and performes superfast. But here is my
problem.

My calendar table is limited by a couple of years. What happens is
that sometimes a value is loaded which is not in the range of the
Calendardate. What we want to do is when a date is loaded is that this
function insert a minimum date when date < minimum date and a maximum
date when date > maximum date.

Yes i know you're thinking : This is datamanipulation and yes this is
true. But now we loose information in our cubes and reports by inner
joining. So if we can use a minimum and a maximum than a user would
say: "This is strange, a lot of values on 1980/1/1!" instead of "I
think that i have not all the data!"

Greetz

HennieHi

If you LEFT or RIGHT JOIN to the calendar table you will get a NULL value
for the column, you can then is CASE to determine the value

CREATE FUNCTION ConvertDate (@.datevalue datetime)
RETURNS INT
AS
BEGIN
DECLARE @.dateint INT
SELECT @.dateint = CAST( CASE WHEN A.Date < '20030101' THEN '19800101'
WHEN A.Date > '20051231' THEN '99991231'
ELSE CONVERT(CHAR(4),C.[Year]) + RIGHT('0'+
CONVERT(VARCHAR(2),C.[Month]),2) + RIGHT('0'+ CONVERT(VARCHAR(2),C.[Day]),2)
END AS INT )
FROM ( SELECT @.datevalue AS [Date] ) A
LEFT JOIN CALENDAR C ON C.[Date] = A.[Date]
RETURN @.dateint
END
GO

John

"Hennie de Nooijer" <hdenooijer@.hotmail.com> wrote in message
news:191115aa.0412300238.7dee0f85@.posting.google.c om...
>I have a problem (who not?) with a function which i'm using in a view.
> This function is a function which calculates a integer value of a
> date. For example: '12/31/2004 00:00:00" becomes 20041231. This is
> very handy in a datawarehouse and performes superfast. But here is my
> problem.
> My calendar table is limited by a couple of years. What happens is
> that sometimes a value is loaded which is not in the range of the
> Calendardate. What we want to do is when a date is loaded is that this
> function insert a minimum date when date < minimum date and a maximum
> date when date > maximum date.
> Yes i know you're thinking : This is datamanipulation and yes this is
> true. But now we loose information in our cubes and reports by inner
> joining. So if we can use a minimum and a maximum than a user would
> say: "This is strange, a lot of values on 1980/1/1!" instead of "I
> think that i have not all the data!"
> Greetz
> Hennie|||On 30 Dec 2004 02:38:51 -0800, Hennie de Nooijer wrote:

>I have a problem (who not?) with a function which i'm using in a view.
>This function is a function which calculates a integer value of a
>date. For example: '12/31/2004 00:00:00" becomes 20041231. This is
>very handy in a datawarehouse and performes superfast. But here is my
>problem.
(snip)

Hi Hennie,

Is this conversion all that your function does? If so, you might want to
try the following alternative (using CURRENT_TIMESTAMP as example; replace
it with your date column / parameter):

SELECT CAST(CONVERT(varchar, CURRENT_TIMESTAMP, 112) AS int)

You could put this in the UDF (probably at least as fast as your current
Calenmdar-table based function), or use it inline as a replacement to the
function call (probably even faster).

It should work for all dates from Jan 1st 1753 through Dec 31st 9999.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)

Friday, February 24, 2012

Integration Services Data Types Maximum Length

Hi,

Is there a way in-code to determine the maximum length of a Integration Services Data Type.

I need to determine based on the data type what the maximum length of a column is IN-CODE.

However, the column.Length property only gives me a length for DT_WSTR and DT_STR values. This is the only property that would seem to remotely give me the right answer.

I need to know the maximum lengths in columns for DT_BOOL, DT_CY, DT_I2, DT_I4, DT_I8, DT_NUMERIC, and DT_UI1. I can always hard-code these values into my program, but that makes no sense. There has to be some sort of way to determine what the maximum possible length of these values are.

For numeric values I could use the column.Precision value but that still leaves with with a lot of data types without a maximum length.
It might help if you explain why you want to know this information.

A DT_I4 is 4 bytes long, for instance, but it can hold -2,147,483,648 to 2,147,483,647. So which value do you want? Do you see the problem?

Data types are fixed in what they can handle, so why not just hard code them into your "IN-CODE"?|||In your example, for DT_I4, -2,147,483,648 is a length of 11 (excluding commas, but including the sign). Thats the kind of value I need to know. I apologize if I wasn't clear on that.

Well, yes, I could hard-code them. But I was wondering whether that type of information is actually stored (which really if you think about it, shouldn't be that difficult, and also worthwhile so a person does not have the compute what each one would be).

Hard-coding is something you also try to avoid if there is a way to avoid it (and hence my question as to whether I can avoid this or not).

Thanks

|||But that's just it, the length of a DT_I4 is 4, not 11.

The "length" of 11 is not stored anywhere. It's a limitation of the storage allocated to the data type. There are probably internal bounds checks, but I'm guessing they are not exposed. You could always try one of the many programming forums in MSDN to see if someone has an idea, as this really isn't an SSIS issue. SSIS data types are simply mapped to structures in .Net.

And the max length of a DT_STR is 8000 bytes. DT_WSTR is 4000 bytes. But more importantly, there is no such thing as a DT_STR, DT_I4, etc... in code. So what SSIS has as limitations may not be the same as the structures available in your code. (A DT_I4 equates to the Integer (Int32) structure.)|||

theddern wrote:

Hi,

Is there a way in-code to determine the maximum length of a Integration Services Data Type.

I need to determine based on the data type what the maximum length of a column is IN-CODE.

However, the column.Length property only gives me a length for DT_WSTR and DT_STR values. This is the only property that would seem to remotely give me the right answer.

I need to know the maximum lengths in columns for DT_BOOL, DT_CY, DT_I2, DT_I4, DT_I8, DT_NUMERIC, and DT_UI1. I can always hard-code these values into my program, but that makes no sense. There has to be some sort of way to determine what the maximum possible length of these values are.

For numeric values I could use the column.Precision value but that still leaves with with a lot of data types without a maximum length.

There is no maximum/minimum length for those data types. They are just what they are - they never change. The length simply isn't relevant.

-Jamie

|||

theddern wrote:

In your example, for DT_I4, -2,147,483,648 is a length of 11 (excluding commas, but including the sign).

No its not. The length of it is 4. 4 bytes that is.

If the value that you want is 11 then just cast it as a DT_STR and get the length of that.

-Jamie

|||So what you are saying is that I need to hard-code the lengths if I need to know those particular type of values?

|||

theddern wrote:

So what you are saying is that I need to hard-code the lengths if I need to know those particular type of values?

Yes! I just can't see the value in knowing the "lengths" of data types.|||And to clarify, I need to know what the maximum length (in characters) a particular column can be if that particular data was transfered to a command delimited flat file.

I was trying to figure that out based on IDTSColumn90.DataType because that would be the most logical place to start.
|||Are you wanting the display length for each data type?|||

theddern wrote:

And to clarify, I need to know what the maximum length (in characters) a particular column can be if that particular data was transfered to a command (sic) delimited flat file.

Why? If it is a delimited file, who cares?|||If he wanted to document specs for a fixed-length output file, he would find these useful. |||

theddern wrote:

So what you are saying is that I need to hard-code the lengths if I need to know those particular type of values?

No. I'm saying you don't need to care.

|||

Phil Brammer wrote:

theddern wrote:

And to clarify, I need to know what the maximum length (in characters) a particular column can be if that particular data was transfered to a command (sic) delimited flat file.

Why? If it is a delimited file, who cares?

Very very very good point.

This seems a very strange thing to want to do.

|||Yes