显示标签为“datatype”的博文。显示所有博文
显示标签为“datatype”的博文。显示所有博文

2012年3月27日星期二

FLOAT datatype weirdness

Hello - I have the following two sets of very simple SQL
--Example #1
DECLARE @.BaseNum float
SELECT @.BaseNum = RAND(
(DATEPART(mm, GETDATE()) * 100000 )
+ (DATEPART(ss, GETDATE()) * 1000 )
+ DATEPART(ms, GETDATE()) )
print @.BaseNum
go
--Example #2
SELECT RAND(
(DATEPART(mm, GETDATE()) * 100000 )
+ (DATEPART(ss, GETDATE()) * 1000 )
+ DATEPART(ms, GETDATE()) )
go
WHY does example #1 only return approx 4, 5, or 6 digits to the right of the
decimal, while Example #2 returns 17 digits to the right of the decimal. I
have tried everything including CAST as CHAR(20), CONVERT(char(20),@.Basenum)
,
etc.
Can someone please provide some input?
ThanksThe results are different because PRINT implictly casts its output to a
string. Exactly what result do you want? FLOAT is an inexact numeric so if
you want to control the decimal precision you will need to cast it as
NUMERIC for example. On the other hand if you just want to *display* a fixed
number of decimal places then just let the application handle that
formatting client-side.
David Portas
SQL Server MVP
--|||MSSQLServerDeveloper wrote:
> Hello - I have the following two sets of very simple SQL
> --Example #1
> DECLARE @.BaseNum float
> SELECT @.BaseNum = RAND(
> (DATEPART(mm, GETDATE()) * 100000 )
> + (DATEPART(ss, GETDATE()) * 1000 )
> + DATEPART(ms, GETDATE()) )
> print @.BaseNum
> go
> --Example #2
> SELECT RAND(
> (DATEPART(mm, GETDATE()) * 100000 )
> + (DATEPART(ss, GETDATE()) * 1000 )
> + DATEPART(ms, GETDATE()) )
> go
> WHY does example #1 only return approx 4, 5, or 6 digits to the right
> of the decimal, while Example #2 returns 17 digits to the right of
> the decimal. I have tried everything including CAST as CHAR(20),
> CONVERT(char(20),@.Basenum), etc.
> Can someone please provide some input?
> Thanks
I don't see a difference here on those queries you supplied in SQL 2000
SP3a. Float is an inexact data type, so that might have something to do
with it.
What version of SQL Server (and SP) are you using.
David Gugick
Imceda Software
www.imceda.com|||I'm using 2k with sp2.
I solved it defining the variable @.BaseNum decimal(18,17)
"David Gugick" wrote:

> MSSQLServerDeveloper wrote:
> I don't see a difference here on those queries you supplied in SQL 2000
> SP3a. Float is an inexact data type, so that might have something to do
> with it.
> What version of SQL Server (and SP) are you using.
> --
> David Gugick
> Imceda Software
> www.imceda.com
>|||MSSQLServerDeveloper wrote:
> I'm using 2k with sp2.
> I solved it defining the variable @.BaseNum decimal(18,17)
Why are you only on SP2 and not on SP3a?
David Gugick
Imceda Software
www.imceda.com|||The default conversion from float to string yields only
6 digits of precision, and by using PRINT to display the
result you are using this conversion (as David pointed
out). In the second example, you are using SELECT,
which returns the float to the client for display without
first converting it to a string. Most clients will display
15 or 16 digits of precision for a float.
The actual float value contains the full precision in
each case, but you are only seeing part of it when
you use PRINT.
Be aware that RAND(), when used with a parameter,
is not random, but is completely reproducible and
deterministic. See
http://groups.google.co.uk/groups?q=0.7135733602414355
for more on this.
Steve Kass
Drew University
"MSSQLServerDeveloper" wrote:

