Monday, March 12, 2012
Number conversion
I have a int-column that stores large numbers in my table and I want the output from a SELECT-statement to output it diffrent...
If the number is 6 100 000 The output should be 6.1 and 19 000 000 should be 19.1
I would also like the numbers to be rounded to the closest 100 000 (round(myInt, -5) right?)
thanks guys!Is this what your after
SELECT ROUND(Cast(Number as Decimal)/1E6,1)
Dave|||I'd suggest using:SELECT Cast(myColumn / 1e6 AS DECIMAL(6, 1))-PatP
Nulls in SQL 2005
Hello,
I have a varchar(20) field which stores a value or empty string.
I have noticed that if the value is NULL and I use is null in my query, the query runs very fast. However If I also include in my query a check for an empty string (len(rtrim(field)) = 0) the query is much slower.
Is it a good idea to force the empty strings to null and if Yes is there an automatic way in sql 2005 (maybe a trigger) to update all empty strings to null.
Thanks, I just need some advice.
Michael
The difference may be coming from the use of functions than checking for an empty string versus a null string. The optimizer cannot make use of an expression in the same way as it makes use of a field.
Using a trigger to turn blanks into null is not a bad idea, but it is not without overhead either. Also consider whether a blank string represents something different than a NULL. You might also want to revisit your data model and see whether it makes more sense to put the value in a separate table and remove the entry when it is not necessary.
|||One more option is to create computed column and index over it, but there are some requirements for that (refer for details: http://msdn2.microsoft.com/en-us/library/ms189292.aspx )|||HI
U can also use this query
select * from table1 where col1 is null or col1 = ''
Friday, March 9, 2012
Nullable Bit Field Storage
know that it stores the first 8 bits of a table in the same byte to save
storage, but a nullable bit field is essentially a tri-state and can't
possibly be stored in a single bit.Each row contains a separate null bitmap with a bit representing the null
state of each column.
Hope this helps.
Dan Guzman
SQL Server MVP
"Brad" <me@.privacy.net> wrote in message
news:MPG.1a70ba8015d9aeee98bb01@.news...
quote:|||In article <Oc027982DHA.556@.TK2MSFTNGP11.phx.gbl>, danguzman@.nospam-
> How does SQL store a collection of bit fields when they are nullable? I
> know that it stores the first 8 bits of a table in the same byte to save
> storage, but a nullable bit field is essentially a tri-state and can't
> possibly be stored in a single bit.
earthlink.net said...
quote:
> Each row contains a separate null bitmap with a bit representing the null
> state of each column.
How is that sized? Is it one byte for every eight nullable columns?|||From the Books Online <createdb.chm::/cm_8_des_02_92k3.htm>:
Null Bitmap (Null_Bitmap) = 2 + (( Num_Cols + 7) / 8 )
Hope this helps.
Dan Guzman
SQL Server MVP
"Brad" <me@.privacy.net> wrote in message
news:MPG.1a7133c92572518f98bb05@.news...
quote:
> In article <Oc027982DHA.556@.TK2MSFTNGP11.phx.gbl>, danguzman@.nospam-
> earthlink.net said...
null[QUOTE]
> How is that sized? Is it one byte for every eight nullable columns?
Nullable Bit Field Storage
know that it stores the first 8 bits of a table in the same byte to save
storage, but a nullable bit field is essentially a tri-state and can't
possibly be stored in a single bit.Each row contains a separate null bitmap with a bit representing the null
state of each column.
--
Hope this helps.
Dan Guzman
SQL Server MVP
"Brad" <me@.privacy.net> wrote in message
news:MPG.1a70ba8015d9aeee98bb01@.news...
> How does SQL store a collection of bit fields when they are nullable? I
> know that it stores the first 8 bits of a table in the same byte to save
> storage, but a nullable bit field is essentially a tri-state and can't
> possibly be stored in a single bit.|||In article <Oc027982DHA.556@.TK2MSFTNGP11.phx.gbl>, danguzman@.nospam-
earthlink.net said...
> Each row contains a separate null bitmap with a bit representing the null
> state of each column.
How is that sized? Is it one byte for every eight nullable columns?|||From the Books Online <createdb.chm::/cm_8_des_02_92k3.htm>:
Null Bitmap (Null_Bitmap) = 2 + (( Num_Cols + 7) / 8 )
--
Hope this helps.
Dan Guzman
SQL Server MVP
"Brad" <me@.privacy.net> wrote in message
news:MPG.1a7133c92572518f98bb05@.news...
> In article <Oc027982DHA.556@.TK2MSFTNGP11.phx.gbl>, danguzman@.nospam-
> earthlink.net said...
> > Each row contains a separate null bitmap with a bit representing the
null
> > state of each column.
> How is that sized? Is it one byte for every eight nullable columns?
Wednesday, March 7, 2012
Null values or multiple tables
These values can be either numeric or text.
I would like to store the text values in a varchar column and the
numeric values in a double column. This of course would help
performance amongs other things.
The question is this:
Is it better to have two tables, one that stores the numeric values
and one that stores the text values.
ex)
CREATE TABLE [dbo].[SnmpMibDataText] (
[SnmpMibDeviceID] [int] NOT NULL ,
[SampleTimestamp] [datetime] NOT NULL ,
[SampleValue] [nvarchar] (255) NOT NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[SnmpMibDataNumeric] (
[SnmpMibDeviceID] [int] NOT NULL ,
[SampleTimestamp] [datetime] NOT NULL ,
[SampleValue] [float] NOT NULL
) ON [PRIMARY]
Or is it better to have 1 table with two columns, one for text and one
for numeric?
CREATE TABLE [dbo].[SnmpMibData] (
[SnmpMibDeviceID] [int] NOT NULL ,
[SampleTimestamp] [datetime] NOT NULL ,
[SampleValueText] [nvarchar] (255),
[SampleValueNumeric] [float]
) ON [PRIMARY]
Now when the Mib value is numeric the text column would be null, and
when the mib value is text the numeric column would be null.
When i ask what is better, i mean overall better (performance,
usability, extendability, etc)?
Thanks in advance
Mark
The one table is probably the better one. Its row size is only slightly
larger than SnmpMibDataText in the two table approach. You have one less
table to manage.
Wei Xiao
Microsoft SQL Server Storage Engine
This posting is provided "AS IS" with no warranties, and confers no rights.
"Mark Oueis" <markoueis@.hotmail.com> wrote in message
news:b1800bd3.0412131123.7d5e2802@.posting.google.c om...
> I have a table that stores SNMP Mib values.
> These values can be either numeric or text.
> I would like to store the text values in a varchar column and the
> numeric values in a double column. This of course would help
> performance amongs other things.
> The question is this:
> Is it better to have two tables, one that stores the numeric values
> and one that stores the text values.
> ex)
> CREATE TABLE [dbo].[SnmpMibDataText] (
> [SnmpMibDeviceID] [int] NOT NULL ,
> [SampleTimestamp] [datetime] NOT NULL ,
> [SampleValue] [nvarchar] (255) NOT NULL
> ) ON [PRIMARY]
> CREATE TABLE [dbo].[SnmpMibDataNumeric] (
> [SnmpMibDeviceID] [int] NOT NULL ,
> [SampleTimestamp] [datetime] NOT NULL ,
> [SampleValue] [float] NOT NULL
> ) ON [PRIMARY]
>
> Or is it better to have 1 table with two columns, one for text and one
> for numeric?
> CREATE TABLE [dbo].[SnmpMibData] (
> [SnmpMibDeviceID] [int] NOT NULL ,
> [SampleTimestamp] [datetime] NOT NULL ,
> [SampleValueText] [nvarchar] (255),
> [SampleValueNumeric] [float]
> ) ON [PRIMARY]
> Now when the Mib value is numeric the text column would be null, and
> when the mib value is text the numeric column would be null.
>
> When i ask what is better, i mean overall better (performance,
> usability, extendability, etc)?
> Thanks in advance
> Mark
|||I would probably go with a three table design: a master table that had the
common definitions and, then, two 1-to1 or 1-to-many child related tables to
store the partiallly populated information. NULL-valued columns suit some
purposes but not usually the condidtion where you just don't know the
information or it is not applicable for some of the rows. That indicates
that it is not an attribute of the parent entitiy but subordinate entity
altogether.
Sincerely,
Anthony Thomas
"wei xiao[MS]" <weix@.online.microsoft.com> wrote in message
news:41beb22b$1@.news.microsoft.com...
The one table is probably the better one. Its row size is only slightly
larger than SnmpMibDataText in the two table approach. You have one less
table to manage.
Wei Xiao
Microsoft SQL Server Storage Engine
This posting is provided "AS IS" with no warranties, and confers no rights.
"Mark Oueis" <markoueis@.hotmail.com> wrote in message
news:b1800bd3.0412131123.7d5e2802@.posting.google.c om...
> I have a table that stores SNMP Mib values.
> These values can be either numeric or text.
> I would like to store the text values in a varchar column and the
> numeric values in a double column. This of course would help
> performance amongs other things.
> The question is this:
> Is it better to have two tables, one that stores the numeric values
> and one that stores the text values.
> ex)
> CREATE TABLE [dbo].[SnmpMibDataText] (
> [SnmpMibDeviceID] [int] NOT NULL ,
> [SampleTimestamp] [datetime] NOT NULL ,
> [SampleValue] [nvarchar] (255) NOT NULL
> ) ON [PRIMARY]
> CREATE TABLE [dbo].[SnmpMibDataNumeric] (
> [SnmpMibDeviceID] [int] NOT NULL ,
> [SampleTimestamp] [datetime] NOT NULL ,
> [SampleValue] [float] NOT NULL
> ) ON [PRIMARY]
>
> Or is it better to have 1 table with two columns, one for text and one
> for numeric?
> CREATE TABLE [dbo].[SnmpMibData] (
> [SnmpMibDeviceID] [int] NOT NULL ,
> [SampleTimestamp] [datetime] NOT NULL ,
> [SampleValueText] [nvarchar] (255),
> [SampleValueNumeric] [float]
> ) ON [PRIMARY]
> Now when the Mib value is numeric the text column would be null, and
> when the mib value is text the numeric column would be null.
>
> When i ask what is better, i mean overall better (performance,
> usability, extendability, etc)?
> Thanks in advance
> Mark
Null values or multiple tables
These values can be either numeric or text.
I would like to store the text values in a varchar column and the
numeric values in a double column. This of course would help
performance amongs other things.
The question is this:
Is it better to have two tables, one that stores the numeric values
and one that stores the text values.
ex)
CREATE TABLE [dbo].[SnmpMibDataText] (
[SnmpMibDeviceID] [int] NOT NULL ,
[SampleTimestamp] [datetime] NOT NULL ,
[SampleValue] [nvarchar] (255) NOT NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[SnmpMibDataNumeric] (
[SnmpMibDeviceID] [int] NOT NULL ,
[SampleTimestamp] [datetime] NOT NULL ,
[SampleValue] [float] NOT NULL
) ON [PRIMARY]
Or is it better to have 1 table with two columns, one for text and one
for numeric?
CREATE TABLE [dbo].[SnmpMibData] (
[SnmpMibDeviceID] [int] NOT NULL ,
[SampleTimestamp] [datetime] NOT NULL ,
[SampleValueText] [nvarchar] (255),
[SampleValueNumeric] [float]
) ON [PRIMARY]
Now when the Mib value is numeric the text column would be null, and
when the mib value is text the numeric column would be null.
When i ask what is better, i mean overall better (performance,
usability, extendability, etc)?
Thanks in advance
MarkThe one table is probably the better one. Its row size is only slightly
larger than SnmpMibDataText in the two table approach. You have one less
table to manage.
--
Wei Xiao
Microsoft SQL Server Storage Engine
This posting is provided "AS IS" with no warranties, and confers no rights.
"Mark Oueis" <markoueis@.hotmail.com> wrote in message
news:b1800bd3.0412131123.7d5e2802@.posting.google.com...
> I have a table that stores SNMP Mib values.
> These values can be either numeric or text.
> I would like to store the text values in a varchar column and the
> numeric values in a double column. This of course would help
> performance amongs other things.
> The question is this:
> Is it better to have two tables, one that stores the numeric values
> and one that stores the text values.
> ex)
> CREATE TABLE [dbo].[SnmpMibDataText] (
> [SnmpMibDeviceID] [int] NOT NULL ,
> [SampleTimestamp] [datetime] NOT NULL ,
> [SampleValue] [nvarchar] (255) NOT NULL
> ) ON [PRIMARY]
> CREATE TABLE [dbo].[SnmpMibDataNumeric] (
> [SnmpMibDeviceID] [int] NOT NULL ,
> [SampleTimestamp] [datetime] NOT NULL ,
> [SampleValue] [float] NOT NULL
> ) ON [PRIMARY]
>
> Or is it better to have 1 table with two columns, one for text and one
> for numeric?
> CREATE TABLE [dbo].[SnmpMibData] (
> [SnmpMibDeviceID] [int] NOT NULL ,
> [SampleTimestamp] [datetime] NOT NULL ,
> [SampleValueText] [nvarchar] (255),
> [SampleValueNumeric] [float]
> ) ON [PRIMARY]
> Now when the Mib value is numeric the text column would be null, and
> when the mib value is text the numeric column would be null.
>
> When i ask what is better, i mean overall better (performance,
> usability, extendability, etc)?
> Thanks in advance
> Mark|||I would probably go with a three table design: a master table that had the
common definitions and, then, two 1-to1 or 1-to-many child related tables to
store the partiallly populated information. NULL-valued columns suit some
purposes but not usually the condidtion where you just don't know the
information or it is not applicable for some of the rows. That indicates
that it is not an attribute of the parent entitiy but subordinate entity
altogether.
Sincerely,
Anthony Thomas
"wei xiao[MS]" <weix@.online.microsoft.com> wrote in message
news:41beb22b$1@.news.microsoft.com...
The one table is probably the better one. Its row size is only slightly
larger than SnmpMibDataText in the two table approach. You have one less
table to manage.
--
Wei Xiao
Microsoft SQL Server Storage Engine
This posting is provided "AS IS" with no warranties, and confers no rights.
"Mark Oueis" <markoueis@.hotmail.com> wrote in message
news:b1800bd3.0412131123.7d5e2802@.posting.google.com...
> I have a table that stores SNMP Mib values.
> These values can be either numeric or text.
> I would like to store the text values in a varchar column and the
> numeric values in a double column. This of course would help
> performance amongs other things.
> The question is this:
> Is it better to have two tables, one that stores the numeric values
> and one that stores the text values.
> ex)
> CREATE TABLE [dbo].[SnmpMibDataText] (
> [SnmpMibDeviceID] [int] NOT NULL ,
> [SampleTimestamp] [datetime] NOT NULL ,
> [SampleValue] [nvarchar] (255) NOT NULL
> ) ON [PRIMARY]
> CREATE TABLE [dbo].[SnmpMibDataNumeric] (
> [SnmpMibDeviceID] [int] NOT NULL ,
> [SampleTimestamp] [datetime] NOT NULL ,
> [SampleValue] [float] NOT NULL
> ) ON [PRIMARY]
>
> Or is it better to have 1 table with two columns, one for text and one
> for numeric?
> CREATE TABLE [dbo].[SnmpMibData] (
> [SnmpMibDeviceID] [int] NOT NULL ,
> [SampleTimestamp] [datetime] NOT NULL ,
> [SampleValueText] [nvarchar] (255),
> [SampleValueNumeric] [float]
> ) ON [PRIMARY]
> Now when the Mib value is numeric the text column would be null, and
> when the mib value is text the numeric column would be null.
>
> When i ask what is better, i mean overall better (performance,
> usability, extendability, etc)?
> Thanks in advance
> Mark