I'm trying to import geography data from a CSV file in to SQL server using the bcp (bulk copy tool) but I can't figure out the format. I would like to use something like this in the CSV files but I get errors on the POINT column:
101932694,POINT(44.0586891174316 -69.2067337036133 4326),2014-07-30,24452353
How can I format geography data in the CSV file so bcp will accept it?
OK, it could not find any information on importing human readable geography data using bcp, so I converted the point data:
POINT (44.058689117431641 -69.206733703613281 4326),...
To the binary form:
E6100000010D000000203B4D51C000000020830746400000000000E6B040,...
And put the binary form in my CSV file. bcp seemed to work fine with that.
I don't know anything about BCP, but hopefully this will give you what you need.
Try importing your data to a temporary table as a string, then importing it into the real table with an update.
Inside of SQL Server spatial data is stored like a varbinary, i.e. your point will be stored as
0xE6100000010C010000203B4D51C0FAFFFF1F83074640
If you try and move the data with SSIS, it actually treats it as varbinary, not as spatial. Because of this, a conversion is required to get your string into the correct format.
SELECT GEOGRAPHY::STGeomFromText('POINT(44.0586891174316 -69.2067337036133)', 4326)
or
SELECT GEOGRAPHY::Point(-69.2067337036133,44.0586891174316,4326)
For those reasons, I am guessing BCP does not implicitly recognize the conversion, and you will probably need to do it manually.
CSV => Temp Table =>
INSERT INTO RealTable (GeogColumn) SELECT GEOGRAPHY::STGeomFromText(GeogString,4326) FROM TempTable
Related
I had to restore a table that contained a datetime column.
I used bulk insert to insert the data from a CSV file. However the import couldn't insert the datetime values because SQL server saw it as a different format.
I ended up modifying the table, removing the datetime data type and replacing it with a varchar.
The issue is the data got converted from this format: 7/15/2015 3:41:57 PM to something like this: 47:47.0
Is there a way I can convert these values back or is the data lost?
As #ChrisSteele mentioned, your data is hosed. It likely got this way by Excel's cool feature to convert datetime strings to integers. Try re-saving the original file in notepad, or changing the format of the column from Date/Datetime to Text if you're using Excel.
I am trying to query results as XML from Oracle db using XMLElement and XMLAgg functions which gives me results in CLOB format. Now, when I try to use this query in Data Source flow task in SSIS, I get an error as unsupported data format.
Query:
select XMLElement("root",
XMLAgg(XMLElement("person",
XMLForest(person.first_name, person.last_name)))) AS "XMLResult"
from person
Question:
How do I use this query in SSIS (2008 R2) to avoid that error or any workaround. Further I need to write the results to a file.
You will need to convert the result into VARCHAR or VARCHAR2 datatype as SSIS doesn't support the XML datatype.
When importing a file.csv to sql server 2008 I am getting a problem.
In my file.csv the decimal is written in this way: (ex. 1234,34112) and It seems that SQL server does not understand the ',' as decimal.
My solution has been to import it using BULK INSERT as VARCHAR and after that convert it to decimal. It works but I guess it may be a better solution which I am not able to get.
Could you help me with that?
Thanks in advance
There are only two ways of doing it one you have already mentioned importing in Sql server and then doing something like this..
CREATE Table ImportTable (Value NVARCHAR(1000)) --<-- Import the data as it is
INSERT INTO #TABLE VALUES
('1234,34112'),('12651635,68466'),('1234574,5874')
/* Alter table add column of NUMERIC type.
And update that column something like this...
*/
UPDATE ImportTable
SET NewColumn = CAST(REPLACE(Value , ',', '.') AS NUMERIC(28,8))
Or you can change it your excel sheet before you import it.
Unless you are using SSIS to import data it is always best to get your data in Sql Server 1st using lose datatypes and then do any data manipulation if needed.
SQL Server Management Studio 17 provides a new direct option to import flat files that handles import of decimal csv columns for you. Right your database, then click Tasks > Import Flat File...
I have a set of xml files that I want to parse the data of and import in to a sql server 2012 database. The provided xml files will be validated against a schema.
I am looking as to what is the best method of doing this is. I have found this: http://msdn.microsoft.com/en-us/library/ms171878.aspx
I am wondering if this is the best way or if there are others?
You have several options:
SSIS XML Source. This does not validate against the schema. If you want to detect and properly handle invalid XML files, create a script task to validate the schema in C#.
Parse the XML in a stored procedure.
Insert the entire XML file in one column. Depending on your schema validation requirements, you can use an untyped or typed XML column. (Or both)
Parse the XML using XPath functions. This is actually very fast.
INSERT INTO SomeTable (Column1, Column2, Column3)
SELECT
YourXmlColumn.value('(/root/col1)[1]','int'),
YourXmlColumn.value('(/root/col2)[1]','nvarchar(10)'),
YourXmlColumn.value('(/root/col3)[1]','nvarchar(2000)'),
YourXmlColumn.value('(/root/col4)[1]','datetime2(0)')
FROM YourXmlTable
I need to generate an SQL insert script to copy data from one SQL Server to another.
So with .net, I'm reading the data a given SQL Server table and write this to a new text file which can then be executed in order to insert this data on other databases.
One of the columns is a VARBINARY(MAX).
How should and can I transform the obtained byte[] into text for the script so that it can still be inserted on the other databases?
SSMS shows this data as hex string. Is this the format to use?
I can get this same format with the following
BitConverter.ToString(<MyByteArray>).Replace("-", "")
But how can this be inserted again?
I tried
CONVERT(VARBINARY(MAX), "0xMyHexString")
This does an insert, but the value is not the same as in the source table.
It turned out you can just directly insert the hex string, no need to convert anything:
INSERT TableName (VarBinColumnName)
VALUES (0xMyHexString)
Just don't ask why I didn't test this directly...
There are two questions on SO that may help:
What is the fastest way to get varbinary data from SQL Server into a C# Byte array?
and
How Do I Insert A Byte[] Into an SQL Server VARBINARY column?