Converting varchar to date

Hi All
when i run the below select statement i get error message. pay_dt is varchar2(8) data type.
ORA-01861 : literal does not match format string
Any help on this appreciated. Thank you very much.
SELECT DISTINCT to_char(to_date(pay_dt
,'YYYY-MM-DD')
,'MON')
from payments

Mahir M. Quluzade wrote:
Your pay_dt datatype varchar2(8), but your gived format YYYY-MM-DD length is 10.
Change yor format please to 'YYYYMMDD' or 'DDMMYYYY' or 'MMDDYYYY' or 'YYYYDDMM' for your payments.pay_dt data.
for examle : pay_dt is '20010528' then use 'YYYYMMDD'unfortunately probably not the case - oracle is surprisingly forgiving with dates and will ignore any separators you add in your format mask if it can still convert it.
SQL> create table foo (dte varchar2(8));
Table created.
Elapsed: 00:00:00.00
SQL> insert into foo values ('20110101');
1 row created.
Elapsed: 00:00:00.00
SQL>
SQL> select to_date(dte,'yyyy-mm-dd') from foo;
TO_DATE(DTE,'YYYY-M
2011-01-01 00:00:00
1 row selected.I'd recommend searching through the data to find the record(s) that are completely around the wrong way or contain invalid characters etc.

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 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(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 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 :)

  • How to convert varchar to date?

    Hello everyone, I'm new to OBIEE and I'm running into a roadblock...
    I have a date that is stored in a varchar2 column in the physical table, like so: 20071225
    Silly I know, but that's what I'm dealing with.
    Anyway, I want to create a logical column in the business model (using the Administration tool) and convert it to a date so that I can do date arithmetic with it, but TO_DATE doesn't seem to be supported.
    In Oracle SQL would do this (I'm using XE):
    SELECT SYSDATE - TO_DATE('20050101', 'YYYYMMDD') FROM DUAL
    Is there any way to accomplish this same thing in the OBIEE? I see the CAST function, but there doesn't seem to be any way to cast a string to a date?

    The original post is 4 years old - I am sure the poster must have figured it out by this time... obiee 11g supports a To_Datetime function though! thanks!

  • Convert varchar to date in BO Universe with Substring and instring function

    to_date(substr(xxxx_v.e_details,instr(xxxx_v.e_details,'|',1,2)+1,9),'DD-MM-YYYY') In the universe while defining the object as date i was facing probs can any body help me out. the column xxxx_e_details is having the datatype varchar in database and the third string is date while i was creating the date object in the universe with the above syntax it was getting the error
    The data in this obect is like this xxxx_v.e_details -> wwww|eeee|MM-DD-YY|
    Edited by: mohammed kashif on Mar 31, 2011 11:47 AM

    i am getting the data but  it is in the format 01-03-0201. The year format  for 2010 is populating as 0201. i am not able to think whether it is due to instring function which is reading data like this. Need suggestion from experts.............

  • Convert VARCHAR to date in OBIEE

    I have an column Period Name and data as
    DEC-04
    DEC-05
    DEC-07
    I want to convert into date format ..so is there any way...i tried with cast but it through error.
    Thanks,
    S

    hi s,
    Try this out :http://varanasisaichand.blogspot.com/2010/01/how-to-change-data-format-to-our-custom.html
    or use evaluate function
    http://varanasisaichand.blogspot.com/2010/05/evaluate-function.html
    Thanks,
    Saichand.v

  • Converting varchar field to date

    I got a varchar field in DB with format YYYY/MM/DD HH:MI:SS in DB . I want to convert this to date field in RPD. When I did
    CAST ( VarChar field AS DATE) in RPD it is not working I am getting sql error.
    Thanks for your help.

    Hi,
    can you try once to cast your char as a timestamp.
    Hope that works.
    Kr,
    A

  • Question - new to BO Data Services Designer - how to automatically transform varchar column data to upper(varchar) across all columns in a table?

    Hello -
    New user to BO Data Services Designer. Company is using Data Services Version 12.2.
    I have many tables that all need the same transformation- converting varchars fields to upper(varchar) fields
    Example:
    I have a table called Items. It has 40 columns, 25 of which are of type varchar.
    What I have been doing is ....
    I make the Item table as the source table then create a Query transform that is then attached to a new target table called - ITEMS_Production.
    I can manually drag and drop columns from the source table to the query and then in the mapping area I can manually type in upper(table_name.column_name) or select the upper function and then select the table.column name from the function drop down list.
    Obviously, I want to do this quicker as I have to do this for lots and lots of tables.
    How can set up Data Services so that I can drag and drop an upper transform quickly and easily or automate this process.
    I also know Python-Java so am happy to script something if need be.
    the logic would be something like -
    if there is a column with a type varchar, add the upper() transformation to column. Go to next column.
    Excited to be using this new tool-
    Thanks in advance.
    Ray

    Use the DS Workbench.

  • 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

  • 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

  • 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 money

    Can someone help me with the syntax on how to convert data
    type varchar to data type money. I'm receiving the following error
    message:
    [Macromedia][SQLServer JDBC Driver][SQLServer]Disallowed
    implicit conversion from data type varchar to data type money,
    table 'tableName', column 'columnName'. Use the CONVERT function to
    run this query.
    Any help would be much appreciated! Thanks.
    SLL

    Within my main page I call all the data from a particular
    table along with a link to edit that particular record. Clicking
    edit takes you to the "edit.cfm page".
    The edit.cfm page displays the data from the particular
    record you clicked.
    When you make changes (or dont make changes) you can update
    the record by clicking a button that takes you to a processor page.
    That runs the following code:
    <!-- Edit or Update -->
    <cfif IsDefined("form.ID")>
    <!--Update-->
    <cfquery datasource="db_name">
    UPDATE dbo.tblName
    SET Type='#form.Type#',
    Price='#form.Price#',
    Category='#form.Category#'
    WHERE ID=#form.ID#
    </cfquery>
    </cfif>
    The error that I receive is:
    [Macromedia][SQLServer JDBC Driver][SQLServer]Disallowed
    implicit conversion from data type varchar to data type money,
    table 'tblName', column 'Price'. Use the CONVERT function to run
    this query.

Maybe you are looking for

  • I updated my 3Gs and most of my songs are gone and I only have 4 apps!

    why did this happen? I need help!

  • Processing soap header in ejb, help me !!!

    Could anyone tell me how can i get the soap header information in ejb to process? I have implement the headercallback in the implementation bean and add the processheader method, but when i invoke the ejb method through proxy, the exception like "you

  • Function and number of rows

    Hi ..is it possible to write a function that will take a table name and a where clause as parameters and return the number of rows in the table that satisfy the where clause...? thanks W

  • Machine Certificate Autoenroll

    Hello All, I was using an Apple Script to Auto-enroll OS X 10.6 in our Microsoft PKI certificate infrastructure (Machines certificate).  The script created all the needed cert request parameters automatically, submitted it via the web based certifica

  • Cross investment data

    Hello, We ara trying to make a cross participation between 2 entities without success. Scenario: A has the 80% of B B has the 5% of A. In the investment data, first we create the participation of A over B as "first consolidation" Then when we go to c