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 8295481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:36:28+00:00 2026-06-08T14:36:28+00:00

I have a database column which type is bytea. It contains floats converted as

  • 0

I have a database column which type is bytea. It contains floats converted as byte array (4 bytes per one float) and encoding is Escape. I’m be able to retrieve corresponding bytea string using substring function.

My question is how can I convert bytea string to float inside a SQL function. Earlier I converted to float in C# side. I used dataReader.getByte method to retrieve bytes and then Converted to float using BitConverter.ToSingle (.Net build in class) method.

Now I can’t use intermediate component as Npqsql driver. I want SQL to directly convert bytea into floats and return the corresponding number when execute a query from 3rd party application.

Thanks
Amila

  • 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-06-08T14:36:30+00:00Added an answer on June 8, 2026 at 2:36 pm

    For this purpose the best possible solution is to convert into bytes using IEEE754-1985 standard using SQL commands.

    First it is required to check for special cases defined by IEEE754-1985 standard. Then just follow the standard algorithm to convert if it is not in any special cases. Sample code is below.

    Inputs are bytea_value bytea, is_little_endian boolean then divide into 4 bytes as below:

      byte_array[0]:= get_byte(bytea_value, 0);
      byte_array[1]:= get_byte(bytea_value, 1);
      byte_array[2]:= get_byte(bytea_value, 2);
      byte_array[3]:= get_byte(bytea_value, 3);
    

    Then get the binary value by considering little endian or big endian

    IF is_little_endian THEN
            binary_value:= byte_array[0]::bit(8) || byte_array[1]::bit(8) || byte_array[2]::bit(8) || byte_array[3]::bit(8);
        ELSE
            binary_value:= byte_array[3]::bit(8) || byte_array[2]::bit(8) || byte_array[1]::bit(8) || byte_array[0]::bit(8); 
        END IF;
    

    Now check for special cases:

    IF binary_value = '00000000000000000000000000000000' OR binary_value = '10000000000000000000000000000000' THEN -- IEEE754-1985 Zero
            return 0.0;
        END IF;
    
    sign := substring(binary_value from 1 for 1);
        exponent := substring(binary_value from 2 for 8);
        mantissa := substring(binary_value from 10 for 23); 
    
        IF exponent = '11111111' THEN
            IF mantissa = '00000000000000000000000' THEN   -- IEEE754-1985 negative and positive infinity
                IF sign = '1' THEN                    
                    return '-Infinity';                    
                ELSE                    
                    return 'Infinity';  
                END IF;                  
            ELSE
              return 'NaN'; -- IEEE754-1985 Not a number
            END IF; 
        END IF;
    

    If it does not belong to any special cases just convert it as below:

    exp := exponent::int;
    
        IF exp > 126 THEN
         exp := exp - 127;
        ELSE
         exp:= -exp;
        END IF;
    
        WHILE mantissa_index < 24 LOOP
            IF substring(mantissa from mantissa_index for 1) = '1' THEN
                result := result + power(2, -(mantissa_index));
            END IF;
            mantissa_index = mantissa_index + 1;
        END LOOP;
    
        result := result * power(2, exp);
    
        IF(sign = '1') THEN
            result = -result;
        END IF;
    
        return result;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a large mysql database table in which one column contains values ranging
I have a database column which contains varchar data type. I would like to
I have a column type money in my sql server database, which is moneyAcumulated
I have a database table named 'student' in which there is one column named
we have a table in an Oracle Database which contains a column with the
I have one database table which contains 8 columns. One of the columns is
I have a table, which contains many columns of float data type with 15
I have a database with one column which is labeled as text. I want
I have a database table which contains an ID column and a Name column.
I have a column in my database of data type Geography which I want

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.