Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Determine bit length?

Author  Topic 

chriskhan2000
Aged Yak Warrior

544 Posts

Posted - 2005-03-09 : 09:01:07
I want to do an insert to a binary table, but was wondering how I would determine the bit length.

Here's what I have for my asp code:

"INSERT INTO binary " _
& "(ID, TYPE, BITS, BIT_LENGTH) " _
& "Values ('" & strID & "', 'X', '" & txbSpecs.Text & "', '')"

For the Bit_Length I have no idea to put it there so my above code doesn't work because bit_length field can't be null. Any suggestions?

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2005-03-09 : 09:35:40
use DATALENGTH function

Go with the flow & have fun! Else fight the flow
Go to Top of Page

chriskhan2000
Aged Yak Warrior

544 Posts

Posted - 2005-03-09 : 09:44:35
Thanks for the info, but where would the datalength go? I'm a newbie with SQL query so I have no idea.

Does it go into my insert query or where? I see examples where it does a Select DataLength(fieldname) As Datalength From Table, but where or how would I incorporate that with my Insert query.
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-03-09 : 09:51:11
What datatype is your "BITS" column ? Why do you need to store the length? Seems like it could be derived whenever needed.

- Jeff
Go to Top of Page

chriskhan2000
Aged Yak Warrior

544 Posts

Posted - 2005-03-09 : 09:57:12
Datatype is Int. I try leaving it blank, but it says that it can't be Null when I do an insert.
Go to Top of Page

chriskhan2000
Aged Yak Warrior

544 Posts

Posted - 2005-03-09 : 10:00:25
Sorry bits is image data type. I got that mixed with the bits length.
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2005-03-09 : 10:05:20
as jeff said you don't need another column for the length when you can yust call datalegth in the select when you're returning data.

Go with the flow & have fun! Else fight the flow
Go to Top of Page

chriskhan2000
Aged Yak Warrior

544 Posts

Posted - 2005-03-09 : 10:16:26
I'm confused. So in my insert I can leave the bits_length field out? Cuz it's giving me error.

THe thing is I'm not doing a select statement, but an insert. I don't see any examples with the DATALENGTH being use for an insert statememt.
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2005-03-09 : 10:20:14
the point is you don't even need a BIT_LENGTH column in your table.
you will probably need to retreive data from the table someday...
when the day arrives you'll do:
select ID, TYPE, BITS, datalength(BITS) as BIT_LENGTH from binary
therefore you don't need that extra column.

Go with the flow & have fun! Else fight the flow
Go to Top of Page

chriskhan2000
Aged Yak Warrior

544 Posts

Posted - 2005-03-09 : 10:29:29
I think we are not in the same page here. Let me explain.

This database that I'm doing the insert in is from an application. I can't go in and change or remove this field from this table. It is there with all the fields, and the bits_length field is set so that it can't be null.

The problem is when I try doing an Insert, I need to determine the length of the bits so that I can input the correct bits_length. I wish I can just ignore this field when doing an insert, but it's not happy unless I have some kind of data in that field.
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2005-03-09 : 10:38:22
well Len(txbSpecs.Text) or LenB(txbSpecs.Text) doesn't work?

Go with the flow & have fun! Else fight the flow
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-03-09 : 10:42:54
I don't think you can insert an Image into a table this way ... wouldn't you need to use parameters? (or, better yet, SHOULDN'T you? ) What happens if the image is somewhat big?

I cannot imagine that what you are tyring will work. YOu should really use stored procedures and parameters, and not try to concatenate a sql string and convert things to text strings.

Either way, the layer of your applicaiton that is passing the image into the table should be able to determine its size -- it's not really a SQL question if the applicaiton layer needs to provide that info.

- Jeff
Go to Top of Page

chriskhan2000
Aged Yak Warrior

544 Posts

Posted - 2005-03-09 : 10:57:30
Thanks for the example, but that doesn't work.

It's trying to insert new data from txbSpecs.Text textbox to Binary table, so it is not pulling it from database and count the length.

I was thinking about counting the characters in that textbox and then insert that value into the bitlengths.

Anyone know the syntax to count characters for aspx pages?
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-03-09 : 11:22:17
Why are you trying to put a text string from a textbox into an image column in your database? Why not a TEXT datatype? Are you trying to store binary data or text?

- Jeff
Go to Top of Page

chriskhan2000
Aged Yak Warrior

544 Posts

Posted - 2005-03-09 : 12:10:54
i'm putting it in as binary.

CAST(" & txbSpecs.Text & " as varbinary(8000))
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-03-09 : 12:19:27
WHY are you doing this??? why not varchar ??? You said the datatype is an image datatype -- is it that or varbinary?

- Jeff
Go to Top of Page

chriskhan2000
Aged Yak Warrior

544 Posts

Posted - 2005-03-09 : 12:46:33
Hehe. It says it's image type, but what's in there looks like binary.
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-03-09 : 12:48:42
To reiterate:

WHY are you doing this??? why not varchar ???


- Jeff
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2005-03-09 : 12:51:20
i must agree with jeff on this one...
but to answer your question:
"INSERT INTO binary " _
& "(ID, TYPE, BITS, BIT_LENGTH) " _
& "select '" & strID & "', 'X', CAST('" & txbSpecs.Text & "' as varbinary(8000)), datalength(CAST('" & txbSpecs.Text & "' as varbinary(8000))))"

and why doesn't LenB() work??

Go with the flow & have fun! Else fight the flow
Go to Top of Page

chriskhan2000
Aged Yak Warrior

544 Posts

Posted - 2005-03-09 : 14:26:41
Thanks Spirit1. That works. I'm fairly new to SQL Query so that's why I didn't know that you can put a select in the Insert.
Go to Top of Page
   

- Advertisement -