how to save value in oracle using nq'[]' operator - sql

I need to save string with single quote in column with nvarchar2 data type. I find here
https://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements003.htm
that I can save it using quote operator. I try to do it in my SQL query
insert into table (question_pattern_id, text, event_selection_id, context_theme_id, placeholder_body)
values ('65', nq'[👽]', '', '', nq'[👽]')
but it inserts ?? instead of 👽
Before data was added by query
insert into table (question_pattern_id, text, event_selection_id, context_theme_id, placeholder_body)
values ('65', n'👽', '', '', n'👽')
and everything was saved correctly

Related

What is the right way to handle type string null values in SQL's Bulk Insert?

For example, I have a column with type int.
The raw data source has integer values, but the null values, instead of being empty (''), is 'NIL'
How would I handle those values when trying to Bulk Insert into MSSQL?
My code is
create table test (nid INT);
bulk insert test from #FILEPATH with (format="CSV", firstrow=2);
the first 5 rows of my .csv file looks like
1
2
3
NIL
7
You can replace the nil with " (empty string) directly in your data source file or insert the data into a staging table and transform it:
BULK INSERT staging_sample_data
FROM '\\data\sample_data.dat';
INSERT INTO [sample_data]
SELECT NULLIF(ColA, 'nil'), NULLIF(ColB, 'nil'),...
Of course if your field is for example a numeric, the staging table should have a string field. Then, you can do as Larnu offers: 'TRY_CONVERT(INT, ColA)'.
*Note: if there are default constraints you may need to check how to keep nulls

All Values in json response including numeric values need to be in double quotes

Iam trying to retrieve the data from the table in json format with below query in SQL Server ,
Select * from TableA for json path, root( 'table 1')
Iam getting json response as ,
{
"TableA":[
{
"Id":34562,
"A Id":2,
"Name":"ZDR"
}
]
}
Where Field "ID" & "A Id" are declared as "int"in the database
Now i need those values should also be displayed within double quotes , is there any way that we can handle in SQL server 2016 itself, to make clear all the field values irrespective of datatype should be enclosed in double quotes please do need full !!!
Like above single query iam having set of 12 quires in a stored procedure eventually when i run the stored procedure i will get a json response where all the values should be enclosed within double quotes .
There is no "automagic" way to do this. What you can do is in your SELECT statements CAST your numeric columns to literal (varchar or nvarchar):
Select CAST(Id as nvarchar(10)) AS Id,
CAST([A Id], nvarchar(10)) AS "A ID",
[Name]
from TableA for json path, root( 'table 1');
Hope this helps!

Bulk Insert - How to tell SQLServer to insert empty-string and not null

This seems like a trivial question. And it is. But I have googled for over a day now, and still no answer:
I wish to do a bulk insert where for a column whose datatype is varchar(100), I wish to insert an empty string. Not Null but empty. For example for the table:
create table temp(columnName varchar(100))
I wish to insert an empty string as the value:
BULK INSERT sandbox..temp FROM
'file.txt' WITH ( FIELDTERMINATOR = '|#', ROWTERMINATOR = '|:' );
And the file contents would be row1|:row2|:|:|:. So it contains 4 rows where last two rows are intended to be empty string. But they get inserted as NULL.
This question is not the same as the duplicate marked question: In a column, I wish to have the capacity to insert both: NULL and also empty-string. The answer's provided does only one of them but not both.
Well instead of inserting empty string explicitly like this why not let your table column have a default value of empty string and in your bulk insert don't pass any values for those columns. Something like
create table temp(columnName varchar(100) default '')

How to parse colon within string in Oracle

I wish to insert a record to an Oracle database where one field contains the path to a file, i.e 'C:\\ProjectCodes\\ProjectZ\\ZCode.txt'. But when I attempt an INSERT statement I am asked about binding the value \\ProjectCodes\\ProjectZ\\ZCode.txt.
This is because of the colon I think. Is it possible to parse the colon to insert this value? The target field in the table is declared as a varchar2.
Here is the INSERT statement. I am using SQL Developer. Had to hide the IP address and password etc or I would be in trouble
INSERT INTO SFTP_Creds (SFTP_HOST, SFTP_PORT, SFTP_USERNAME, SFTP_PASSWORD,
SFTP_FINGERPRINT, SFTP_HOSTKEY, SFTP_FILE)
VALUES (‘xxxx.xxxx.xxxx.xxxx’, 22, ‘CORP\\username’, ‘password’, NULL,
NULL, 'C:\\ProjectCodes\\ProjectZ\\ZCode.txt');

Passing default values to a column in Bulk insert

I am trying to get data from a csv file with the following data.
Station code;Priority vehicle;DateBegin;DateEnd
01;y;20100214;20100214
02;n;20100214;20100214
03;;20100214;20100214
Now I want a value 'n' in the table when no data is provided for the column 'Priority vehicle' in csv file.
I am writing the query as
BULK INSERT dbo.#tmp_station_details
FROM 'C:\station.csv'
WITH (
FIELDTERMINATOR ='';'',
FIRSTROW = 2,
ROWTERMINATOR = ''\n''
)
Check the full explanation here:
http://msdn.microsoft.com/en-us/library/ms187887.aspx
"By default, when data is imported into a table, the bcp command and BULK INSERT statement observe any defaults that are defined for the columns in the table. For example, if there is a null field in a data file, the default value for the column is loaded instead. "
My suggestion is to specify a default value for the Priority vehicle column and the Null value from the csv file will be overwritten to your SQL table with the default value specified in the table design.