Inserting data from a text file directly to an sql table - sql

for example if i had a text file in the following format
A
B
C
D
E
.
.
.
and an sql table calld table with column called letters
i want to insert each value in the text file to the column letters, is it possible to do this directly from SQL Server without using any other programming language?
also is it possible to specify a seperator for the values like "/" or "#"? in case the the text format was A/B/C/D or A#B#C#D?

Yes, exactly. Use BULK INSERT can resolve your issue.

Related

Enquote multiple text fields in CSV - Sublime Text 3

I have a CSV file containing info to be inserted in a database with SQL using insert similar to the below example:
INSERT INTO `Person`(`name`, `occupation`, `residence`, `comments`) VALUES
(GIBEL ΕΛΛΗ,NULL,NULL,NULL)
I want to separate the text values from the null values. Specifically, I want to enquote text values and leave null as it is. Is there a way to do this using Sublime Text 3? Please bear in mind that I am not familiar with regexp. Each field in my DB is either VARCHAR or TEXT.
Example you supplied
INSERT INTO `Person`(`name`, `occupation`, `residence`, `comments`) VALUES (GIBEL ΕΛΛΗ,NULL,NULL,NULL)
Activate RegEx in ST3 Find and Replace.
Find Value
(.*VALUES \()(.*)(,NULL,NULL,NULL\))
Replace Value
$1"$2"$3
Apply Replace All
Result
INSERT INTO `Person`(`name`, `occupation`, `residence`, `comments`) VALUES ("GIBEL ΕΛΛΗ",NULL,NULL,NULL)

Replace all URLs in SQL Server table column which contains html data

I have a SQL Server table column which contains html contents with links in format href="/page/random_id1/random_id2".
Here random_id1 is a guid and random_id2 is a slug. Now i would like to run a sql query which can replace all href values by converting them to href="/page/random_id2".
Notice here, i'm removing random_id1 part from every herf.
Here's a sample query:
select top 1 id, htmlcode from tenantpage;
03781776-5f64-4c59-a6f7-5873e0b9f128
<div>test</div>Link<div>something else <span>test Link</span></div>Link
I don't know if this is possible, if possible please help here.

When Oracle database link to connect to Excel's using ODBC I have "identifier's name length exceeds maximum"

I tried to get data in Oracle from Excel using ODBC and database link. I use sql like this:
Select * From A222$#TEST2
When TEST2 - name of Excel database link, and A222 - name of my worksheet of Excel file.
However, because in first row of Excel I have big string values like
A1234567890_B1234567890_C1234567890_D1234567890_E1234567890 that converted to very big column name, and when I try to use sql like this:
CREATE VIEW VIEW2 AS Select * From A222$#TEST2;
// or
Select A1234567890_B1234567890_C1234567890_D1234567890_E1234567890 as c1 from A222$#TEST2;
// or
Select "A1234567890_B1234567890_C1234567890_D1234567890_E1234567890" as c1 from A222$#TEST2;
I get following error
ORA-01948: identifier's name length (31) exceeds maximum (30)
ORA-00972: identifier is too long
And I can't change this Excel file or create temp copy of this file.
It's possible:
Using name of Excel columns like A1, AB1, Z1 instead of first row's
values in sql query from ODBC Excel?
Or something fix this problem without changing this Excel file or
creating temp copy of this Excel's file?
Thank you in advance!
I don't know if I fully understand the problem. As Oracle points out, the this count#of#people#how#live#in#San#francisco is not a valid Oracle name. But do you really want a column name, particularly if it is invalid?
I am guessing you want a string value not a column name so use single quotes:
Select 'count#of#people#how#live#in#San#francisco' as c1
from A222$#TEST2;
Also, only one from per select.

Is there a way to define replacement of one string to other in external table creation in greenplum.?

I need to create external table for a hdfs location. The data is having null instead of empty space for few fields. If the field length is less than 4 for such fields, it is throwing error when selecting data. Is there a way to define replacement of all such nulls with empty space while creating table it self.?
I am trying it in greenplum, just tagged hive to see what can be done for such cases in hive.
You could use the serialization property for mapping NULL string to empty string.
CREATE TABLE IF NOT EXISTS abc ( ) ROW FORMAT DELIMITED FIELDS TERMINATED BY '|' STORED AS TEXTFILE TBLPROPERTIES ("serialization.null.format"="")
In this case when you query it from hive you would get empty value for that field and hdfs would have "\N".
Or
If you want to represented empty string instead of '\N', you can using COALESCE function:
INSERT OVERWRITE tabname SELECT NULL, COALESCE(NULL,"") FROM data_table;
the answer to the problem is using NULL as 'null' statement in create table syntax for greenplum. As i have mentioned, i wanted to get few inputs from people who faced such issues in hive. so i have tagged hive as well. But, greenplum external table syntax supports NULL AS phrase in which we can specify the form of NULL that you want to keep.

Values inserted in hive table with double quotes for string from csv file

I am exporting a csv file into hive table.
about the csv file : column values are enclosed within double-quotes , seperated by comma .
Sample record from csv
"4","good"
"3","not bad"
"1","very worst"
I created a hive table with the following statement,
create external table currys(review_rating string,review_comment string ) row format fields delimited by ',';
Table created .
now I loaded the data using the command load data local inpath and it was successful.
when I query the table,
select * from currys;
The result is :
"4" "good"
"3" "not bad"
"1" "very worst"
instead of
4 good
3 not bad
1 very worst
records are inserted with double-quotes which shouldnt be.
Please let me know how to get rid of this double quote .. any help or guidance is highly appreciated...
Thanks beforehand!
Are you using any serde? If so, then you can write a regex command in the SERDE PROPERTIES to remove the quotes.
Or you can use the csv-serde from here and define the quote character.