How to insert an existing GUID into Oracle RAW(16) field in a script - sql

I have an sql server script which inserts known fixed guid values into a table. It looks like:
INSERT INTO MyTable (ID)
VALUES ('BBD098BF-58F0-4A84-90C2-F806D6D06061')
Note that guid is in human-readable form.
Since ID is uniqueidentifier sql server understands how to convert a string to guid data type.
I need to make the same script for Oracle, ID is of RAW(16) type. Taking the script directly doesn't work because Oracle interprets a string just like a binary, it should be some "other" string, a string representation of a correct binary chunk.
Does anyone knows a way to convert human-readable sql server string to a string required by Oracle?
So far I can only think about saving a guid to Oracle in .net code, for example, and than making a select in oracle script to get a string. But this is crazy.
Thanks!

According to this link
Sqlserver reverses the 3 first sections so you need to do:
hextoraw(substr(guid,7,2)||
substr(guid,5,2)||
substr(guid,3,2)||
substr(guid,1,2)||
substr(guid,12,2)||
substr(guid,10,2)||
substr(guid,17,2)||
substr(guid,15,2)||
substr(guid,20,4)||
substr(guid,25,12)
)
(guid is like 'BBD098BF-58F0-4A84-90C2-F806D6D06061')

Related

SQL data types for AnyLogic

I am saving the output of my AnyLogic model into an SQL server database. For non-AnyLogic aficionados, AnyLogic is based on Java. However, I am not sure what data types I need to specify for my columns in the database.
So far I am using these:
double in AnyLogic : float in SQL
string in AnyLogic : varchar in SQL
int in AnyLogic : int in SQL
I also have parameters that are of type Option list, which is, if I understand correctly, a form of Java enum. I tried to save those parameters as varchar, but this (obviously) does not work. In addition, my model contains various boolean parameters. For my boolean parameters, I add columns of type bit in SQL by running:
ALTER TABLE myTable
ADD my_bool BIT NOT NULL DEFAULT 0;
However, running the model returns this error
SQLServerException: Invalid column name 'false'. Caused by: Invalid column name 'false'
So concretely, how can I export parameters of type Option list and boolean?
This addresses the original question which was tagged MySQL.
I don't know all the issues around "option list". Seems like a string (with a length such as varchar(255)) would work. You can also look into the built-in enum type, although I would not normally recommend using enums.
I would recommend using boolean instead of bit as the equivalent for boolean. Seems more mnemonic.
That said, MySQL understands false as a constant. You can check this by running:
select false
This also works:
select "false"
However, this returns the error that you specify:
select `false`
I suspect that the code being generated is using this construct. You will need to look at the code -- and you might need to figure out some other way of handling this. In MySQL you can use 0 for false and that might fix your problem.
The AnyLogic database is a standard HSQLDB database (not something proprietary) but they've added AnyLogic client functionality to define 'column types' as though they are Java types (with special types for option lists and compiled-on-the-fly-and-run Java code).
If you look at the db.script file (HSQLDB just stores the persistent DB data as an SQL script which creates the tables and INSERTs the values) you can see the underlying HSQLDB types which map closely to SQL Server types.
boolean --> BOOLEAN
double --> DOUBLE
int --> INT
String --> VARCHAR(16777216)
Date --> TIMESTAMP
Code --> VARCHAR(16777216)
Option List --> VARCHAR(255)
NB: The 'Java column types' are supposed to make it easier for a non-technical user to understand what they will get from a Java perspective when querying that column but, for example, they are confusing in that queries will return Java nulls for missing values, so a boolean column actually effectively returns a Boolean.
That should help.
I managed to address part of my problem. I am now able to store String variables from Java into my SQL database. The issue was due to incorrect use of quotations.
Java uses double quotations for String variables (e.g.: ""). SQL expects single quotations (e.g.: '') for string-like columns such as varchar() and char()
I had to amend my SQL query to this:
String insertTableSQL = "INSERT INTO test (my_string) VALUES(" +" '"+my_variable_string+"' )";
Note that my_variable_string is a derivative of a Java enum, which I obtained by executing String my_variable_string= my_enum.name();

How to convert ADF Pipeline Run Id (string) to GUID?

I have a table in a SQL database with a UNIQUEIDENTIFIER data type
I would like to populate this table with the Pipeline Run Id parameter in Azure Data Factory
When I pass this parameter, it is passed as a string value, my table in the SQL database expects a value GUID() value. Is there a way to convert the parameter to GUID() type. Or should I consider changing the datatype in the target table?
Thanks in advance :)
You'll probably be better served by changing the SQL data type to a varchar if that's possible. Pipelines don't have a variable type for guid. They also do not have a conversion function for string to guid. The guid() function generates a new guid value but returns a string. The Data Flow expression language doesn't even contain a reference to guid. All in all, my conclusion is if you can just treat them as strings you should.