> Hello - I have the following two sets of very simple SQL
> --Example #1
> DECLARE @.BaseNum float
> SELECT @.BaseNum = RAND(
> (DATEPART(mm, GETDATE()) * 100000 )
> + (DATEPART(ss, GETDATE()) * 1000 )
> + DATEPART(ms, GETDATE()) )
> print @.BaseNum
> go
> --Example #2
> SELECT RAND(
> (DATEPART(mm, GETDATE()) * 100000 )
> + (DATEPART(ss, GETDATE()) * 1000 )
> + DATEPART(ms, GETDATE()) )
> go
> WHY does example #1 only return approx 4, 5, or 6 digits to the right of t
he
> decimal, while Example #2 returns 17 digits to the right of the decimal.
I
> have tried everything including CAST as CHAR(20), CONVERT(char(20),@.Basenu
m),
> etc.
> Can someone please provide some input?
> Thanks
>

Float Datatype Truncation Bug

Hi Friends,

I have a table

Create table #table1(a float)

insert into #table1 values(123456789.987654321)

select a from #table1

drop table #table1

when i run this in SQLServer 2005 Management Studio i get the following truncated output

(1 row(s) affected)

a

-

123456789.987654

(1 row(s) affected)

when i run this query using SQL Query Analyser or OSQL Utility i get the following output

(1 row(s) affected)

a
--
123456789.98765431

(1 row(s) affected)

I want the full output in SQLServer 2005 itself.... Is this a microsoft bug?

I would like to know how to fix this?

Thanks and Regards,

It is not a bug. Your data is not truncated on the table(while storing). its bcs of the Management Console only.(MC result only truncate the values). Connect the same SQL Server 2005 from QA you will get the same result as 2000 (in your case your proper result).

If you want to trust your result use the following query.(explicit precision setting)

Code Snippet

Create table #table1(a float)

Insert into #table1 values(123456789.987654321)

Select cast(a as numeric(38,8)) from #table1

Drop table #table1

float datatype

I have several fields in a table with a float datatype. When the user
enters 1.1 into the database, it returns 1.1000000000000001 -- I would think
it should be 1.1000000000000000.
Won't this eventually cause mathematically errors and what do I do about
this.
FLOAT is an approximate numeric type. If you require accurate,
fixed-precision results from calculations then use an exact type such as
NUMERIC.
David Portas
SQL Server MVP

float datatype

I have several fields in a table with a float datatype. When the user
enters 1.1 into the database, it returns 1.1000000000000001 -- I would think
it should be 1.1000000000000000.
Won't this eventually cause mathematically errors and what do I do about
this.FLOAT is an approximate numeric type. If you require accurate,
fixed-precision results from calculations then use an exact type such as
NUMERIC.
David Portas
SQL Server MVP
--

Float Datatype

Hi Friends,

I have a table

Create table #table1(a float)

insert into #table1 values(123456789.987654321)

select a from #table1

drop table #table1

when i run this in SQLServer 2005 Management Studio i get the following truncated output

(1 row(s) affected)

a

-

123456789.987654

(1 row(s) affected)

when i run this query using SQL Query Analyser or OSQL Utility i get the following output

(1 row(s) affected)

a
--
123456789.98765431

(1 row(s) affected)

I want the full output in SQLServer 2005 itself.... Is this a microsoft bug?

I would like to know how to fix this?

Thanks and Regards,

It is not a bug. Your data is not truncated on the table(while storing). its bcs of the Management Console only.(MC result only truncate the values). Connect the same SQL Server 2005 from QA you will get the same result as 2000 (in your case your proper result).

If you want to trust your result use the following query.(explicit precision setting)

Code Snippet

Create table #table1(a float)

Insert into #table1 values(123456789.987654321)

Select cast(a as numeric(38,8)) from #table1

Drop table #table1

sql

float datatype

I have several fields in a table with a float datatype. When the user
enters 1.1 into the database, it returns 1.1000000000000001 -- I would think
it should be 1.1000000000000000.
Won't this eventually cause mathematically errors and what do I do about
this.FLOAT is an approximate numeric type. If you require accurate,
fixed-precision results from calculations then use an exact type such as
NUMERIC.
--
David Portas
SQL Server MVP
--

FLOAT

I want to use only float datatype but the problem is that it return more
than two decimal, I want to minimize to two decimal points? I don't want to
use decimal datatype... I only wanna use float....
The result should looks like
152254.45
but now it's giving me.
152254.45123232363
Any solution ?
Thanks
On Wed, 2 Nov 2005 11:19:15 -0500, Rogers wrote:

