Convert varchar to sdo_geometry

I have a table of linestrings - all coordinates have precision, which I want to remove.
I am trying to do it in PL/SQL as below, but since the new geometry with the rounded coordinates is a varchar, I keep getting the error:
PL/SQL: ORA-00932: inconsistent datatypes: expected UDT got CHAR
Has anyone got any ideas how I can round the coordinates within the varray, without having to take them out and put them back in?
SET SERVEROUT ON size 200000
     DECLARE
     i          number := 1;
     k          mdsys.sdo_geometry;
     geom          varchar2(32767);
     BEGIN
     SELECT     GEOMETRY
     into     k
     FROM     TABLE
     WHERE     id = &id;
          geom := 'MDSYS.SDO_GEOMETRY(2002, NULL, NULL, MDSYS.SDO_ELEM_INFO_ARRAY(1, 2, 1), MDSYS.SDO_ORDINATE_ARRAY(';
          while i < k.sdo_ordinates.count LOOP
               geom := geom || round(k.sdo_ordinates(i)) || ',';
               i := i + 1;
          end loop;
          geom := geom || round(k.sdo_ordinates(i)) || ')' ;
          dbms_output.put_line(geom);
          UPDATE     TABLE
          SET     GEOMETRY = geom
          WHERE     id = &id;
     END;
/

Got it working after the original post. Just didn't realise that sdo_ordinates behaves the same as any other varray collection.
In the end I used it as a trigger.
create or replace trigger tr_ins_geom
before insert on <table> for each row
     DECLARE
     i          number := 0;
     geom          mdsys.sdo_geometry := :new.geometry;
     point_x          number := round(:new.geometry.sdo_point.x);     
     point_y          number := round(:new.geometry.sdo_point.y);     
     new_point     mdsys.sdo_geometry;
     BEGIN
     if (point_x is null and point_y is null) then     
          for i in 1..geom.sdo_ordinates.count LOOP
               geom.sdo_ordinates(i) := round(geom.sdo_ordinates(i));
          end loop;
          :new.GEOMETRY := geom;
     else
          new_point := MDSYS.SDO_GEOMETRY(2001, NULL, MDSYS.SDO_POINT_TYPE(point_x, point_y, NULL), NULL, NULL);
          :new.GEOMETRY := new_point;
     end if;
end;
/

