Using Hex values

  • Hello
      I have a scenario where in I am using a Hex value, to get a meaningful value I need to append 00 to the original hex value and then reverse it and convert to integer
      Can some one suggest me a better way to do this, I am using the following Query but am unable to get any results.

      select  reverse(Data1+data2)  -- where data1 is the original hex value and data2 is 00  Thanks in advance.
        
      
      IQ

  • IQ1 - Thursday, February 15, 2018 10:47 AM

    Hello
      I have a scenario where in I am using a Hex value, to get a meaningful value I need to append 00 to the original hex value and then reverse it and convert to integer
      Can some one suggest me a better way to do this, I am using the following Query but am unable to get any results.

      select  reverse(Data1+data2)  -- where data1 is the original hex value and data2 is 00  Thanks in advance.
        
      
      IQ

    Why not convert straight to numerical data type?
    😎


    DECLARE @MHEX VARBINARY(2) = 0x80;
    SELECT
      CONVERT(INT,@MHEX,0) AS INT_VAL;

    Output

    INT_VAL
    128

  • IQ1 - Thursday, February 15, 2018 10:47 AM

    Hello
      I have a scenario where in I am using a Hex value, to get a meaningful value I need to append 00 to the original hex value and then reverse it and convert to integer
      Can some one suggest me a better way to do this, I am using the following Query but am unable to get any results.

      select  reverse(Data1+data2)  -- where data1 is the original hex value and data2 is 00  Thanks in advance.
        
      
      IQ

    It would be helpful if you posted the original Hex value and the expected integer result for people to be able to hit the mark you're expecting.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden - Friday, February 16, 2018 4:39 AM

    IQ1 - Thursday, February 15, 2018 10:47 AM

    Hello
      I have a scenario where in I am using a Hex value, to get a meaningful value I need to append 00 to the original hex value and then reverse it and convert to integer
      Can some one suggest me a better way to do this, I am using the following Query but am unable to get any results.

      select  reverse(Data1+data2)  -- where data1 is the original hex value and data2 is 00  Thanks in advance.
        
      
      IQ

    It would be helpful if you posted the original Hex value and the expected integer result for people to be able to hit the mark you're expecting.

    I'm curious about why the reverse!
    It's a bit obtuse for endian :ermm:

    Far away is close at hand in the images of elsewhere.
    Anon.

  • Thanks for your suggestions, hope this table helps in explaining what  I am looking for.

      Initial Value

    Appending 00 to the value

    Reversed Reding( Final Reading required)

    0xE3C8F405

    0x05F4C8E3

    0x05F4C8E3

    0x6F4001

    0x6F400100

    0x0001406F

    0x10FB

    0x10FB0000

    0x0000FB10

    0xE5

    0xE5000000

    0x000000E5


     

  • So it looks like it's a formatting thing, not any sort of manipulation of the data itself?
    You're trying to get the existing hex data to be 8 characters (plus the 0x?)

    If that's the case, it might be better to do that in the front-end (application) code, it's not really something SQL is intended to do.

  • I know SQL is not intended to do this but for sure there may be a way to do in SQL also.

  • jasona.work - Friday, February 16, 2018 8:14 AM

    So it looks like it's a formatting thing, not any sort of manipulation of the data itself?
    You're trying to get the existing hex data to be 8 characters (plus the 0x?)

    If that's the case, it might be better to do that in the front-end (application) code, it's not really something SQL is intended to do.

    +1

    However, a possibility

    SELECT [value],
     CONVERT(varbinary(8),'0x'+
     REPLACE(SUBSTRING(CONVERT(char(8), [value], 2),7,2),'  ','00')+
     REPLACE(SUBSTRING(CONVERT(char(8), [value], 2),5,2),'  ','00')+
     REPLACE(SUBSTRING(CONVERT(char(8), [value], 2),3,2),'  ','00')+
     REPLACE(SUBSTRING(CONVERT(char(8), [value], 2),1,2),'  ','00')
     ,1)
    FROM (VALUES
     (CAST(0xE3C8F405 as varbinary)),
     (CAST(0x6F4001 as varbinary)),
     (CAST(0x10FB as varbinary)),
     (CAST(0xE5 as varbinary))
     ) a ([value]);

    Far away is close at hand in the images of elsewhere.
    Anon.

  • jasona.work - Friday, February 16, 2018 8:14 AM

    If that's the case, it might be better to do that in the front-end (application) code, it's not really something SQL is intended to do.

    I'll never understand why people say that such data manipulation is "not really something SQL is intended to do".  The acronym of "DML" stands for "Data Manipulation Language".  😉

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden - Friday, February 16, 2018 8:27 AM

    jasona.work - Friday, February 16, 2018 8:14 AM

    If that's the case, it might be better to do that in the front-end (application) code, it's not really something SQL is intended to do.

    I'll never understand why people say that such data manipulation is "not really something SQL is intended to do".  The acronym of "DML" stands for "Data Manipulation Language".  😉

    Unless its done on a large table, in a loop, row-by-row, in the OLTP, during the heaviest business hours, making the poor production server scream for help.

  • Another possibility...

     SELECT  Original = hv.HexValue
            ,Final    = CONVERT(BINARY(4),REVERSE(CONVERT(BINARY(4),(hv.HexValue))))
       FROM (--==== This is just test data
            VALUES
             (0xE3C8F405)
            ,(0x6F4001)
            ,(0x10FB)
            ,(0xE5)
            ) hv (HexValue)
    ;

    Results:

    Original Final
    ---------- ----------
    0xE3C8F405 0x05F4C8E3
    0x6F4001 0x0001406F
    0x10FB 0x0000FB10
    0xE5 0x000000E5

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden - Friday, February 16, 2018 8:27 AM

    jasona.work - Friday, February 16, 2018 8:14 AM

    If that's the case, it might be better to do that in the front-end (application) code, it's not really something SQL is intended to do.

    I'll never understand why people say that such data manipulation is "not really something SQL is intended to do".  The acronym of "DML" stands for "Data Manipulation Language".  😉

    I don't know that I'd agree that adding leading zeroes to a hex value falls under the intent of DML...

    I'm not saying it's not possible (after all, there's already at least one possible solution posted here, but to my mind, DML would be converting those hex values to integers, or character strings, not padding them with leading zeroes...

  • ManicStar - Friday, February 16, 2018 8:45 AM

    Jeff Moden - Friday, February 16, 2018 8:27 AM

    jasona.work - Friday, February 16, 2018 8:14 AM

    If that's the case, it might be better to do that in the front-end (application) code, it's not really something SQL is intended to do.

    I'll never understand why people say that such data manipulation is "not really something SQL is intended to do".  The acronym of "DML" stands for "Data Manipulation Language".  😉

    Unless its done on a large table, in a loop, row-by-row, in the OLTP, during the heaviest business hours, making the poor production server scream for help.

    Heh... Ok... I'll disagree with that.  😉  Such simple manipulations are a whole lot less expensive than having an outside source have to read from the server and then put it back not to mention the cost of developing external code, testing it, and maintaining it.  There are also a whole lot of times when such things don't have a GUI readily available like when converting ETL imports, etc.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • jasona.work - Friday, February 16, 2018 8:46 AM

    Jeff Moden - Friday, February 16, 2018 8:27 AM

    jasona.work - Friday, February 16, 2018 8:14 AM

    If that's the case, it might be better to do that in the front-end (application) code, it's not really something SQL is intended to do.

    I'll never understand why people say that such data manipulation is "not really something SQL is intended to do".  The acronym of "DML" stands for "Data Manipulation Language".  😉

    I don't know that I'd agree that adding leading zeroes to a hex value falls under the intent of DML...

    I'm not saying it's not possible (after all, there's already at least one possible solution posted here, but to my mind, DML would be converting those hex values to integers, or character strings, not padding them with leading zeroes...

    It's a simple conversion to normalize some data that's no different than casting GETDATE() to a DATE datatype to strip off the time element.  You wouldn't send that little bit of computational heaven to the GUI, would you?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden - Friday, February 16, 2018 8:56 AM

    jasona.work - Friday, February 16, 2018 8:46 AM

    Jeff Moden - Friday, February 16, 2018 8:27 AM

    jasona.work - Friday, February 16, 2018 8:14 AM

    If that's the case, it might be better to do that in the front-end (application) code, it's not really something SQL is intended to do.

    I'll never understand why people say that such data manipulation is "not really something SQL is intended to do".  The acronym of "DML" stands for "Data Manipulation Language".  😉

    I don't know that I'd agree that adding leading zeroes to a hex value falls under the intent of DML...

    I'm not saying it's not possible (after all, there's already at least one possible solution posted here, but to my mind, DML would be converting those hex values to integers, or character strings, not padding them with leading zeroes...

    It's a simple conversion to normalize some data that's no different than casing GETDATE() to a DATE datatype to strip off the time element.  You wouldn't send that little bit of computational heaven to the GUI, would you?

    OK, chalk up my response to not being familiar with how hex is handled in SQL (I'm a production DBA, not a Dev DBA... 😉 )
    Seeing as SQL sees, and "stores" 0xE5 and 0x000000E5 the same, obviously I was heading down the wrong track.

    Learn something new every day!

    Oh, and no, I wouldn't expect someone to have to use the front end to strip the time from a datetime field (or displaying just the date from a getdate())
    😀:hehe:
    But, just to annoy you, Jeff, if they did it by converting the getdate() to a string, reversing it,
    then using a cursor to loop through until they reached the end of the time and collected the remaining characters before reversing it again and then converted it to a date, I'd be fine with that...
    :hehe::hehe:

Viewing 15 posts - 1 through 15 (of 23 total)

You must be logged in to reply to this topic. Login to reply