Showing posts with label example. Show all posts
Showing posts with label example. Show all posts

Tuesday, March 27, 2012

BinToBit function

Anyone happen to have a function that does the following:

I have an integer for example 57.

This translates to binary: 111001

I'm looking for a function like this:

Code Snippet

CREATE FUNCTION [dbo].[BinToBit] (@.ValueCol int, @.Number TinyInt)

RETURNS bit AS

If I would call the function:

select dbo.BinToBit(57, 0) it should return 1

select dbo.BinToBit(57, 1) it should return 0

select dbo.BinToBit(57, 2) it should return 0

select dbo.BinToBit(57, 3) it should return 1

select dbo.BinToBit(57, 4) it should return 1

select dbo.BinToBit(57, 5) it should return 1

I've been looking on the net, because I'm convinced someone must have this kind of function, unfortunately haven't been able to find it.

CREATE FUNCTION [dbo].[BinToBit] (@.ValueCol int, @.Number TinyInt)

RETURNS bit AS

BEGIN

RETURN (@.ValueCol & POWER(2,@.Number))

END

|||Ha! Perfect thanks!

Bind to multiple tables from stored procedure

I know a sql stored procedure can return >1 tables. How can I use .Net 2.0 to read these tables one at a time, for example the first one could iterate Forum entries and the second one all internal links used in these forums... The idea is to use fewer backtrips to the sql server?

