SQL hashbytes update statement not as expected - tsql

I'm trying to update a column on SQL server r2 using the hashbytes command. Here's the simplified version of the command:
COMMAND: "UPDATE [tbl] SET [checksum] = HASHBYTES('MD5',[field1])"
The problem is that, it writes strange characters like this to all the fields:
"˜Iý¸¶C"KéS©c"
However, if I do a select (using the same fields):
select HASHBYTES('MD5',[field1]) from [tbl];
It returns a correct string:
0x9849FDB80C17B64322DA094BE963A963
Anyone know why it would do this. I've tried on a test database and the update command works as expected. But it doesn't work on our production server.

The reason you are getting this is because HASHBYTES returns a binary data type, and this is not text.
Using the build in function fn_varbintohexstr you can convert the binary data into text, as follows:
UPDATE [tbl] SET [checksum] = master.dbo.fn_varbintohexstr(HASHBYTES('MD5',[field1]))

Related

Convert XML PATH sample code from SQL Server to DB2

I'm converting the SQL server to db2..
I need a solution for stuff and for xml path
Ex
Select stuff(select something
from table name
Where condition
For xml path(''),1,1,'')
Pls convert this into db2.
Your code is an old school XML "trick" to convert multiple values to a single string. (Often comma separated but in this case space separated.) Since those days DB2 (and the sql standards) have added a new function called listagg which is designed to solve this exact problem:
Select listagg(something,' ')
from table name
Where condition
db2 docs -
https://www.ibm.com/support/knowledgecenter/en/SSEPEK_12.0.0/sqlref/src/tpc/db2z_bif_listagg.html
https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/db2/rbafzcollistagg.htm

MySQLi Update data in a table

Please help me out with this. Basically to update an existing table (chem_users) with data using 2 keys UserId & Password (or only 1 primary key is allowed?).
Using MySQLi, what is wrong with this syntax.
$sql = "UPDATE chem_users SET (Prj1, Prj2) VALUES ('{$_POST['kinetics']}',
'{$_POST['thermo']}') WHERE (UserId=JohnKing Password=1234rewq)";
I got this error:
Error saving user data You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(Prj1, Prj2) VALUES ('kinetics' at line 1
Try using this syntax:
$sql="UPDATE `chem_users` SET `Prj1`=".$_POST['kinetics'].",`Prj2`=".$_POST['thermo']." WHERE `UserId`='JohnKing' AND `Password`='1234rewq'";
By the way, you shouldn't just concatenate a variable inside a query like you are doing,you should use Prepared Statements.You can learn a little bit about it in this link: http://www.w3schools.com/php/php_mysql_prepared_statements.asp

replacing characters in a CLOB column (db2)

I have a CLOB(2000000) field in a db2 (v10) database, and I would like to run a simple UPDATE query on it to replace each occurances of "foo" to "baaz".
Since the contents of the field is more then 32k, I get the following error:
"{some char data from field}" is too long.. SQLCODE=-433, SQLSTATE=22001
How can I replace the values?
UPDATE:
The query was the following (changed UPDATE into SELECT for easier testing):
SELECT REPLACE(my_clob_column, 'foo', 'baaz') FROM my_table WHERE id = 10726
UPDATE 2
As mustaccio pointed out, REPLACE does not work on CLOB fields (or at least not without doing a cast to VARCHAR on the data entered - which in my case is not possible since the size of the data is more than 32k) - the question is about finding an alternative way to acchive the REPLACE functionallity for CLOB fields.
Thanks,
krisy
Finally, since I have found no way to this by an SQL query, I ended up exporting the table, editing its lob content in Notepad++, and importing the table back again.
Not sure if this applies to your case: There are 2 different REPLACE functions offered by DB2, SYSIBM.REPLACE and SYSFUN.REPLACE. The version of REPLACE in SYSFUN accepts CLOBs and supports values up to 1 MByte. In case your values are longer than you would need to write your own (SQL-based?) function.
BTW: You can check function resolution by executing "values(current path)"

SQL LIKE statement using using unicode characters does not show correct result

I'm using SQL Server 2008 R2. I'm just wondering why this statement doesn't work correctly.
For example: The statement
WHERE CONTRACTORNAME LIKE '%á%'
would gives me the correct result for every records containing "á". But the statement
WHERE CONTRACTORNAME LIKE '%ạ%'
would not gives any records even though in CONTRACTORNAME column have a tons of records containing this character. Any help?
Try using a Unicode search string:
WHERE CONTRACTORNAME LIKE N'%ạ%'

TSQL - export query to xls /xslx / csv

I have a complicated dynamic query in TSQL that I want to export to Excel.
[The result table contains fields with text longer than 255 chars, if it matters]
I know I can export result using the Management Studio menus but I want to do it automatically by code. Do you know how?
Thanks in advance.
You could have a look at sp_send_dbmail. This allows you to send an email from your query after it's run, containing an attached CSV of the resultset. Obviously the viability of this method would be dependent on how big your resultset is.
Example from the linked document:
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'AdventureWorks2008R2 Administrator',
#recipients = 'danw#Adventure-Works.com',
#query = 'SELECT COUNT(*) FROM AdventureWorks2008R2.Production.WorkOrder
WHERE DueDate > ''2006-04-30''
AND DATEDIFF(dd, ''2006-04-30'', DueDate) < 2' ,
#subject = 'Work Order Count',
#attach_query_result_as_file = 1 ;
One way is to use bcp which you can call from the command line - check out the examples in that reference, and in particular see the info on the -t argument which you can use to set the field terminator (for CSV). There's this linked reference on Specifying Field and Row Terminators.
Or, directly using TSQL you could use OPENROWSET as explained here by Pinal Dave.
Update:
Re;: 2008 64Bit & OPENROWSET - I wasn't aware of that, quick dig throws up this on MSDN forums with a link given. Any help?
Aside from that, other options include writing an SSIS package or using SQL CLR to write an export procedure in .NET to call directly from SQL. Or, you could call bcp from TSQL via xp_cmdshell - you have to enable it though which will open up the possible "attack surface" of SQL Server. I suggest checking out this discussion.
Some approaches here: SQL Server Excel Workbench
I needed to accept a dynamic query and save the results to disk so I can download it through the web application.
insert into data source didn't work out for me because of continued effort in getting it to work.
Eventually I went with sending the query to powershell from SSMS
Read my post here
How do I create a document on the server by running an existing storedprocedure or the sql statement of that procedure on a R2008 sql server
Single quotes however was a problem and at first i didn't trim my query and write it on one line so it had line breaks in sql studio which actually matters.