How can I have a checksum (be it MD5 or anything else) of a image column calculated in SQL Server? i.e. something like
SELECT HASHBYTES('md5', d.data)
FROM ATTACHMENTDATA D
8116 Argument data type image is invalid for argument 2 of hashbytes
function
Trying:
SELECT HASHBYTES('md5', CONVERT(nvarchar(4000), d.data))
FROM ATTACHMENTDATA D
results in:
529 Explicit conversion from data type image to nvarchar is not
allowed
If that matters I use Microsoft SQL Server 10.50.4000 (SQL Server 2008 R2)
Cast data column to VARBINARY:
SELECT HASHBYTES('md5', CAST(d.data AS VARBINARY(MAX)))
FROM ATTACHMENTDATA D
MD5 is weak hash algorithm use SHA1, SHA2_256 or SHA2_512 instead.
Note also that: IMAGE datatype is deprecated.
try converting it to a VARBINARY(MAX)
Also, see this note from Microsoft at https://msdn.microsoft.com/en-za/library/ms187993.aspx
Related
I am facing a problem when applying SHA256 hash to German Umlaut Characters.
--Without Umlaut
SELECT CONVERT(VARCHAR(MAX), HASHBYTES('SHA2_256','o'), 2) as HASH_ID
Sql server Output 65C74C15A686187BB6BBF9958F494FC6B80068034A659A9AD44991B08C58F2D2
This is matching to the output in
https://www.pelock.com/products/hash-calculator
--With Umlaut
SELECT CONVERT(VARCHAR(MAX), HASHBYTES('SHA2_256','ö'), 2)
Sql server Output B0B2988B6BBE724BACDA5E9E524736DE0BC7DAE41C46B4213C50E1D35D4E5F13
Output from pelock: 6DBD11FD012E225B28A5D94A9B432BC491344F3E92158661BE2AE5AE2B8B1AD8
I want the SQL server output to match to pelock. I have tested outputs from other sources (Snowflake and python) and all of it aligns with output from pelock. Not sure why SQL server is not giving the right result. Any help is much appreciated.
You have two issues:
The literal text itself is being reinterpreted, because you have the wrong database collation. You can use the N prefix to prevent that, but this leads to a second problem...
The value from pelock is UTF-8, but using N means it will be UTF-16 nvarchar.
So you need to use a UTF-8 binary collation, the N prefix and cast it back to varchar.
SELECT CONVERT(VARCHAR(MAX), HASHBYTES('SHA2_256',CAST(N'ö' COLLATE Latin1_General_100_BIN2_UTF8 AS varchar(100))), 2)
Result
6DBD11FD012E225B28A5D94A9B432BC491344F3E92158661BE2AE5AE2B8B1AD8
db<>fiddle
UTF-8 collations are only supported in SQL Server 2019 and later. In older version you would need to find a different collation that deals with the characters you have. It may not be possible to find a collation that deals with all of your data.
I am using open query to retrieve data from IBM db2 to SQL server.
Below is the sample query used
select top 10 * from OpenQuery(Link server, 'Select columnName from table where column2=15' )
The columnName needs to be converted / cast in Unicode format to accept multi lingual characters. How to use casting in the inner query?
My issue is similar to that of https://www.sqlservercentral.com/forums/997384/linker-server-to-as400-db2-character-translation-problems
I want to retrieve the data in Thai and Chinese characters. I have rows of data which are to be in Thai and Chinese characters. But the data shows as garbled when I use the command which I have provided. The column type in sql server is defined as nvarchar.
Cast your column with the CCSID code xxxx corresponding to your language :
select cast(columnName as char(14) CCSID xxxx) from ...
Here I have a question about data converting regarding SQL Server and SYBASE.
Take 71632.0638353154 as an example, how to convert it to 71,632.06 in SQL Server and SYBASE?
I know that there is a convert() function in SQL Server and SYBASE, but every time when I tried to use it to convert that number, the database UI will throw me an exception.
I use sybase UI to excute below SQL instance:
select convert(varchar(30),convert(varchar(8),convert(money,71632.0638353154),1))
but this causes this error:
Insufficient result space for explicit conversion of MONEY value '71,632.06' to a VARCHAR field.
Would anyone tell me how to do it? thx.
I'm unable to test in Sybase but
SELECT CONVERT(VARCHAR(30), CAST(71632.0638353154 AS MONEY),1)
works for me in SQL Server.
The more generic solution is:
select cast(71632.0638353154 as decimal(10, 2))
The cast function is standard SQL and available in most databases. DECIMAL is a built-in data type.
In a SQL Server 2000 DB, I have a table which holds string representations of Oracle DB dates. They are formatted like "16-MAY-12". I need to convert these to datetime. I can not seem to find a conversion style number that matches, nor can I find a function that will allow me to specify the input format. Any ideas?
This seems to work for me:
SELECT CONVERT(DATETIME, '16-MAY-12');
You can also try using TO_CHAR() to convert the Oracle values to a more SQL Server-friendly format (best is YYYYMMDD) before pulling them out of the darker side.
Follow Aaron's advice and cast to string on the Oracle side and then did a check/recast on the MS SQL side. See example below:
;WITH SOURCE AS (
SELECT * FROM openquery(lnk,
'SELECT
TO_CHAR(OFFDATE , ''YYYY-MM-DD HH24:MI:SS'') AS OFFDATE,
FROM
ORACLE_SOURCE')),
SOURCE_TRANSFORM AS
(
SELECT
CASE
WHEN ISDATE(OFFDATE) = 1 THEN CAST(OFFDATE AS DATETIME)
ELSE NULL END AS OFFDATE
FROM
SOURCE
)
SELECT * FROM SOURCE_TRANSFORM
Is there some efficient way how to get length of data in "varbinary(max) filestream" column?
I found only samples with conversion to varchar and then calling the "LEN" function.
SELECT
length = DATALENGTH(Name),
Name
FROM
Production.Product
ORDER BY
Name
"Returns the number of bytes used to represent any expression."
T-SQL and quote taken from MSDN's DATALENGTH (Transact-SQL) library.