Isthis article of any use?|||no fortunately not :(

Thursday, March 22, 2012

Binary

Hi,
I have a column that contains binary representation of numbers. For example
'10' for 2 and '11' for 3 and ...
I wanted to write a function to convert this column to INT but I thought
there might be a built-in function in SQL Server.
Are there any function?
Thanks in advance,
LeilaHi
You should be able to cast this directly!
DECLARE @.bin varbinary(10)
SET @.bin = 2
SELECT @.bin, cast(@.bin as int )
John
"Leila" wrote:

> Hi,
> I have a column that contains binary representation of numbers. For exampl
e
> '10' for 2 and '11' for 3 and ...
> I wanted to write a function to convert this column to INT but I thought
> there might be a built-in function in SQL Server.
> Are there any function?
> Thanks in advance,
> Leila
>
>|||Leila,
There is an undocumented system function fn_replbitstringtoint()
that will do this, if you pad your column value by adding '0's on
the left to make it 32 characters long. Here are some examples:
select fn_replbitstringtoint('00000000000000000
000000000000011')
select fn_replbitstringtoint('1')
select fn_replbitstringtoint(right(replicate('0
',32)+'1',32))
declare @.yourBits varchar(32)
set @.yourBits = '1101'
select fn_replbitstringtoint(right(replicate('0
',32)+@.yourBits,32))
Since this function is not documented, it is not supported and
may not exist or work the same way in the future. You should
not use it in production code.
You can write an equivalent function as a user-defined function.
Here's a newsgroup thread that should help:
http://groups.google.co.uk/groups?q...tstring+integer
Steve Kass
Drew University
Leila wrote:

>Hi,
>I have a column that contains binary representation of numbers. For example
>'10' for 2 and '11' for 3 and ...
>I wanted to write a function to convert this column to INT but I thought
>there might be a built-in function in SQL Server.
>Are there any function?
>Thanks in advance,
>Leila
>
>|||John,
Leila has the input string '10'. If she already had the
value 2 as input, she wouldn't need to do this. I don't
think there is any conversion using CAST() that will
produce 2 as the result of CAST('10' as ').
Your example produces the integer 2 in the statement
SET @.bin = 2, not in the CAST() operation, and in order
to do SET @.bin = 2, you must already have the answer.
Leila needs something to fill in the gap here:
declare @.columnValue varchar(32)
set @.columnValue = '10'
declare @.result int
'? -- @.result gets the value 2, without your typing 2
select @.result
SK
John Bell wrote:
>Hi
>You should be able to cast this directly!
>DECLARE @.bin varbinary(10)
>SET @.bin = 2
>SELECT @.bin, cast(@.bin as int )
>John
>"Leila" wrote:
>
>|||I am not sure how I miss-interpreted that!
"Steve Kass" wrote:

> John,
> Leila has the input string '10'. If she already had the
> value 2 as input, she wouldn't need to do this. I don't
> think there is any conversion using CAST() that will
> produce 2 as the result of CAST('10' as ').
> Your example produces the integer 2 in the statement
> SET @.bin = 2, not in the CAST() operation, and in order
> to do SET @.bin = 2, you must already have the answer.
> Leila needs something to fill in the gap here:
> declare @.columnValue varchar(32)
> set @.columnValue = '10'
> declare @.result int
> '? -- @.result gets the value 2, without your typing 2
> select @.result
> SK
> John Bell wrote:
>
>|||Thanks Steve :-)
"Steve Kass" <skass@.drew.edu> wrote in message
news:OZckRZrjFHA.3960@.TK2MSFTNGP12.phx.gbl...
> Leila,
> There is an undocumented system function fn_replbitstringtoint()
> that will do this, if you pad your column value by adding '0's on
> the left to make it 32 characters long. Here are some examples:
> select fn_replbitstringtoint('00000000000000000
000000000000011')
> select fn_replbitstringtoint('1')
> select fn_replbitstringtoint(right(replicate('0
',32)+'1',32))
> declare @.yourBits varchar(32)
> set @.yourBits = '1101'
> select fn_replbitstringtoint(right(replicate('0
',32)+@.yourBits,32))
> Since this function is not documented, it is not supported and
> may not exist or work the same way in the future. You should
> not use it in production code.
> You can write an equivalent function as a user-defined function.
> Here's a newsgroup thread that should help:
> http://groups.google.co.uk/groups?q...tstring+integer
> Steve Kass
> Drew University
>
> Leila wrote:
>
example|||Why are you doing assembly level programming in SQL? The whole idea of
a database was to separate data from application code with a layer or
two of abstractions between them. You should not be worrying about
things like this.|||Joe,
This is the legacy data and I'm trying to transform it!
Thanks.
"--CELKO--" <jcelko212@.earthlink.net> wrote in message
news:1122068660.835679.173370@.z14g2000cwz.googlegroups.com...
> Why are you doing assembly level programming in SQL? The whole idea of
> a database was to separate data from application code with a layer or
> two of abstractions between them. You should not be worrying about
> things like this.
>|||I would think about getting a low level language like C or something
and move it over to a CSV file that you can edit before it goes into
your SQL data base. The data is probably a mess.

Bill of material (SQL2000)

Hi,
Does anyone got a really good/fast example on how to do a BOM with unlimited
levels in SQL2000
- without using cursors :)
2 tables (one for items, one for parent/child relations)
In SQL2005 we of course are blessed with the new CTE, not in SQL2000 :-/
In advance, thanks...
Kr. Sorenhow do you want your data output? just a SP that returns a list of all
materials a particular item requires, right down the tree?
if so then the only way I can think of without cursors is using global
temporary tables and recursively called stored procedures to populate
it (but then you might as well use a cursor for that).
if you're wanting columns of data to represent the tree structure then
it's Dynamic SQL you'll need|||Soren, here goes:
-- DDL & Sample Data for Parts, BOM
SET NOCOUNT ON;
USE tempdb;
GO
IF OBJECT_ID('dbo.BOM') IS NOT NULL
DROP TABLE dbo.BOM;
GO
IF OBJECT_ID('dbo.Parts') IS NOT NULL
DROP TABLE dbo.Parts;
GO
CREATE TABLE dbo.Parts
(
partid INT NOT NULL PRIMARY KEY,
partname VARCHAR(25) NOT NULL
);
INSERT INTO dbo.Parts(partid, partname) VALUES( 1, 'Black Tea');
INSERT INTO dbo.Parts(partid, partname) VALUES( 2, 'White Tea');
INSERT INTO dbo.Parts(partid, partname) VALUES( 3, 'Latte');
INSERT INTO dbo.Parts(partid, partname) VALUES( 4, 'Espresso');
INSERT INTO dbo.Parts(partid, partname) VALUES( 5, 'Double Espresso');
INSERT INTO dbo.Parts(partid, partname) VALUES( 6, 'Cup Cover');
INSERT INTO dbo.Parts(partid, partname) VALUES( 7, 'Regular Cup');
INSERT INTO dbo.Parts(partid, partname) VALUES( 8, 'Stirrer');
INSERT INTO dbo.Parts(partid, partname) VALUES( 9, 'Espresso Cup');
INSERT INTO dbo.Parts(partid, partname) VALUES(10, 'Tea Shot');
INSERT INTO dbo.Parts(partid, partname) VALUES(11, 'Milk');
INSERT INTO dbo.Parts(partid, partname) VALUES(12, 'Coffee Shot');
INSERT INTO dbo.Parts(partid, partname) VALUES(13, 'Tea Leaves');
INSERT INTO dbo.Parts(partid, partname) VALUES(14, 'Water');
INSERT INTO dbo.Parts(partid, partname) VALUES(15, 'Sugar Bag');
INSERT INTO dbo.Parts(partid, partname) VALUES(16, 'Ground Coffee');
INSERT INTO dbo.Parts(partid, partname) VALUES(17, 'Coffee Beans');
CREATE TABLE dbo.BOM
(
partid INT NOT NULL REFERENCES dbo.Parts,
assemblyid INT NULL REFERENCES dbo.Parts,
unit VARCHAR(3) NOT NULL,
qty DECIMAL(8, 2) NOT NULL,
UNIQUE(partid, assemblyid),
CHECK (partid <> assemblyid)
);
INSERT INTO dbo.BOM(partid, assemblyid, unit, qty)
VALUES( 1, NULL, 'EA', 1.00);
INSERT INTO dbo.BOM(partid, assemblyid, unit, qty)
VALUES( 2, NULL, 'EA', 1.00);
INSERT INTO dbo.BOM(partid, assemblyid, unit, qty)
VALUES( 3, NULL, 'EA', 1.00);
INSERT INTO dbo.BOM(partid, assemblyid, unit, qty)
VALUES( 4, NULL, 'EA', 1.00);
INSERT INTO dbo.BOM(partid, assemblyid, unit, qty)
VALUES( 5, NULL, 'EA', 1.00);
INSERT INTO dbo.BOM(partid, assemblyid, unit, qty)
VALUES( 6, 1, 'EA', 1.00);
INSERT INTO dbo.BOM(partid, assemblyid, unit, qty)
VALUES( 7, 1, 'EA', 1.00);
INSERT INTO dbo.BOM(partid, assemblyid, unit, qty)
VALUES(10, 1, 'EA', 1.00);
INSERT INTO dbo.BOM(partid, assemblyid, unit, qty)
VALUES(14, 1, 'mL', 230.00);
INSERT INTO dbo.BOM(partid, assemblyid, unit, qty)
VALUES( 6, 2, 'EA', 1.00);
INSERT INTO dbo.BOM(partid, assemblyid, unit, qty)
VALUES( 7, 2, 'EA', 1.00);
INSERT INTO dbo.BOM(partid, assemblyid, unit, qty)
VALUES(10, 2, 'EA', 1.00);
INSERT INTO dbo.BOM(partid, assemblyid, unit, qty)
VALUES(14, 2, 'mL', 205.00);
INSERT INTO dbo.BOM(partid, assemblyid, unit, qty)
VALUES(11, 2, 'mL', 25.00);
INSERT INTO dbo.BOM(partid, assemblyid, unit, qty)
VALUES( 6, 3, 'EA', 1.00);
INSERT INTO dbo.BOM(partid, assemblyid, unit, qty)
VALUES( 7, 3, 'EA', 1.00);
INSERT INTO dbo.BOM(partid, assemblyid, unit, qty)
VALUES(11, 3, 'mL', 225.00);
INSERT INTO dbo.BOM(partid, assemblyid, unit, qty)
VALUES(12, 3, 'EA', 1.00);
INSERT INTO dbo.BOM(partid, assemblyid, unit, qty)
VALUES( 9, 4, 'EA', 1.00);
INSERT INTO dbo.BOM(partid, assemblyid, unit, qty)
VALUES(12, 4, 'EA', 1.00);
INSERT INTO dbo.BOM(partid, assemblyid, unit, qty)
VALUES( 9, 5, 'EA', 1.00);
INSERT INTO dbo.BOM(partid, assemblyid, unit, qty)
VALUES(12, 5, 'EA', 2.00);
INSERT INTO dbo.BOM(partid, assemblyid, unit, qty)
VALUES(13, 10, 'g' , 5.00);
INSERT INTO dbo.BOM(partid, assemblyid, unit, qty)
VALUES(14, 10, 'mL', 20.00);
INSERT INTO dbo.BOM(partid, assemblyid, unit, qty)
VALUES(14, 12, 'mL', 20.00);
INSERT INTO dbo.BOM(partid, assemblyid, unit, qty)
VALUES(16, 12, 'g' , 15.00);
INSERT INTO dbo.BOM(partid, assemblyid, unit, qty)
VALUES(17, 16, 'g' , 15.00);
GO
-- Creation Script for Function fn_partsexplosion
---
-- Function: fn_partsexplosion, Parts Explosion
--
-- Input : @.root INT: Root part id
--
-- Output : @.PartsExplosion Table:
-- id and level of contained parts of input part
-- in all levels
--
-- Process : * Insert into @.PartsExplosion row of input root part
-- * In a loop, while previous insert loaded more than 0 rows
-- insert into @.PartsExplosion next level of parts
---
USE tempdb;
GO
IF OBJECT_ID('dbo.fn_partsexplosion') IS NOT NULL
DROP FUNCTION dbo.fn_partsexplosion;
GO
CREATE FUNCTION dbo.fn_partsexplosion(@.root AS INT)
RETURNS @.PartsExplosion Table
(
partid INT NOT NULL,
qty DECIMAL(8, 2) NOT NULL,
unit VARCHAR(3) NOT NULL,
lvl INT NOT NULL,
n INT NOT NULL IDENTITY, -- surrogate key
UNIQUE CLUSTERED(lvl, n) -- Index will be used to filter lvl
)
AS
BEGIN
DECLARE @.lvl AS INT;
SET @.lvl = 0; -- Initialize level counter with 0
-- Insert root node to @.PartsExplosion
INSERT INTO @.PartsExplosion(partid, qty, unit, lvl)
SELECT partid, qty, unit, @.lvl
FROM dbo.BOM
WHERE partid = @.root;
WHILE @.@.rowcount > 0 -- while previous level had rows
BEGIN
SET @.lvl = @.lvl + 1; -- Increment level counter
-- Insert next level of subordinates to @.PartsExplosion
INSERT INTO @.PartsExplosion(partid, qty, unit, lvl)
SELECT C.partid, P.qty * C.qty, C.unit, @.lvl
FROM @.PartsExplosion AS P -- P = Parent
JOIN dbo.BOM AS C -- C = Child
ON P.lvl = @.lvl - 1 -- Filter parents from previous level
AND C.assemblyid = P.partid;
END
RETURN;
END
GO
-- Parts Explosion
SELECT P.partid, P.partname, PE.qty, PE.unit, PE.lvl
FROM dbo.fn_partsexplosion(2) AS PE
JOIN dbo.Parts AS P
ON P.partid = PE.partid;
Output:
partid partname qtyn unit lvl
-- -- -- -- --
2 White Tea 1.00n EA 0
6 Cup Cover 1.00n EA 1
7 Regular Cup 1.00n EA 1
10 Tea Shot 1.00n EA 1
14 Water 205.00n mL 1
11 Milk 25.00n mL 1
13 Tea Leaves 5.00n g 2
14 Water 20.00n mL 2
-- Parts Explosion, Aggregating Parts
SELECT P.partid, P.partname, PES.qty, PES.unit
FROM (SELECT partid, unit, SUM(qty) AS qty
FROM dbo.fn_partsexplosion(2) AS PE
GROUP BY partid, unit) AS PES
JOIN dbo.Parts AS P
ON P.partid = PES.partid;
Output:
partid partname qty unit
-- -- -- --
2 White Tea 1.00 EA
6 Cup Cover 1.00 EA
7 Regular Cup 1.00 EA
10 Tea Shot 1.00 EA
13 Tea Leaves 5.00 g
11 Milk 25.00 mL
14 Water 225.00 mL
In SQL Server 2005 it would look like this:
-- CTE Solution for Parts Explosion
DECLARE @.root AS INT;
SET @.root = 2;
WITH PartsExplosionCTE
AS
(
-- Anchor member returns root part
SELECT partid, qty, unit, 0 AS lvl
FROM dbo.BOM
WHERE partid = @.root
UNION ALL
-- Recursive member returns next level of parts
SELECT C.partid, CAST(P.qty * C.qty AS DECIMAL(8, 2)),
C.unit, P.lvl + 1
FROM PartsExplosionCTE AS P
JOIN dbo.BOM AS C
ON C.assemblyid = P.partid
)
SELECT P.partid, P.partname, PE.qty, PE.unit, PE.lvl
FROM PartsExplosionCTE AS PE
JOIN dbo.Parts AS P
ON P.partid = PE.partid;
BG, SQL Server MVP
www.SolidQualityLearning.com
www.insidetsql.com
Anything written in this message represents my view, my own view, and
nothing but my view (WITH SCHEMABINDING), so help me my T-SQL code.
"Soren S. Jorgensen" <nospam@.nodomain.com> wrote in message
news:ObORLLiXGHA.3936@.TK2MSFTNGP05.phx.gbl...
> Hi,
> Does anyone got a really good/fast example on how to do a BOM with
> unlimited levels in SQL2000
> - without using cursors :)
> 2 tables (one for items, one for parent/child relations)
> In SQL2005 we of course are blessed with the new CTE, not in SQL2000 :-/
> In advance, thanks...
> Kr. Soren
>
>|||Get a copy of TREES & HIERARCHIES IN SQL for details then Google Nested
Sets model.

Tuesday, March 20, 2012

big problem with data conversion

Dear colleagues, I have a big problem with data conversion. For example, on my Windows form (C#) i have one text box and in my table I have field type float. When I use following conversion or casting:

float i = (float)Convert.ToDouble(textBox1.Text);

and insert number 12,3 in my table I have following number 12.30000000000032324!

What I must to do with this conversion and have in my table number 12,3?

Hi,

although this is not a C# forum, you should take a look at the Help for a more precise datatype than float.

http://msdn2.microsoft.com/en-us/library/b1e65aza.aspx

In addition see the float definition in the BOL for more information about float in SQL Server.

You might want to have a look on the decimal type instead.

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de

Thursday, March 8, 2012

BI Example for SQL Server 2005

Hi all,

I'm trying to find an example on BI using SQL Server 2005. Ideally something that would allow me to analyze Customer and Product information like Sales per period, per product line, etc. Something really really standard that the user could query directly from Excel 2003.

I'm sure such simple example exists, I'm looking for the DB schema, cubes, dimensions already setup in SQL 2005. My question is if the are scripts available so my only task would be to import our ERP data into SQL and make sure it gets processed.

I'm not interested in really fancy stuff SQL can deliver, just something that is easy to start with and build on.

I'd appreciate it if you can point me to any direction...

Thanks,

ST

The AdventureWorks DW sample shipping with 2005 is meant for this very purpose.

BI Example for SQL Server 2005

Hi all,

I'm trying to find an example on BI using SQL Server 2005. Ideally something that would allow me to analyze Customer and Product information like Sales per period, per product line, etc. Something really really standard that the user could query directly from Excel 2003.

I'm sure such simple example exists, I'm looking for the DB schema, cubes, dimensions already setup in SQL 2005. My question is if the are scripts available so my only task would be to import our ERP data into SQL and make sure it gets processed.

I'm not interested in really fancy stuff SQL can deliver, just something that is easy to start with and build on.

I'd appreciate it if you can point me to any direction...

Thanks,

ST

The AdventureWorks DW sample shipping with 2005 is meant for this very purpose.

Friday, February 24, 2012

Best way to this?

Hi All!
I'm setting up a game that involves pari-mutuel betting.
For example - Total Pool is 1, 213. Take total amount and divide into
bet.
Bet Odds would be:
Calculation
First- 314 4 - 1 (3.86 - 1)
(1213 / 314)
Second - 251 5 - 1 (4.83 - 1)
(1213 / 251)
Third - 414 3 - 1 (2.92 - 1) (1213 / 41
4)
Fourth - 234 5 - 1 (5.18 - 1) (1213 /
234)
Total - 1,213
I thought of setting three tables, one for totals of each place, one table
to hold the grand total, and one table for the results of the odds to go to.
And then at the end of the race, clear out all tables and do it again.
Any thoughts or better suggestions. I'm all ears!
Thanks!what do you mean one table to hold the grand total?
Is the total pool going to change?
If you are going to keep flushing the data then why do you need permanent
tables in the first place. I am sorry I don't know about this game..
Can you give an insight to it?
"Rudy" wrote:

> Hi All!
> I'm setting up a game that involves pari-mutuel betting.
> For example - Total Pool is 1, 213. Take total amount and divide into
> bet.
> Bet Odds would be:
> Calculation
> First- 314 4 - 1 (3.86 - 1)
> (1213 / 314)
> Second - 251 5 - 1 (4.83 - 1)
> (1213 / 251)
> Third - 414 3 - 1 (2.92 - 1) (1213 /
414)
> Fourth - 234 5 - 1 (5.18 - 1) (1213
/
> 234)
> Total - 1,213
> I thought of setting three tables, one for totals of each place, one table
> to hold the grand total, and one table for the results of the odds to go t
o.
> And then at the end of the race, clear out all tables and do it again.
> Any thoughts or better suggestions. I'm all ears!
> Thanks!
>|||I'm a little . Surely you don't need to store the computed data
somewhere.
You have your data (simplified, please don't criticise for lack of primary
keys, etc):
create table bets (position varchar(20), numbets int);
And you have data:
insert into bets values ('First', 314);
insert into bets values ('Second', 251);
insert into bets values ('Third', 414);
insert into bets values ('Fourth', 234);
And then you have views which show the info you want:
select *, (select sum(numbets) from bets) / convert(float,numbets) payout
from bets
But I wouldn't show the "5-1" version, because you might want to use 7-2 if
it's near 3.5 for example. Much easier just to say "Paying $1.52" or whateve
r.
Does this help?
Rob
"Rudy" wrote:

> Hi All!
> I'm setting up a game that involves pari-mutuel betting.
> For example - Total Pool is 1, 213. Take total amount and divide into
> bet.
> Bet Odds would be:
> Calculation
> First- 314 4 - 1 (3.86 - 1)
> (1213 / 314)
> Second - 251 5 - 1 (4.83 - 1)
> (1213 / 251)
> Third - 414 3 - 1 (2.92 - 1) (1213 /
414)
> Fourth - 234 5 - 1 (5.18 - 1) (1213
/
> 234)
> Total - 1,213
> I thought of setting three tables, one for totals of each place, one table
> to hold the grand total, and one table for the results of the odds to go t
o.
> And then at the end of the race, clear out all tables and do it again.
> Any thoughts or better suggestions. I'm all ears!
> Thanks!
>|||Hi Guys!
This helps Rob. I may want to keep some of the records, not sure. My main
objetive here is just to have the calculation done quickly. Omni, is just a
simple game to bet on horses.
Thanks again!!!
Rudy
"Rob Farley" wrote:
> I'm a little . Surely you don't need to store the computed data
> somewhere.
> You have your data (simplified, please don't criticise for lack of primary
> keys, etc):
> create table bets (position varchar(20), numbets int);
> And you have data:
> insert into bets values ('First', 314);
> insert into bets values ('Second', 251);
> insert into bets values ('Third', 414);
> insert into bets values ('Fourth', 234);
> And then you have views which show the info you want:
> select *, (select sum(numbets) from bets) / convert(float,numbets) payout
> from bets
> But I wouldn't show the "5-1" version, because you might want to use 7-2 i
f
> it's near 3.5 for example. Much easier just to say "Paying $1.52" or whate
ver.
> Does this help?
> Rob
>
> "Rudy" wrote:
>|||Well you can always save the contents of the view to an archive table after
betting stops. That's not going to be hard to do. But while the numbers are
changing, just let your view change with it.
"Rudy" wrote:
> Hi Guys!
> This helps Rob. I may want to keep some of the records, not sure. My main
> objetive here is just to have the calculation done quickly. Omni, is just
a
> simple game to bet on horses.
> Thanks again!!!
> Rudy
> "Rob Farley" wrote:
>