I am trying to query using iSQL client and export the query results to an Excel with column name. When I try below script error message saying:
Invalid Format "Excel".
Other than Excel format (ASCII, CSV, etc) all are working but no column name.
SQL query
select * from siebel.S_ORG_EXT;
OUTPUT TO 'C:\Siebel SQLs\Account.xls' FORMAT EXCEL
DELIMITED BY ';' QUOTE '' ;
You need to set WITH COLUMN NAMES. So your query could look like
select * from siebel.S_ORG_EXT;
OUTPUT TO 'C:\Siebel SQLs\Account.xls' FORMAT EXCEL
DELIMITED BY ';' QUOTE ''
WITH COLUMN NAMES;
Related
I have some code which pulls CSV's from S3 into a Redshift table. I'm getting issues whereby, if the CSV is stored in a certain column order, the copy command doesn't match the column order in the CSV header.
So if I have a CSV with the columns id|age|name and I have a Redshift table with the columns id|name|age, it will attempt to pull in the data in the CSV header order. So in this case, it will attempt to pull the name CSV column into the age column in Redshift, which causes a type error.
My query is:
copy schema.#tmp from <s3file>
iam_role <iamrole>
acceptinvchars
truncatecolumns
IGNOREBLANKLINES
ignoreheader 1
COMPUPDATE OFF
STATUPDATE OFF
delimiter ','
timeformat 'auto'
dateformat 'auto';
Do I need to define the column order in the copy command to match the two up?
COPY ignores column names in the file; the columns are matched from left to right.
But you can specify a column list in the COPY statement. Use that to tell PostgreSQL the order of the columns in the file.
I have a SQL script which outputs data to csv file. The script works fine however the leading zero is stripped from the phone number when I export to csv.
SQL:
SELECT RTRIM(Mobile_Telephone) AS Mobile_Number FROM TABLE
Datatype: char(20), not null
I am exporting data to csv using SSIS package
Can you please advise how I can preserve Leading zeros after exporting to csv.
Thanks
Aruna
Try using CONCAT syntax after SELECT
SELECT CONCAT('Phone Number Value') FROM table
See if it helps
I am using the below sql to export data to a csv file:
select * INTO outfile 'customer.csv'
FIELDs terminated by ',' enclosed by '"'
LINES terminated by '\n'
from CUSTOMER customer
where customer.date_created < '2015-10-22 10:00:00';
I get this result in csv:
Problem is data doesn't import from this generated csv because the date format is different than in DB.
DB date format is yyyy-mm-dd hh:mm:ss. Also null values are replaced with \n which also fail when importing.
How can I generate the csv columns with correct i.e. yyyy-mm-dd hh:mm:ss date format and null/empty values?
Errors:
Incorrect datetime value: '23/07/2015 11:55' for column 'DATE_CREATED' at row 1
Incorrect integer value: '\N' for column 'column_name' at row 1
Note:
I am using mysql workbench to import the file.
I don't want to change the format/data directly in csv file.
Thanks
UPDATE:
Thanks to AdrianBR I realised I was opening the file with excel first which was overriding the date format hence wrong date format was showing even with notepad++.
\n is still a problem.
When opened with notepad++ for the first time it looks like this:
"100","0","2015-12-02 10:16:36","2015-12-02 10:16:36","0",\N,
The issue is most likely not coming from mysql.
It is most likely coming from the way Excel displays and later saves the dates.
to troublehsoot:
Open the file in a text editor such as notepad or notepad++ and check what the date looks like, if it's in ISO or not. It will probably be fine.
Now, if you open it in excel, it will be displayed in local format.
If you save the file now, you are likely to overwrite the ISO date format, with excel's local date format, making it not a valid importable mysql date anymore.
Moral: don't use excel when working with data, only use it to display charts. Excel makes assumptions about your data and messes with it in the most unexpected ways. Remember than 1.19 VAT tax rate? Excel seems to think it's the same as Jan 19. That integer ID? Excel thinks it's better off to write it in scientific notation and round it to first 4 digits. That Iso date? Excel thinks you are better off guessing which is the month and which is the date. That decimal point? surely you wanted comma as decimal, and dot as thousands separator instead. FTFY!
maybe specify the columns explicitly, and include the format in the select list like so:
TO_CHAR( mydate, 'yyyy-mm-dd hh24:mi:ss' )
edit:
as in:
SELECT my_id, my_val1, TO_CHAR( mydate, 'yyyy-mm-dd hh24:mi:ss' )
FROM mytable
I am trying to query a Sybase using iSQL client and export the query results to a text file or CSV file with column name. However the column headings are not exported to the file. I tried below script it shows error message, below the working script without column heading and error script, appreciate any valuable advice.
working sql:
select * from siebel.S_ORG_EXT;
OUTPUT TO 'C:\\Siebel SQLs\\Account.CSV' FORMAT TEXT
DELIMITED BY ';' QUOTE ''
Not working sql :
select * from siebel.S_ORG_EXT;
OUTPUT TO 'C:\\Siebel SQLs\\Account.CSV' FORMAT TEXT
DELIMITED BY ';' QUOTE '' WITH COLUMN NAMES;
If you are using Sybase iAnywhere the WITH COLUMN NAMES option is not recognized by that Sybase product. Just thought I'd mention this for those like myself who have struggled with a similar issue.
HTH
You can try following query:
SELECT * FROM siebel.S_ORG_EXT; OUTPUT TO 'C:\\Siebel SQLs\\Account.CSV' FORMAT ASCII DELIMITED BY ';' QUOTE '' WITH COLUMN NAMES;
Alternatively you could use a different SQL client. For example Squirrel SQL which supports JDBC connections. In other SQL clients you will need to import the jconn2.jar which is part of your local web client installation.
This question already has answers here:
Exporting results of a Mysql query to excel?
(9 answers)
Closed 8 years ago.
How do you save output of a MySQL query to a MS Excel sheet?
Even if it's only possible to store the data in a .txt file, it will be okay.
From Save MySQL query results into a text or CSV file:
MySQL provides an easy mechanism for writing the results of a select
statement into a text file on the server. Using extended options of
the INTO OUTFILE nomenclature, it is possible to create a comma
separated value (CSV) which can be imported into a spreadsheet
application such as OpenOffice or Excel or any other application which
accepts data in CSV format.
Given a query such as
SELECT order_id,product_name,qty FROM orders
which returns three columns of data, the results can be placed into
the file /tmp/orders.txt using the query:
SELECT order_id,product_name,qty FROM orders
INTO OUTFILE '/tmp/orders.txt'
This will create a tab-separated file, each row on its own line. To
alter this behavior, it is possible to add modifiers to the query:
SELECT order_id,product_name,qty FROM orders
INTO OUTFILE '/tmp/orders.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
In this example, each field will be enclosed in double quotes, the
fields will be separated by commas, and each row will be output on a
new line separated by a newline (\n). Sample output of this command
would look like:
"1","Tech-Recipes sock puppet","14.95" "2","Tech-Recipes chef's hat","18.95"
Keep in mind that the output file must not already exist and that the
user MySQL is running as has write permissions to the directory MySQL
is attempting to write the file to.
Syntax
SELECT Your_Column_Name
FROM Your_Table_Name
INTO OUTFILE 'Filename.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
Or you could try to grab the output via the client:
You could try executing the query from the your local client and
redirect the output to a local file destination:
mysql -user -pass -e "select cols from table where cols not null" > /tmp/output
Hint: If you don't specify an absoulte path but use something like INTO OUTFILE 'output.csv' or INTO OUTFILE './output.csv', it will store the output file to the directory specified by show variables like 'datadir';.
You can write following codes to achieve this task:
SELECT ... FROM ... WHERE ...
INTO OUTFILE 'textfile.csv'
FIELDS TERMINATED BY '|'
It export the result to CSV and then export it to excel sheet.