I need bigint version of sha1 encrypted text. This is the sha1 converted ouputed,
select upper(sha1('THIsneedstobeChanges'))
output
upper(sha1(CAST(THIsneedstobeChanges AS BINARY)))
0BF3770DF2705F6318491A955D32666395810537
So now I need to convert it to bigint
select cast(upper(sha1('THIsneedstobeChanges')) as bigint)
Output is null.
Basically I am trying to replicate this code from SQL to spark -SQL,
select cast(HASHBYTES('SHA1', 'testdata') as bigint)
Could you please help with this issue
Related
I'm trying to create the following table in PostgreSQL 13:
CREATE TABLE cached (
text VARCHAR NOT NULL,
text_hash BYTEA GENERATED ALWAYS AS (sha256(convert_to(text, 'LATIN1'))) STORED PRIMARY KEY
);
However, I'm getting the following error:
generation expression is not immutable
I'm guessing that this is because convert_to is not immutable. How do I work around this? Is there a way of converting the text column to bytea in an immutable way?
Things I'd rather avoid:
Casting to text::bytea. It won't work correctly as explained here.
Using triggers.
CREATE OR REPLACE FUNCTION cripto(TEXT )
RETURNS TEXT AS
$function$
DECLARE tText TEXT ;
BEGIN
SELECT sha256(convert_to($1, 'LATIN1')) INTO tText;
RETURN tText ;
END ;
$function$
IMMUTABLE
LANGUAGE plpgsql ;
CREATE TABLE cripto(
text TEXT ,
text_hash TEXT GENERATED ALWAYS AS ( cripto(text) ) STORED PRIMARY KEY
);
I'm working on a poc to migrate an on-prem SQL Server database to Amazon Aurora for PostgreSQL. Amazon's Schema Conversion Tool struggled to translate the SQL Server code for the creation of a table on this column:
[DOB] AS (CONVERT([varchar],datefromparts([DOB_year],[DOB_month],[DOB_day]),(120))) PERSISTED,
as the CONVERT function is unsupported in Postgres.
The best translation I can come up with is:
dob varchar(30) GENERATED ALWAYS AS (to_char((make_date(dob_year, dob_month, dob_day))::timestamp, 'YYYY-MM-DD HH24:MI:SS')) STORED,
but neither the SCT nor pgAdmin4 are recognising to_char() and make_date() as functions. 'dob_day', 'dob_month' and 'dob_year' are all column names with datatype of integer. I'm new to all this but another column definition is using other functions, e.g. replace() and right(), successfully, so I'm confused why this isn't working.
When I tried to run the code in pgAdmin I got this error:
ERROR: generation expression is not immutable
SQL state: 42P17
Thanks
to_char() is is not marked as immutable even though in your case it would be. But there are format masks that are not immutable if e.g. time zones or different locales are involved.
If you really want to (or are forced to) convert day,month, year in a formatted string (rather than a proper date which would be the correct thing to do), then you can only achieve this with a custom function:
create function create_string_date(p_year int, p_month int, p_day int)
returns text
as
$$
select to_char(make_date(p_year, p_month, p_day), 'yyyy-mm-dd hh24:mi:ss');
$$
language sql
immutable;
Marking the function as immutable isn't cheating, because we know that with the given input and format string this is indeed immutable.
dob text generated always as (create_string_date(dob_year, dob_month, dob_day)) stored
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
I try to put a big number of messages (350M) to customer topic (source topic) with value format like this
10957402000||10965746672||2|2756561822|452048703649890|8984048701003649890
and then I make some streams and table on that topic, but the delimited format supported by ksql is just comma separator. I have some questions:
Is there any way to config ksql can understand my format? Or I have to convert to format default by ksql (comma separator)
From the original value from source topic like above, how this command can mapping value to table column? Or I have to convert format to json?
CREATE STREAM (sub_id BIGINT, contract_id BIGINT, cust_id BIGINT, account_id BIGINT,telecom_service_id BIGINT, isdn BIGINT, imsi BIGINT) \
WITH (KAFKA_TOPIC='customer', VALUE_FORMAT='DELIMITED');
Thanks you.
Edit 26 February 2021 ksqlDB now supports configurable delimiters - use the VALUE_DELIMITER (or KEY_DELIMITER) configuration option. For example:
CREATE STREAM (COL1 INT, COL2 VARCHAR)
WITH (KAFKA_TOPIC='test', VALUE_FORMAT='DELIMITED', VALUE_DELIMITER='TAB')
Original answer:
Currently KSQL only supports comma-separated for DELIMITED value format. So you'll need to use commas, or JSON, or Avro, for your source data.
I am using PostgreSQL and I need fields as strings not text.
Here is the statement
select AutoNbr,
concat_ws(', ', Site_Street, Site_City, Site_Country, Site_PostCode) as cFullAddress , Order_Date
from Porder
What I need is the cFullAddress to be a varchar not a text field.
According to documents:
If character varying is used without length specifier, the type accepts strings of any size. The latter is a PostgreSQL extension.
In addition, PostgreSQL provides the text type, which stores strings of any length. Although the type text is not in the SQL standard, several other SQL database management systems have it as well.
But you can cast it into varchar;
select AutoNbr,
concat_ws(', ', Site_Street, Site_City, Site_Country, Site_PostCode)::varchar as cFullAddress , Order_Date,
CAST(concat_ws(', ', Site_Street, Site_City, Site_Country, Site_PostCode) as VARCHAR)
from Porder
Related link for using cast: type cast
Difference between text and varchar (character varying)