SQL Server Insert Varbinary - sql

I need to take a Varbinary(max) from a SQL Server column and insert it into another varbinary column, however my image does not render when displayed via the new field, what am I doing wrong?
I've copied from SQL Server, ie
E7D28201A240202178EF491E413EB4DDBCFB54A15077A989A37A0C5E4E0F143AEE7000CE3F3A70C0E4F342E524C8F4A7243888AA581EC051B030E7A8EF4E233DF.....
and then
update client
set Photo = convert(varbinary(max),'E7D28201A240202178EF491E413EB4DDBCFB54A15077A989A37A0C5E4E0F143AEE7000CE3F3A70C0E4F342E524C8F4A7243888AA581EC051B030E7A8EF4E233DF.....')
where id='40946b09-81be-42b0-ae62-63182acb1e89'

The data is not Base64 encoded as that would have a much larger range of alpha characters, whereas this value is pure hex values (i.e. 0-9, A-F). Also, the example HTML code provided by the O.P. (in a comment on the Question) is:
<img src="data:image;base64,#System.Convert.ToBase64String(Model.PhotoDisplay)
which shows that the source value of Model.PhotoDisplay is first converted to Base64 so that the data:image;base64 type designation will be correct.
The problem is that SQL Server, when asked to convert the value, does not know that it is already a hex/binary string. You need to tell SQL Server what the format of the source data is. This can be done in two ways:
Depending on how you are building this value, you just need to add the 0x prefix and remove the single-quotes:
UPDATE cl
SET cl.Photo = CONVERT(VARBINARY(MAX), 0xE7D28201A24020.....)
FROM Client cl
WHERE cl.[id] = '40946b09-81be-42b0-ae62-63182acb1e89';
If you need to keep the value as a string, and are using SQL Server 2008 or newer, then you can use the "style" option of the CONVERT function to tell SQL Server that it is a binary value, and you have the additional option of
adding the 0x prefix and using a "style" of 1:
UPDATE cl
SET cl.Photo = CONVERT(VARBINARY(MAX), '0xE7D28201A24020.....', 1)
FROM Client cl
WHERE cl.[id] = '40946b09-81be-42b0-ae62-63182acb1e89';
keep the value without the 0x prefix and using a "style" of 2:
UPDATE cl
SET cl.Photo = CONVERT(VARBINARY(MAX), 'E7D28201A24020.....', 2)
FROM Client cl
WHERE cl.[id] = '40946b09-81be-42b0-ae62-63182acb1e89';
But, if this data really is coming from a VARBINARY(MAX) field in another table, then how is it ending up as a string in the first place? The easiest way to move this is to either:
Transfer the data directly between tables:
UPDATE cl
SET cl.Photo = src.SomeField
FROM Client cl
INNER JOIN SomeTable src
ON src.JoinField = cl.JoinField
WHERE something? = somethingElse?;
Use a VARBINARY(MAX) variable:
DECLARE #Photo VARBINARY(MAX);
SELECT #Photo = src.photo
FROM SomeTable src
WHERE src.SomeField = ?;
UPDATE cl
SET cl.Photo = #Photo
FROM Client cl
WHERE cl.[id] = '40946b09-81be-42b0-ae62-63182acb1e89';

Why not copy it directly using an SQL statement, rather than copy/pasting from SSMS?
I'll leave an example to copy the data from another client (bogus id for the example):
UPDATE client
SET Photo = (SELECT Photo FROM client WHERE id='00000000-0000-0000-0000-000000000000')
WHERE id='40946b09-81be-42b0-ae62-63182acb1e89';

Related

UTF8 changed to Latina 1 - Umlauts are not considered

I am currently updating a table using the UPDATE command, whereby a text section is also read from another table using a substring. The command works fine so far. The only problem are the umlauts, which are not taken into account during the update. As I found out, for some reason the substring is rewritten into the format latina1, although the corresponding column of the table (action) has utf8 preset. Enclosed is the code for updating.
SQL:
update vms_vertrag_datei d
inner join vms_vertrag_verlauf v ON d.vertrag = v.vertrag
SET d.nutzer = v.nutzer, d.uploaddatum= v.timestamp
WHERE d.filename in (SELECT DISTINCT SUBSTRING(v.aktion,LOCATE('"',v.aktion)+1,(((LENGTH(v.aktion))-LOCATE('"', REVERSE(v.aktion))-1)-LOCATE('"',v.aktion)))FROM vms_vertrag_verlauf v)
AND v.aktion like 'Datei%hinzugefĆ¼gt';
Does anyone know how I can now also consider text with umlauts? Am just after longer online research something at despair.

