I just got a job working mainly with SQL Server 2016 (v13.0.5).
The task I'm working on is about taking a column of XML strings that are currently encoded in base64, and finding a way to decode this, change it, and encode it again through straight SQL or things like stored procedures - as we mainly make forms with this data in Report Builder and don't connect with web scripts or other programs, using something else than SQL for this (Python has been suggested) would be adding an extra cog to the system, which is not welcomed. So please don't give me a hard time for looking for this, I know it's not optimal.
I have looked at several other posts here. The encoding function offered as a solution in Microsoft SQL 2016 decode Base64 column gives me a column full of nulls. This other one Base64 encoding in SQL Server 2005 T-SQL is meant for converting a single string, and it seems I'm not experienced enough with SQL in general to try and embed this in a stored procedure or something to go through the column one value at a time. Here's my last attempt, which just does not change anything.
SET NOCOUNT ON;
DECLARE @xmlvalue varchar(200)
DECLARE pruebacursor CURSOR FOR
SELECT xmlvalue FROM base64pruebas
OPEN pruebacursor
FETCH NEXT FROM pruebacursor INTO @xmlvalue
WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE base64pruebas
SET XMLValue = (CAST(N'' AS XML).value('xs:base64Binary(sql:variable("@xmlvalue"))', 'VARCHAR(MAX)'))
WHERE xmlvalue = @xmlvalue
FETCH NEXT FROM pruebacursor INTO @xmlvalue
END
CLOSE pruebacursor;
DEALLOCATE pruebacursor;
At the end of this, the column in question is still the same as when I started.
Thank you for any suggestions on how to improve this.