Similar Messages

  • How to convert varchar to date datatype while insert or update in table

    Hai All
    I need to convert to varchar to date.
    I have two Tables T1,T2
    T1 Structure
    Code varchar
    Time varchar
    Date varchar
    T2 Structure
    Empname var
    Empcode var
    Intime date
    Outtime date
    Intrin date
    Introut date
    Att_date
    Now i need to move Time form T1 to T2 Intime,outtime,intrin,introut according some condition
    So now i need to convert Varchar to Date while insert or update
    I have tried something
    Insert into T1 (code,intime,att_date)values
    (code,To_date(Date||time,'dd-mon-yyyy hh24mi'),att_date);
    OR While update
    Update T2 set Outtime=To_date(Date||time,'dd-mon-yyyy hh24mi') where...
    I got an error Ora-01861
    Regards
    Srikkanth.M

    You didn't show any example of your date or time values, butyou might need to add a space between them, like
    To_date(Date || ' ' || time,'dd-mon-yyyy hh24mi')

  • Convert varchar(20) date to date that looks like mm/dd/yyyy

    Good afternoon,
    I am a total novice at SQL and have been tasked to change a varchar(20) date column (shows up as m/dd/yy) to show up as mm/dd/yyyy. I've looked at the previous posts, but being a novice, it's difficult to wade through the non-applicable stuff. The column
    that needs to be changed is field2, but I'm not sure how to construct a statement to reflect my objective.
    Thank you in advance. 

    The sample uses a table variable. You can program it the same way for a table:
    DECLARE @t TABLE (dt VARCHAR(20))
    INSERT INTO @t
    VALUES ('1/2/12')
    ,('3/10/10')
    ,('7/23/9')
    ,('aa')
    ,('12/31/99')
    ,('10/20/00')
    ,('10/20/01')
    SET DATEFORMAT mdy
    SELECT dt
    ,CONVERT(VARCHAR(10), CASE
    WHEN isdate(dt) = 0
    THEN NULL
    ELSE cast(dt AS DATE)
    END, 101) AS newDt
    FROM @t;/******** TRY_CONVERT() method ************/
    SELECT dt, newDt=CONVERT(varchar(10),TRY_CONVERT(date,dt),101) FROM @t;
    GO
    dt newDt
    1/2/12 01/02/2012
    3/10/10 03/10/2010
    7/23/9 07/23/2009
    aa NULL
    12/31/99 12/31/1999
    10/20/00 10/20/2000
    10/20/01 10/20/2001
    dt newDt
    1/2/12 01/02/2012
    3/10/10 03/10/2010
    7/23/9 07/23/2009
    aa NULL
    12/31/99 12/31/1999
    10/20/00 10/20/2000
    10/20/01 10/20/2001
    Starting with SQL Server 2012 you can use TRY_CONVERT().
    You can start studying datetime functions here:
    http://www.sqlusa.com/bestpractices/datetimeconversion/
    Kalman Toth Database & OLAP Architect
    T-SQL Scripts at sqlusa.com
    New Book / Kindle: Exam 70-461 Bootcamp: Querying Microsoft SQL Server 2012

  • Convert varchar(255) to varchar(50)

    I want to transfer data from a staging table to another table. The staging table has columns with datatypes I don't always want, and I'm converting them like so:
    INSERT INTO [dbo].[my_table]
    SELECT CONVERT(datetime,TimeIndex,103),
    CONVERT(decimal(6,3),Network_In,3),
    CONVERT(decimal(6,3),Network_Out,3),
    CONVERT(decimal(6,3),Network_Total,3)
    FROM [dbo].[staging_table]
    How would I convert varchar(255) to varchar(50)?
    Thanks in advance
    Adam

    Using substring is returning the column so thanks but now I can't convert datetime for some reason:
    INSERT INTO [dbo].[mytable]
    SELECT SUBSTRING(ClusterName,1,50) AS 'ClusterName',
    CONVERT(datetime,TimeIndex,103),
    ClusterID,
    CONVERT(decimal(3,2),Memory,3),
    CONVERT(decimal(3,2),CPU,3),
    CONVERT(int,CPUTotal),
    CONVERT(int,MemoryTotal)
    FROM [dbo].[stagingtable]
    GO
    Now I get an error I didn't get before:
    Implicit conversion from data type datetime to decimal is not allowed. Use the CONVERT function to run this query.
    But if I run the datetime convert on it's own as a single column select then it works okay. Any ideas where I've gone wrong there?
    To sum up - the above fails but the below works:
    SELECT CONVERT(datetime,TimeIndex,103) AS 'TimeIndex'
    FROM [dbo].[stagingtable]
    Thanks
    Adam

  • Replace and convert varchar to decimal

    Hi all
    What could be the easiest way to replace and convert varchar(50) to decimal
    My string   '21 807,00'  Need to have it in db as  21807.00
    Thanks
    AKE

    On any version you can use somple convert with replace:
    declare @MyStr varchar(10) = '21 807,00'
    SELECT CONVERT(decimal(10,2), REPLACE(REPLACE(@MyStr, ',','.'),' ',''))
    but if there is a different format and the convert will not work, then you will get error. Therefore it is best (as mentioned) to use TRY_CONVERT on newer server or use Try block on old servers
    [Personal Site] [Blog] [Facebook]

  • Converting varchar to datetime 103

    Hi Guys
    Im making a select query where in a colomn the date is yyyy-mm-dd, i need dd-mm-yyyy (103). A simple convert query does not work because the colomn is not set as a datetime but as a varchar. Its not possible to change the tables at the source.
    The colomn name is DATE_IN
    I think the following post could be helpfull but i dont know how to fix the given statements into the select query
    http://stackoverflow.com/questions/1509977/convert-varchar-into-datetime-in-sql-server

    > in a colomn the date is yyyy-mm-dd, i need dd-mm-yyyy (103)
    As suggested above, this is string date format to string date format conversion, purely string operations.
    If you convert to DATE / DATETIME first (universal internal binary representation), then it may fail if invalid date. 
    You can use the ISDATE or TRY_CONVERT functions to check if the string date is valid.
    DATETIME functions:
    http://www.sqlusa.com/bestpractices/datetimeconversion/
    Kalman Toth Database & OLAP Architect
    SQL Server 2014 Design & Programming
    New Book / Kindle: Exam 70-461 Bootcamp: Querying Microsoft SQL Server 2012

  • How to convert varchar to int in MSSQL?

    Hi,
    I am using mssql. how can i convert varchar to int type.
    for eg.
    id (int) txt(varchar)
    1 ---------10
    2 ---------10a
    3 ---------10b
    i want to convert varchar to int. HOw? pls explain with query.
    Thanks
    edward

    "10a" wouldn't normally be considered a number. So what sort of number do you expect it to be?
    Other than that you can look at the substring and convert/cast functions.

  • Convert Varchar to Time

    I need my code to add the varchar CPUTM field + varchar CPUZIPTIM field which both has time values to see if greater than 2 hours. How do I do this when both fields are varchar. The value in CPUTM field is 
    335:55:20.97 duration time.  My code is below.
    CPUTM =     335:55:20.97 duration time
    CPUZIPTM =     0:00:01.96 
    duration time
    select * FROM [SMF_DATA].[dbo].[System_Management_Facility]
    WHERE ((convert(varchar(13), CONVERT(time, CPUTM) + CONVERT(time, CPUZIPTM))) > '02:00:00.00')

    Well, there is no time span in T-SQL, so you need either to store it in the smallest necessary unit (seconds, milliseconds) or to convert ít on the fly.
    E.g.
    DECLARE @CPUTM VARCHAR(255) = '335:55:20.97';
    DECLARE @CPUZIPTM VARCHAR(255) = '00:00:01.96';
    DECLARE @T BIGINT = PARSENAME(REPLACE(@CPUTM, ':', '.'), 1) +
    PARSENAME(REPLACE(@CPUTM, ':', '.'), 2) * 100 +
    PARSENAME(REPLACE(@CPUTM, ':', '.'), 3) * 6000 +
    PARSENAME(REPLACE(@CPUTM, ':', '.'), 4) * 360000 +
    PARSENAME(REPLACE(@CPUZIPTM, ':', '.'), 1) +
    PARSENAME(REPLACE(@CPUZIPTM, ':', '.'), 2) * 100 +
    PARSENAME(REPLACE(@CPUZIPTM, ':', '.'), 3) * 6000 +
    PARSENAME(REPLACE(@CPUZIPTM, ':', '.'), 4) * 360000;
    SELECT @T / 360000,
    (@T % 360000) / 6000,
    (@T % 6000) / 100,
    (@T % 100);

  • Convert varchar to decimal - arithmetic overflow

    I'm using SQL Server 2014 and I'm trying to convert data from a staging table over to a production table. I seem to be getting an Arithmetic overflow error converting varchar to numeric on the decimal conversion. I'm sure I've overlooked something with the
    syntax of the CONVERT.
    This is the staging table:
    CREATE TABLE [dbo].[staging_table](
    [TimeIndex] [varchar](100) NULL,
    [Cluster] [varchar](100) NULL,
    [AvgMem] [varchar](100) NULL,
    [AvgCPU] [varchar](100) NULL,
    [TotalMemory] [varchar](100) NULL,
    [TotalCPU] [varchar](100) NULL,
    [Datacenter] [varchar](100) NULL,
    [vCenter] [varchar](100) NULL
    ) ON [PRIMARY]
    This is the prod table I'm moving it to:
    CREATE TABLE [dbo].[Clusters](
    [ClusterID] [int] IDENTITY(1,1) NOT NULL,
    [ClusterName] [varchar](25) NULL,
    [DatacenterName] [varchar](25) NULL,
    [TimeIndex] [datetime] NULL,
    [AvgCPU] [decimal](5, 2) NULL,
    [AvgMem] [decimal](5, 2) NULL,
    [TotalCPU] [decimal](8, 2) NULL,
    [TotalMem] [decimal](8, 2) NULL,
    [vCenterID] [int] NULL,
    CONSTRAINT [PK_Clusters_1] PRIMARY KEY CLUSTERED
    [ClusterID] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    and here's an example INSERT INTO statement throwing the error:
    INSERT INTO [dbo].[Clusters] (ClusterName,DatacenterName,TimeIndex,AvgCPU,AvgMem,TotalCPU,TotalMem,vCenterID)
    SELECT SUBSTRING(Cluster,1,25) AS ClusterName,
    SUBSTRING(Datacenter,1,25) AS DatacenterName,
    CONVERT(datetime,TimeIndex,103) AS TimeIndex,
    CONVERT(decimal(5,2),AvgCPU) AS AvgCPU,
    CONVERT(decimal(5,2),AvgMem) AS AvgMem,
    CONVERT(decimal(8,2),TotalCPU) AS TotalCPU,
    CONVERT(decimal(8,2),TotalMemory) AS TotalMem,
    '3' FROM [dbo].[staging_table]
    Sample data is 0.00 to 100.00 in fields AvgCPU and AvgMem, and TotalCPU and TotalMem usually goes up to about 7 digits with no decimal (eg. 7543253) but could be 8 and although I've never seen a decimal I wouldn't rule it out so decided to account for it.
    I assume it's something I've overlooked with the syntax but any ideas would help.
    Thanks
    Adam

    The problem is your precision and scale you are assigning to your decimals.
    decimal(5,2) = this is a total of 5 digits, 3 digits for the whole number and 2 for the fractional.
    decimal(8,2) = this is a total of 8 digits, 6 digits for the whole number and 2 for the fractional. 
    So converting a varchar of 7 or 8 digits for TotalCPU or TotalMem will give you an error because your definition will actually only allow for 6 digits of storage. You could test this by doing decimal(8,0) or decimal(10,2) both which will allow for up to
    8 whole numbers.
    If you are worried about space Sql Server will allocate a set number of bytes for ranges based on the precision (first number in the parenthesis). See this page which explains in detail how much space each range takes up and also further details on
    decimal and numerics.
    -Igor

  • Convert Varchar to Date

    Hi
    im trying to convert varchar to date.
    So, i used To_date ( cilumn, 'DD-MM-YYYY') , Since i have to order the out put in Decending Order i used the same convertion function with Order by clause.
    The problem is ... the out put is not ordered,
    <sql>
    select date_created
    from
    select distinct
    to_date(created_date,'DD-MM-YYYY') as date_created
    from schedule_backup
    order by date_created desc
    <sql>
    out put is
    =================
    DATE_CREA
    26-NOV-09
    23-NOV-09
    19-NOV-09
    16-NOV-09
    11-NOV-09
    04-NOV-09
    30-OCT-09
    11-SEP-09
    04-MAR-10
    01-MAR-10
    25-FEB-10
    DATE_CREA
    22-FEB-10
    17-FEB-10
    15-FEB-10
    11-FEB-10
    09-FEB-10
    03-FEB-10
    27-JAN-10
    20-JAN-10
    15-JAN-10
    04-JAN-10
    thanks
    raj

    Hi, Raj,
    When you store dates in a VARCHAR2 column, you're asking for touble, so it seems that everything is working according to plan.
    Even aside from that, your query works perfectly for me.
    In the output you posted, the order is correct within each year, and you're only showing 2 digits of the year. Are you sure the centuries are right in the schedule_backup table? Perhaps everything that was supposed to be '2010' got enetereed as '1910' or '0010'.
    Post some sample data (CREATE TABLE and INSERT statements) that gets the wrong results with your query.
    For example:
    CREATE TABLE     schedule_backup
    (     created_date     VARCHAR2 (10)
    INSERT INTO schedule_backup (created_date) VALUES ('11-11-2009');
    INSERT INTO schedule_backup (created_date) VALUES ('04-11-2009');
    INSERT INTO schedule_backup (created_date) VALUES ('30-10-2009');
    INSERT INTO schedule_backup (created_date) VALUES ('11-09-2009');
    INSERT INTO schedule_backup (created_date) VALUES ('04-03-2010');
    COMMIT;

  • Convert VARCHAR to date - don't know format in advance

    Hi,
    If I want to convert varchar to date, but I don't kniow the format of the varchar in advance. For example, it may be '20-MAY-99', '2009-01-10 10:01:00', but it is not known in advance. How can I convert it to date?
    Many thanks

    You'll have to define a hierarchy of date formats to try. There are some strings that map to many different valid dates depending on the format. The string '01/02/03' might translate to January 2, 2003, January 2, 1903, February 1, 2003, February 1, 1903, February 3, 2001, February 3, 1901, etc.
    Assuming you define a hierarchy of formats, you can write a function that tries each format in turn, i.e.
    CREATE FUNCTION my_to_date( p_string IN VARCHAR2 )
      RETURN DATE
    IS
      l_date_variable DATE;
    BEGIN
      l_date_variable := to_date( p_string, <<format 1>> );
      return l_date_variable;
    EXCEPTION
      WHEN OTHERS THEN
        BEGIN
          l_date_variable := to_date( p_string, <<format 2>> );
          return l_date_variable;
        EXCEPTION
          WHEN OTHERS THEN
    END;You'll need to define the N date formats to try and the order in which to try them.
    Justin

  • Convert varchar to date in OBIEE-sql server database

    Hi we have SQL server database,
    and we need to convert varchar to to_date .
    In OBIEE we use evaluate,cast etc on Oracle......Not sure about sql server..please help

    i am usiing below formula in my reports-answer side:
    Cast((case when "Job CV Fact"."Total Cv Sent" is not null then "DATE DIM".Date end)as Date)
    below error is coming:
    A general error has occurred. [nQSError: 46046] Datetime value 01/01/08 does not match the specified format. (HY000)
    cast("DATE DIM".Date as date) the above error comes :)

  • In OBIEE rpd convert varchar to datetime

    Hello,
    Can anyone help one how can we convert a column that is set to varchar to datetime in obiee;
    Suppose COLUMN_A is varchar and keeps value like '2008-12-30 10:11:22'
    and we want to convert it DATETIME.
    Thanks and Regards

    use cast(column as TIMESTAMP)

  • Crystal reports converting varchar 254 to memo data type

    Hi All,
    We are still using Seagate Crystal Reports 7.5 for some of our reporting. I have a problem that hopefully someone can help with. One of our connections is to ODBC- AR System ODBC Data Source.
    In that data source there are fields defined as varchar 254 (confirmed by DBA),
    But Seagate is calling them memo fields- which is causing problems since you canu2019t use memos in selection criteria or formulas.
    This happened after the field names were renamed. I noticed when the newly named fields were not showing as an option to map the old name fields to- because by default it looks for same data type, after unchecking same data type option the fields showed for mapping but wrong data type makes them useless. I canu2019t even convert them to string with formula field.
    Any input would be greatly appreciated.
    Thanks-

    Hi,
    Yup, this was a problem with versions prior to 10.  If a field was longer than 254 characters, Crystal handled them like Memos and you cannot use them in formulas.
    The only suggestion I can offer would be to upgrade.  Crystal can now use memos in formulas and no longer has a 254 character limit on strings.  So your charcter fields will remain as charater and not changed to Memos.
    Good luck,
    Brian

  • Converting Varchar into Clob in Oracle 8i

    Hi All,
    Kindly tell me how can we convert a varchar variable into a Clob in Oracle 8i inside a stored procedure. I searched alot on the net, we have a To_Clob function available from 9i but doesn't exist in 8i. There was also talk about using dbms_lob, but i dont know how to use that to convert it.
    Kindly provide me with the details of how to do this as I am just starting out with Oracle.
    Thanks in Advance,
    Sajid.

    Hi,
    You can use the dbms_lob.to_clob() function(In oracle8i).
    In oracle 9i it is not required.
    Thanks & Regards
    venkata

Maybe you are looking for

  • Query does not uses indices

    I am using 10.2.0.1 database instance and wrote a SQL query which involves several tables but I had made sure all columns in where clause has got proper indexes. The query is fairly simple does not use any function etc. in where clause but inner join

  • Unable to Migrate WCS Database to NCS with Demo License

    I have NCS 1.1.0.58 running on a VM with a demo license, should I be able to migrate my WCS 7.0.230.0 database to NCS?  I can not tell if the  migration process worked and there are no objects in NCS.  I have attached a console capture of the migrati

  • Is it possible to improve the LMP Bluetooth in iMac from x4 to x6

    hi Is it possible to improve the LMP Bluetooth in iMac from x4 to x6

  • Error in TDS AddOn

    Hello All, We are using TDS Add On v5.6 for 2007B at a client. For a few days, it has started giving following error - when we try to add an AP Invoice - with TDS: TDS Tax amount is not matching with the Invoice WTAX Amount. Would anybody know the wa

  • Yellow peril.

    In mail, when putting the cursor over items, especially illustrations, I get a yellow colored box pop up, containing what looks like html data - which often obscures what I'm trying to look at, and has never contained anything of interest to me. How