Replace or extend a URL in database table

I changed the position of some files in my file server and I need the update the URL of the file in my database.
For example from:
/sba/sbazen/test/SBA33G_TC/3300009//00150671.pdf
I want to get:
/sba/sbazen/test/SBA33G_TC/new_folder_name/3300009//00150671.pdf
Important to know: The number on SBA33G is variable. We can have SBA100G or SBA02G.
if the old folder path is always the same length you can use stuff
select len('/sba/sbazen/test/SBA33G_TC/'), stuff(t.oldval, 27, 1, '/new_folder_name/')
from (
select '/sba/sbazen/test/SBA33G_TC/3300009//00150671.pdf' oldval
) t
Why not simply use Update?
UPDATE tableName
SET
colName = '/sba/sbazen/test/SBA33G_TC/new_folder_name/3300009//00150671.pdf'
WHERE colName = '/sba/sbazen/test/SBA33G_TC/3300009//00150671.pdf'

Invalid number string (7498) JDBC Request

I'm converting this progress statement into SQL.
for each usr_mstr where usr_userid matches "PRF52" exclusive-lock:
assign usr_force_change = no.
end.
This is what I currently have.
UPDATE PUB.usr_mstr SET usr_force_change = 'false' WHERE usr_userid = 'PRF52'
The error that I am receiving is '[DataDirect][OpenEdge JDBC Driver][OpenEdge] Invalid number string (7498)'.
A select statement for this field is working and returns the following.
SELECT usr_force_change FROM PUB.usr_mstr WHERE usr_userid = 'PRF52'
usr_force_change
false
The column data type was of type 'LOGICAL'. This translates to type 'BIT' in SQL. I updated the statement to the following at it worked.
UPDATE PUB.usr_mstr SET usr_force_change = '0' WHERE usr_userid = 'PRF51'
You need to choose Query type as Update Statement when submit update
Update Statement - use this for Inserts and Deletes as well

UPDATE QUERY - Sum up a value from form with value from table

I've just started using microsoft access so I don't really know how to solve this. I would like to use an update query to add a value from a form to a value on a table.
I originally used the SUM expression which gave me an error saying it was an aggregate function.
I also tried to add the two values together (e.g [field1] + [field2]) which as a result gave me a value with both numbers together instead of adding them together.
The following is the SQL I'm using:
UPDATE Votes
SET Votes.NumVotes = [Votes]![NumVotes]+[Forms]![frmVote]![txtnumvotes]
WHERE (((Votes.ActID) = [Forms]![frmVote]![combacts])
AND ((Votes.RoundNum) = [Forms]![frmVote]![combrndnum]))
I want to add a value [txtnumvotes] a form to a field [NumVotes] from the table [Votes].
Could someone please help me?
You can specify the expected data type with parameters:
PARAMETERS
[Forms]![frmVote]![txtnumvotes] Short,
[Forms]![frmVote]![combacts] Long,
[Forms]![frmVote]![combrndnum] Long;
UPDATE
Votes
SET
Votes.NumVotes = [Votes]![NumVotes]+[Forms]![frmVote]![txtnumvotes]
WHERE
(((Votes.ActID) = [Forms]![frmVote]![combacts])
AND
((Votes.RoundNum) = [Forms]![frmVote]![combrndnum]))
Without the specification, Access has to guess, and that sometimes fails.

Update colum with value from another column + text

I imported an e-store's database values into my own, and it mostly worked out fine. However, there were no image file names. So, I need to update the entire database- over 6,000 records, so that under 'image' we get a path + model name + jpg, so each product can be associated with an image. Im having trouble mixing the dynamic column value with the static path. Here is what I need to accomplish:
UPDATE `store`.`pr_product`
SET `image` = 'data/products/`**model_no**`.jpg'
WHERE `pr_product`.`product_id` = `pr_product`.`product_id` ;
But cannot get it to recognize the dynamic nature of 'model_no'
Thanks in advance
Max,
Please what you means about dynamic nature of 'model_no'?
Is this column's datatype int or long or varchar
Please need more explaination with example
you can test the following if e.g model_no is column in pr_product table
UPDATE store.pr_product
SET image = 'data/products/'+pr_product.model_no+'.jpg'
WHERE pr_product.product_id = pr_product.product_id ;
Best Regards,
Mohammed Thabet Zaky