>I want to use only float datatype but the problem is that it return more
>than two decimal, I want to minimize to two decimal points? I don't want to
>use decimal datatype... I only wanna use float....
>The result should looks like
>152254.45
>but now it's giving me.
>152254.45123232363
>Any solution ?
Hi Rogers,
No, of course not. The FLOAT datatype is designed to *not* have an exact
and limited number of decimals. The datatypes that are designed to offer
a fixed number of decimal places are decimal and numeric.
Saying "I want to have two decimal points but I don't want to use
decimal datatype... I olnly wanna use float" is like saying "I want to
travel by air but I don't want to use a plane... I only wanna use a
boat".
Best, Hugo
(Remove _NO_ and _SPAM_ to get my e-mail address)
|||I only want to use float datatype because I have to frequenly convert the
database into Access 97 and Access doesn't support decimal and numeric
datatype.
Thanks
"Hugo Kornelis" <hugo@.pe_NO_rFact.in_SPAM_fo> wrote in message
news:q43im1ddb79j8bmre6kugqrjo7nem75f6r@.4ax.com...
> On Wed, 2 Nov 2005 11:19:15 -0500, Rogers wrote:
>
> Hi Rogers,
> No, of course not. The FLOAT datatype is designed to *not* have an exact
> and limited number of decimals. The datatypes that are designed to offer
> a fixed number of decimal places are decimal and numeric.
> Saying "I want to have two decimal points but I don't want to use
> decimal datatype... I olnly wanna use float" is like saying "I want to
> travel by air but I don't want to use a plane... I only wanna use a
> boat".
> Best, Hugo
> --
> (Remove _NO_ and _SPAM_ to get my e-mail address)
|||Rogers wrote:
> I only want to use float datatype because I have to frequenly convert
> the database into Access 97 and Access doesn't support decimal and
> numeric datatype.
>
Access 2003 ( I realize this is not Access 97 - but that version is almost 9
years old) has a Number Data Type, which can then be qualified with the
"Number" type. Choice abound:
- Byte
- Integer
- Long Integer
- Single
- Double
- Replication ID
- Decimal - which has precision and scale to match that used by SQL Server
David Gugick
Quest Software
|||On Wed, 2 Nov 2005 14:47:07 -0500, Rogers wrote:

>I only want to use float datatype because I have to frequenly convert the
>database into Access 97 and Access doesn't support decimal and numeric
>datatype.
Hi Rogers,
I just did a quick test - create a table with a decimal(9,2) column in
SQL Server, then create a linked table in Access 97. Access defines the
column as double. I could enter and retrieve data without any problems
(other than data with more than two decimals being rounded).
Best, Hugo
(Remove _NO_ and _SPAM_ to get my e-mail address)
sql

Flatten out an XML Document (Currently using OPENXML)

I am attempting to take a column with an XML datatype and make it available for reporting with as little code as possible. Specifically, we are storing credit report info in a column that has an XML datatype. We are using the OPENXML command to navigate the XML structure:

DECLARE @.idoc int
DECLARE @.doc xml
SET @.doc = 'GET THE XML COLUMN HERE
EXEC sp_xml_preparedocument @.idoc OUTPUT, @.doc

SELECT *
FROM OPENXML (@.idoc, '/XML_INTERFACE/CREDITREPORT/OBJECTS/DBCOMMCREDITSCORE',2)
WITH (
Rating varchar(10) 'DBRATING',
OverFlow xml '@.mp:xmltext')
EXEC sp_xml_removedocument @.idoc

The result will be a column that shows the Rating field and an Overflow column that shows the XML string.

I can take the above code and put it in a stored procedure to return the values in a flat format. My questions:

    Does anyone know how I can use a view to display the information? (Create View cannot have the above statements. Is there a way to create a tabular representation off of the SP result set, etc.)
    Does anyone have a better way of taking an XML datatype and making this available for report writers?
Dan O

1. The XQuery "nodes()" function will work for this. It is similar to OPENXML, but it is just used from within a normal SELECT statement, so you could create views on top of it.

Here is an example using the nodes() function on the xml data type.

http://msdn2.microsoft.com/en-us/ms188282.aspx

2. nodes() seems like the way to go, but you could also try pre-shredding the xml into a more convenient relational structure if your XML has a lot of nesting that would require many joins to reassemble.

2012年2月24日星期五

first 5 words