INSERT Statement in SQL Server Strips Characters, but using nchar(xxx) works - why?

I have to store some strange characters in my SQL Server DB which are used by an Epson Receipt Printer code page.
Using an INSERT statement, all are stored correctly except one - [SCI] (nchar(154)). I realise that this is a control character that isn't representable in a string, but the character is replaced by a '?' in the stored DB string, suggesting that it is being parsed (unsuccessfully) somewhere.
The collation of the database is LATIN1_GENERAL_CI_AS so it should be able to cope with it.
So, for example, if I run this INSERT:
INSERT INTO Table(col1) VALUES ('abc[SCI]123')
Where [SCI] is the character, a resulting SELECT query will return 'abc?123'.
However, if I use NCHAR(154), by directly inserting or by using a REPLACE command such as:
UPDATE Table SET col1 = REPLACE(col1, '?', NCHAR(154))
The character is stored correctly.
My question is, why? And how can I store it directly from an INSERT statement? The latter is preferable as I am writing from an existing application that produces the INSERT statement that I don't really want to have to change.
Thank you in advance for any information that may be useful.
When you write a literal string in SQL is is created as a VARCHAR unless you prefix is with N. This means if you include any Unicode characters, they will be removed. Instead write your INSERT statement like this:
INSERT INTO Table(col1) VALUES (N'abc[SCI]123')

SQL PWDENCRYPT & PWDCOMPARE 2008 vs 2012 Return Different Results [duplicate]

I have a web application done in ASP.NET MVC 4. It has users, that are stored in SQL Server database in tables webpages_UserProfile and webpages_Membership, etc.
I have another application, and what I need to do is to query the table webpages_Membership, where password of users are stored encrypted, and compare them to a plain text password.
So I tried doing something like
SELECT *
FROM webpages_Membership
WHERE PwdCompare('mypasswordsend', Password) = 1
But it doesn't works. I know the column is a nvarchar(128).
How can I compare it?
Let's look at the second argument to PwdCompare (emphasis mine):
password_hash
Is the encryption hash of a password. password_hash is *varbinary(128)*.
So, if your column is storing the password in plain text, or is storing a string representation of the binary hash, it's not going to work. You should either change the column to be correct or you will need to convert it first, e.g. check this script:
SELECT PWDENCRYPT(N'mypassword');
Yields:
0x0200D422C0365A196E308777C96CBEF3854818601DDB516CADA98DBDF6A5F23922DC0FADD29B806121EA1A26AED86F57FCCB4DDF98F0EFBF44CA6BA864E9E58A818785FDDEDF
If we try to compare that value as a string, we get 0:
SELECT PWDCOMPARE(N'mypassword', N'0x0200D422C0365A196E308777C96CBEF3854818601DDB516CADA98DBDF6A5F23922DC0FADD29B806121EA1A26AED86F57FCCB4DDF98F0EFBF44CA6BA864E9E58A818785FDDEDF');
If we try to compare it as a varbinary value, we get 1:
SELECT PWDCOMPARE(N'mypassword', 0x0200D422C0365A196E308777C96CBEF3854818601DDB516CADA98DBDF6A5F23922DC0FADD29B806121EA1A26AED86F57FCCB4DDF98F0EFBF44CA6BA864E9E58A818785FDDEDF);
If you can't fix the table, then you can perform this expensive explicit conversion in your query every time (note that the trailing ,1 is important):
SELECT PWDCOMPARE(N'mypassword',
CONVERT(VARBINARY(128), N'0x0200D422C0365A196E308777C96CBEF3854818601DDB516CADA98DBDF6A5F23922DC0FADD29B806121EA1A26AED86F57FCCB4DDF98F0EFBF44CA6BA864E9E58A818785FDDEDF'
, 1));

How can I split a string value in a SQL Server CE query?

I have a table containing a nvarchar column [AffectedNodes] that looks something like this when you take a peek at its contents (two variations shown):
"MID128; MID129; MID130"
"[1,3,2]; [3,1,2]"
We are working on a change which will move the AffectedNodes into its own table [AffectedNode] that has a nvarchar column [NodeId], which should store one of the nodes from the above string. I'm tasked with migrating the existing content to the new format.
As you can see the values are split using semi-column and a space.
To follow the database upgrade process they use in our project I have to write an SQL query in SQL Server CE. I'm wondering how I could do this in a neat way. Thanks!
SQL CE supports, CHARINDEX and SUBSTRING function, did you check that out or did you see any issue with them?