Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6911151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:54:54+00:00 2026-05-27T08:54:54+00:00

I save my long value in a SQL Server table as varbinary(max) : var

  • 0

I save my long value in a SQL Server table as varbinary(max):

var savedValue = BitConverter.GetBytes(longValue);

Now I need to work with that value in T-SQL query, but when I trying to get value:

select cast(Value as bigint) from dbo.MyValues

It returns different number value. For example if I saved -8588797048854775808 in .NET, in T-SQL I get 33802181122903688

Please tell me what’s the problem? Have that issue any solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:54:55+00:00Added an answer on May 27, 2026 at 8:54 am

    Casting from varbinary to bigint (and back) uses network byte order (big-endian).
    BitConverter uses the endian-ness of the machine it is run on (little-endian for x86 and x64).

    Hence BitConverter.GetBytes run on -8588797048854775808 (0x88CE7696E7167800) is {0x00,0x88,0xE9,0x18,0x69,0x89,0x31,0x77}, and cast on {0x00,0x88,0xE9,0x18,0x69,0x89,0x31,0x77} is 0x0088E91869893177 = 38536887891734903.

    The obvious thing to do is to just store 64-bit integers as 64-bit integers in the first place.

    If you really need to do this conversion then:

    var savedValue = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(longValue))
    

    Will swap around the bytes, while also being portable in that it won’t swap the bytes if run on a big-endian machine.

    Alternatively, if you don’t want to use the System.Net namespace for some reason, or if you want to be extensible to types other than the three IPAddress.HostToNetworkOrder handeles, use:

    var savedValue = BitConverter.GetBytes(longValue);
    if(BitConverter.IsLittleEndian)
      Array.Reverse(savedValue);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to save a big file, sometime it takes a very long time
I have a SQL script that will insert a long string into a table.
In my embedded c program I have a struct: struct var{ unsigned long value;
I have a table with the column's datatype as xml in SQL Server 2005.
i save in mysql database the time using NOW() function. how can i change
I need to save a user's login information in encrypted form for this application
When adding a new record, the Key Attribute property does not save it's value
I have a numeric database field (numeric(3) in SQL Server 2000) that allows nulls,
How can I save the current selected spinner value, such that when I reopen
I need to grab certain parts from many tables, save them and finally generate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.