how can i get Hashbytes for nvarchar (max)? - hashbytes

How to find hashbytes for nvarchar(max), if i am using normal
Select hashbytes('md5','std.jobText')
I am getting error:
String or binary data would be truncated.
Could any one suggest any solution?

Related

PostgreSQL - jsonb - How to get the datatype for value in query with jsonpath

In PostgreSQL using jsonb column, is there a way to select / convert an attribute with actual datatype the datatype instead of getting it as a string object when using jsonpath? I would like to try to avoid cast as well as -> and ->> type of construct since I have to select many attributes with very deep paths, I am trying to do it using jsonpath and * or ** in the path
Is it possible to do it this way or must I use the -> and ->> for each node in the path ? This will make the query look complicated as I have to select about 35+ attributes in the select with quite deep paths.
Also, how do we remove quotes from the selected value?
This is what I was trying, but doesn't work to remove quotes from Text value and gives an error on numeric
Select
PolicyNumber AS "POLICYNUMBER",
jsonb_path_query(payload, '$.**.ProdModelID')::text AS "PRODMODELID",
jsonb_path_query(payload, '$.**.CashOnHand')::float AS "CASHONHAND"
from policy_json_table
the PRODMODELID still shows the quotes around the value and when I add ::float to second column, it gives an error
SQL Error [22023]: ERROR: cannot cast jsonb string to type double precision
Thank you
When you try to directly cast the jsonb value to another datatype, postgres will attempt to first convert it to a json text and then parse that. See
How to convert Postgres json(b) to integer?
How to convert Postgres json(b) to float?
How to convert Postgres json(b) to text?
How to convert Postgres json(b) to boolean?
When you have strings in your JSON values, to avoid the quotes you'll need to extract them by using one of the json functions/operators returning text. In your case:
SELECT
PolicyNumber AS "POLICYNUMBER",
jsonb_path_query(payload, '$.**.ProdModelID') #>> '{}' AS "PRODMODELID",
(jsonb_path_query(payload, '$.**.CashOnHand') #>> '{}')::float AS "CASHONHAND"
FROM policy_json_table
jsonb_path_query function returns data with quotes (""), so you cannot cast this to integer or float. For casting value to integer, you need value without quotes.
You can use this SQL for getting without quotes:
Select
PolicyNumber AS "POLICYNUMBER",
(payload->>'ProdModelID')::text AS "PRODMODELID",
(payload->>'CashOnHand')::float AS "CASHONHAND"
from policy_json_table
If you need to use exactly jsonb_path_query then you can trim these quotes:
Select
PolicyNumber AS "POLICYNUMBER",
jsonb_path_query(payload, '$.**.ProdModelID')::text AS "PRODMODELID",
trim(jsonb_path_query(payload, '$.**.CashOnHand')::text, '"')::float AS "CASHONHAND"
from policy_json_table

Convert string to datetime in SQL Server 2012

I have a below requirement.
I want to insert records into a table using a stored procedure with below parameters
CREATE TABLE Mytable (MyPassword VARCHAR(10),PasswordDateTime DateTime)
My stored procedure is as follows to insert data into the above table.
CREATE PROCEDURE [dbo].[spPassword_Insert]
-- Parameters
#Password VARCHAR(200)
,#PasswordDateTime VARCHAR(20)
AS
SELECT #PasswordDateTime = CAST(#PasswordDateTime AS DATETIME)
INSERT INTO Mytable
SELECT #Password,#PasswordDateTime
I get the value of #PasswordDatetime from stored procedure as '2020-01-13 12:19:43.02'
I am getting the #PasswordDateTime value as string from the stored procedure and I want to convert the value data type as Date-time as per table definition without changing the value format.I want to insert the value as it is but the data type is to be changed.
While I am trying to convert a #PasswordDateTime value into date-time format, I am getting Conversion failed when converting date and/or time from character string error.
Please suggest how to convert this.
I got the answer.
DECLARE #PasswordDateTime VARCHAR(50)='2015-12-02 20:40:37.8130000'
SELECT #PasswordDateTime =cast(#PasswordDateTime as datetime2(2))
SELECT #PasswordDateTime
Thanks

Inserting data in SQL Server table not showing error on both condition

I have use procedure to insert data into table for fixed column size. But data trim and inserted successfully without any error.
I have lost some content from that variable.
This code snippet is not showing any error:
declare #temp varchar(5)
declare #tm table (a varchar(5))
set #temp ='abcdefghijkl'
insert into #tm
values(#temp)
select * from #tm
But this code snippet is showing this error:
String or binary data would be truncated
declare #temp1 varchar(5)
declare #tm1 table (a varchar(5))
insert into #tm1
values('abcdefghijkl')
select * from #tm1
The fact that the second code snippet is raising an error is a good thing.
It prevents you from corrupting data by mistake.
In the first code snippet, however, SQL Server will silently trim the string due to implicit conversion rules.
Whenever you attempt to populate a variable with a data that has a different data type, SQL Server will attempt to implicitly convert the data type to the data type of the variable.
This is well documented in the Converting Character Data section of the char and varchar (Transact-SQL) page:
When character expressions are converted to a character data type of a different size, values that are too long for the new data type are truncated.
This does not happen when inserting into a table, providing ANSI_WARNINGS is set to ON (which is the default state).
When ANSI_WARNINGS is set to ON, you get the
String or binary data would be truncated
error message.
When it's set to OFF, however, the implicit conversion will silently truncate the data:
set ansi_warnings off;
declare #temp1 varchar(5)
declare #tm1 table (a varchar(5))
insert into #tm1
values('abcdefghijkl')
select * from #tm1
Result:
a
abcde
Note: The ansi_warnings state does not have effect the implicit conversion when setting a variable value - it will always be truncated regardless of the ansi_warnings state.

Hash nvarchar as UTF32

I have a column of type nvarchar. I would like to get a SHA256 hash of the UTF32 representation of these characters.
I have found HASHBYTES which seems to do the meat of what I want to do, with SELECT HASHBYTES('SHA2_256', MyBinaryData).
It also indicates it can operate on nvarchar, but it doesn't indicate how it does the conversion from characters to bytes. I particularly need the hash of the UTF32 representation. How can I get that hash? Is there an in-database way to encode the nvarchar to UTF32 that I can feed to HASHBYTES? Is there another way?
To get the UTF32 representation of a nvarchar column (in SQL Server) you should use some CLR code.
Try something similar to the code in this answer: https://stackoverflow.com/a/14041069/1187211

Insert hex string value to sql server image field is appending extra 0

Have an image field and want to insert into this from a hex string:
insert into imageTable(imageField)
values(convert(image, 0x3C3F78...))
however when I run select the value is return with an extra 0 as 0x03C3F78...
This extra 0 is causing a problem in another application, I dont want it.
How to stop the extra 0 being added?
The schema is:
CREATE TABLE [dbo].[templates](
[templateId] [int] IDENTITY(1,1) NOT NULL,
[templateName] [nvarchar](50) NOT NULL,
[templateBody] [image] NOT NULL,
[templateType] [int] NULL)
and the query is:
insert into templates(templateName, templateBody, templateType)
values('I love stackoverflow', convert(image, 0x3C3F786D6C2076657273696F6E3D.......), 2)
the actual hex string is quite large to post here.
I have just had similar problem and I blame myself.
It is possible, that you copy just part of data you need. In my case, I added '0' to the end of the blob.
The cause of this could be copying the value from SQL Management Studio to clipboard.
insert into imageTable(imageField) values(0x3C3F78...A)
Select returned: 0x03C3F78...
insert into imageTable(imageField) values(0x3C3F78...A 0 )
Select returned: 0x3C3F78...
I hope this will help.
Best wishes.
This is correct for 0x0: each pair of digits makes one byte so 0x00 has the value stored
When I run SELECT convert(varbinary(max), 0x55) I get 0x55 out on SQL Server 2008. SELECT convert(varbinary(max), 85) gives me 0x00000055 which is correct is 85 is a 32 bit integer
What datatype are you casting to varbinary?
Edit: I still can't reproduce using image not varbinary
Some questions though:
is this an upgraded database? What is the compatibility level?
why use image: use varbinary(max) instead
what happens when you change everything to varbinary?