Convert Geometry to Geography - sql-server

I want to convert geometry to geography in SQL Server; I followed this article:
https://blogs.msdn.microsoft.com/edkatibah/2008/08/19/working-with-invalid-data-and-the-sql-server-2008-geography-data-type-part-1b/
Here is my query:
INSERT INTO gCOMMUNE
SELECT
[dbo].[commune].[ogr_fid],
GEOGRAPHY::STGeomFromWKB(commune.ogr_geometry.STAsBinary(),4326)
FROM [IMMATS].[dbo].[commune]
but when I ran the command to convert I got this error:
Msg 213, Level 16, State 1, Line 26 The name or column number of the values provided does not match the definition of the table.

It looks like gCOMMUNE table has different number of columns than the query for selecting the data to be inserted in it. You must specify the column names in the INSERT INTO statement. Assuming there are columns named id and geom (from your clarification it turns out they are [ogr_fid] and [ogr_geog]), here is how your statement could look like:
INSERT INTO gCOMMUNE([ogr_fid], [ogr_geog])
SELECT
[dbo].[commune].[ogr_fid],
GEOGRAPHY::STGeomFromWKB(commune.ogr_geometry.STAsBinary(),4326)
FROM [IMMATS].[dbo].[commune]

Related

Make the datatype of one column a type of another column?

SQL Server
ALTER TABLE Students
AlTER COLUMN Name nickname
I want to ALTER Name column datatype to be as that of the nickname column datatype but an error occurred like:
Msg 2715, Level 16, State 6, Line 1
Column, parameter, or variable #5: Cannot find data type nickname.
Following the example from w3schools on SQL tutorial, there was an example that talked about changing the datatype of one column using another, I tried it on SQL Server Management Studio but it didn't work.

Select text column from table in SQL Server stored procedure

I am having difficulty figuring this out. I have an incident table that contains columns id, comments, incidentdate, and incidentdescID. There are 10 years worth of data in this table. I wrote a stored procedure to extract the last 4 years worth of data but I am running into the following error.
Msg 8152, Level 16, State 10, Line 27
String or binary data would be truncated.
So when I change the date range for the incident to be between 2015 to 2016 I am not getting an error. Then when I change it to be between 2017-2018 I am still not getting an error. But when I change it to be between 2016-2017 I get the error. Also when I comment out the comments column, I do not get an error no matter what date range I put.
So I was thinking there might be a special character in the Comments column which is a text column in the Incident table. If that is the case how would I be able to select that column but remove the special characters in the stored procedure without making changes to the table?
If you suspect your "Comments" column is the culprit then you can search my friend for junk values. I got this error once and fixed by replacing char(10) and char(13) by blanks.
1.
SELECT REPLACE(REPLACE(tbl.comments, CHAR(10), '*JUNK*'), CHAR(13), '*JUNK*') AS CleandComments
FROM [your table name] tbl
Copy your query result into any editor and search for records corresponding JUNK keywords.
This ideally happens when you are importing data from excels or source tables with NVARCHAR datatype whereas your destination is a CSV or accepts only VARCHAR.
If above is your case then you simply need to put REPLACE function on your column/s in your procedure

Copy one table data into another db by using INTO Select

I am getting this error :
Msg 156, Level 15, State 1, Line 39
Incorrect syntax near the keyword 'in'
My code:
SELECT *
INTO EmployeesBackup IN 'DB2.mbd'
FROM Employee
Your syntax is off, and perhaps you intended to do an INSERT INTO ... SELECT:
INSERT INTO DB2.EmployeesBackup
SELECT *
FROM DB1.Employee;
This would work assuming that both databases are on the same server, and that your backup table EmployeesBackup has the same definition as the Employee table.
Try this:
INSERT INTO [DBName].dbo.EmployeesBackup
SELECT * FROM [DBName].dbo.Employee
Please note that, Either both database should be available on the same SQL Server OR if one of them is available on the different server then it should be Linked

String or binary data would be truncated When try to insert to a float field

I'm working on SQL Server 2008.
I delete all data from a table and then I try to insert value to the table. Here's the code:
TRUNCATE TABLE [dbo].[STRAT_tmp_StratMain]
INSERT INTO [dbo].[STRAT_tmp_StratMain] ([FileNum])
SELECT [dbo].[STRAT_tmp_Customer].[NumericFileNumber]
FROM [dbo].[STRAT_tmp_Customer];
The FileNum in STRAT_tmp_StratMain is float number and is also index and can't be null.
NumericFileNumber is float and can be null but is never null and there are no duplicates in it (each row is unique number).
The table STRAT_tmp_StratMain contain much more fields but all can be null and also has a defualt values.
When I try to run this query I get the error:
Msg 8152, Level 16, State 4, Line 1 String or binary data would be
truncated. The statement has been terminated.
I tried also to do simply:
INSERT INTO [dbo].[STRAT_tmp_StratMain] ([FileNum]) Values (1);
Still get the same error.
Any ideas?
Thanks,
Ilan
I am not able to reproduce your issue. When I run this code on SQL Server 2008, I get no error:
DECLARE #tt TABLE (FileNum float NOT NULL);
INSERT INTO #tt (FileNum) VALUES (1);
Check the Default constraints on all the columns in your target table and make sure none of them would try to insert a string value that would truncated by the datatype limitations of the column.
example: SomeColumn varchar(1) DEFAULT 'Hello'
This due to the data you are trying to insert does not fit in the field: if you have a defined length of (say) 10 or 50 characters but the data you are trying to insert is longer than that.

MS SQL server 2005 - Error while querying with a column name as key

I have a table with a column name as "key". I am unable to filter based on that column
select * from myTable where key='someVal'
I get the following error
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'key'.
I cannot change the column name. How I can circumvent this issue?
It's because key is a keyword. If you have keywords as object names, you need to put them in brackets:
select * from myTable where [key]='someVal'

Resources