From a column of datatype either text or varchar how can I get a definite number of words?
Suppose it got -'I am a BA from Calcutta.Currently I am in Delhi'
I want to retreive first 5 words which will provide me the output-
'I am a BA from'

How can I do that?

SubhasishI imagine something like...

CREATE FUNCTION FindFifthWord
(@.str varchar(1000))
RETURNS int
AS
BEGIN
DECLARE @.SpacesFound INT
DECLARE @.LastSpacePosition INT
SET @.SpacesFound = 0
SET @.LastSpacePosition = 0

WHILE (@.SpacesFound < 5)
BEGIN
IF (CHARINDEX(' ', @.str, @.LastSpacePosition + 1) = 0)
BREAK

ELSE
BEGIN
SET @.LastSpacePosition = CHARINDEX(' ', @.str, @.LastSpacePosition + 1)
SET @.SpacesFound = @.SpacesFound + 1
END

END

RETURN @.LastSpacePosition
END|||No sorry this will not help me.
I may have ' , ' or '.' or space in the words.
How can I remove those?
And I am not getting the required output.

Subhasish|||Well it has to be delimited by something...in my case in the UDF I used space as the delimeter...

There has to be something...otherwise how would you decide to split it up?

In a sentence, the natural delimeter would be a space.|||create function dbo.fn_N_NumberOfWords (
@.phrase nvarchar(4000),
@.HowMany int ) returns nvarchar(4000)
as begin
declare @.i int, @.pos int
declare @.tbl table (cnt int not null, pos int not null)

set @.phrase = replace(replace(@.phrase, '.', '. '), ' ', ' ')

select @.i = 1, @.pos = 1
while @.i <= @.HowMany begin
set @.pos = charindex(' ', @.phrase + ' ', @.pos+1)
insert @.tbl select @.i, @.pos-1
set @.i = @.i + 1
end
return (select substring(@.phrase, 1, pos) from @.tbl where cnt = @.HowMany)
end
go
select dbo.fn_N_NumberOfWords('I am a BA from Calcutta.Currently I am in Delhi', 4)|||Yeah...I found the one I built...

CREATE FUNCTION udf_WORDS
(@.str varchar(8000), @.Del char(1)=' ')
RETURNS int
AS

/* Mimic REXX function */
BEGIN
DECLARE @.Words INT, @.Pos INT, @.x Int
SELECT @.Words = 0, @.Pos = 1, @.x = -1

WHILE (@.x <> 0)
BEGIN
SET @.x = CHARINDEX(@.Del, @.str, @.Pos)
SET @.Pos = @.x + 1
SET @.Words = @.Words + 1
END

RETURN @.Words
END|||if you're just counting words, then you don't have to have a loop:

alter function dbo.fn_CountWords (
@.phrase nvarchar(4000),
@.delimiter nchar(1) ) returns int
as begin
set @.phrase = rtrim(ltrim(@.phrase))
if cast(reverse(@.phrase) as nchar(1)) != @.delimiter
set @.phrase = rtrim(ltrim(@.phrase)) + @.delimiter
return (
select (datalength(@.phrase) - datalength(replace(@.phrase, @.delimiter, '')))/2
)
end
go|||Very cool...

Just remove the delimiter and compare the string lendths...|||I like the concept. I think you need some logic to account for duplicate delimiters, such as two spaces between sentences.

subhasishray appears to want a function that returns the first five (or N?) words as a string. I think a loop cannot be avoided in solving this.

Create function NWords(@.CharString varchar(500), @.NWords int)
returns varchar(500)
as
begin
declare @.TempString varchar(500)
set @.CharString = ltrim(rtrim(@.CharString))
set @.TempString = @.CharString + ' '
while @.NWords > 0
begin
set @.TempString = ltrim(right(@.TempString, len(@.TempString) + 1 - CharIndex(' ', @.TempString)))
set @.NWords = @.NWords - 1
end
return left(@.CharString, len(@.CharString) - len(@.TempString))
end
GO

print dbo.NWords('This is my string, just for sample purposes. You are free to copy it if your like.', 5)
GO

Result:
This is my string, just

If he wants commas and periods stripped out, h have to use the REPLACE function.

blindman|||i was talking about avoiding the loop while counting words, not while returning the n number of words.