Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Function to encode MD5 in SQL-Server 2012

Posted on 2014-10-27
3
Medium Priority
?
925 Views
Last Modified: 2014-10-27
Hi.

I have created this function to get the MD5 code of a text:

FUNCTION [dbo].[fn_md5] (@data_a_codificar varchar) 
RETURNS CHAR(32) AS
BEGIN
  RETURN convert(varchar,hashbytes('MD5',@data_a_codificar),2)
END

Open in new window


Then I execute these two sentences:

select dbo.fn_md5('Hello');

select convert(char,hashbytes('MD5','Hello'),2)

Open in new window


Why aren't they returning the same values?
0
Comment
Question by:gplana
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
johny_bravo1 earned 2000 total points
ID: 40406163
Change your function

ALTER FUNCTION [dbo].[fn_md5] (@data_a_codificar varchar(32))
RETURNS CHAR(32) AS
BEGIN
  RETURN convert(varchar(32),hashbytes('MD5',@data_a_codificar),2)
END

             
select dbo.fn_md5('Hello');

select convert(varchar(32),hashbytes('MD5','Hello'),2)
0
 
LVL 15

Author Comment

by:gplana
ID: 40406178
Thanks, but still different values. Here is the result of the two selects:

1A8E9B7EA00C8125B37DC3F231564C72
8B1A9953C4611296A827ABF8C47804D7
0
 
LVL 15

Author Closing Comment

by:gplana
ID: 40406181
I have mispelled your solution. It works fine.

Thank you.
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question