why does my binary conversion to varchar return an empty string?

db042190 1,521 Reputation points
2022-04-07T15:16:03.52+00:00

Hi, I'm running 2017 and the following script isnt returning a varchar visual of my binary(8) column. I tried different sizes and a sub select but nothing works...i'll post the image next. I got the approach from https://stackoverflow.com/questions/45051735/convert-binary-to-varchar

USE [research]
drop table testbinary
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[TestBinary](
 [Id] [int] IDENTITY(1,1) NOT NULL,
 Testcolumn char(1),
 [RV] rowversion,
CONSTRAINT [PK_TestBinary] PRIMARY KEY CLUSTERED 
(
 [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
insert testbinary (testcolumn) select 'y'
select max(RV),convert(varchar(max),max(RV),1) from testbinary
Developer technologies Transact-SQL
0 comments No comments
{count} votes

Accepted answer
  1. amomen 381 Reputation points
    2022-04-07T15:31:19.347+00:00

    Hi,

    Convert it first to varbinary and then varchar like this:

     select max(RV),CONVERT(varchar(max),convert(VARBINARY(100),max(RV)),1) from testbinary
    

2 additional answers

Sort by: Most helpful
  1. db042190 1,521 Reputation points
    2022-04-07T15:19:02.173+00:00

    this is an image of the result set returned

    190999-capturebinaryquestion.png

    0 comments No comments

  2. Jingyang Li 5,896 Reputation points Volunteer Moderator
    2022-04-07T17:39:54.453+00:00

    master.[sys].fn_varbintohexstr

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.