Avatar of Maliki Hassani
Maliki Hassani
Flag for United States of America asked on

Oracle: date coverted to text

Experts,

I am trying to create a primary key for my Oracle table, which will use a couple of fields to be unique.  I need to take my report_date field and convert it to a text which mean removing the slashes, spaces and colon.  Here is the output of a record in my report_date field:
1/20/2014 8:34:36 AM

need to convert to:
120201483436AM

Thank you
Oracle DatabaseSQL

Avatar of undefined
Last Comment
Maliki Hassani

8/22/2022 - Mon
SOLUTION
slightwv (䄆 Netminder)

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Maliki Hassani

ASKER
So I have a table that already has records.  It doesn't have a primary key. The table can have duplicate report dates but the report_type is being set as, "Daily" or "Weekly".  I am trying to create a primary key using both fields concatenated.

So what you are saying is that I can keep it in the same format it is now?
Maliki Hassani

ASKER
I am using Aqua data studio and to delete a record from the edit table mode, it requires a primary key.
slightwv (䄆 Netminder)

Is the table column already a date?

If so, yes.  Just create a primary key with two columns:  (report_date,report_type)
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Sean Stuber

Converting dates to text in order to use them as indexes is a terrible idea because it loses value in the process.

For example...

How many values exists between  these two dates (end points inclusive?)

to_date('2014-11-17 14:38:00','yyyy-mm-dd hh24:mi:ss')
to_date('2014-11-17 14:39:00','yyyy-mm-dd hh24:mi:ss')

answer: 61  - this is something Oracle understands about date


How many values exist between these two text strings?
'20141117143800'
'20141117143900'

answer:  millions, billions, trillions or more, possibly much, much more

why?  because in addition to the 61 strings you expect as valid values,  because they are text and not actually dates, the possible range of values inludes values like these.
'2014111714380a'  

'201411171438z'||chr(13)



What this means for your queries is that index and table scans have wildly different scales in how big Oracle must estimate.
Maliki Hassani

ASKER
it's actually a string format.  I am trying to fix that.  The problem is that I am uploading to the table using a macro in excel, which upload as a string.
Sean Stuber

if you already have text values that look like dates, this is opportunity to improve the data model.

Rather than stripping the values of characters to make some canonical string as a new column,  make the new column a date type and convert your text into a date
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
slightwv (䄆 Netminder)

Even if you leave it as a varchar2 column, why must you change it to "120201483436AM" to make it part of a primary key?
Maliki Hassani

ASKER
Slightwv, I thought it would look more like a real GUI ID..

Sdstuber,  so what you are saying is create new column, run a edit command to update the new column (date format), and then a create a new column ('GUI_ID') and truncate the two columns mentioned above, then set as primary?
slightwv (䄆 Netminder)

>>Slightwv, I thought it would look more like a real GUI ID..

It is an index value not a column so what does it matter what it looks like?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Maliki Hassani

ASKER
great point.. :)
Sean Stuber

I was not suggesting doing anything to the old columns, I'm assuming legacy code is still using them.  If not, then sure you could get rid of them afterward.

But create a new date type column, and populate it with the old text content.

Create the new PK on the date column and the other non-text-date column
Maliki Hassani

ASKER
Experts,  I have implemented all the suggestions.   My only issue now is my vba script gives an error "Not a valid month" when I try to upload.  

What I have done to table is added a  new column "Report_Date_Time" as a date format and populated it with the old text as dates.  

From the VBA side, I added a new dim as a Date which is set to Now.
For some reason the upload doesn't think it is a date format to be giving that error.

Any ideas?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Sean Stuber

are you using parameters (binding) or are you simply concatenating the value into the sql?

parameters with correct datatypes are better  - more efficient and help eliminate MANY conversion issues.

if concatenating then you are getting a text string which may or may not be in the format you need.

if it's not in the correct format, then use TO_DATE() with whatever format applies to the text represetnation of your Date variable, or better yet, use a bind parameters
Maliki Hassani

ASKER
Okay, that makes sense.. i am concatenating so that is the issue. Are you saying to use to_date on the vba side?
ASKER CERTIFIED SOLUTION
Sean Stuber

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Maliki Hassani

ASKER
Roger, thanks
Your help has saved me hundreds of hours of internet surfing.
fblack61
Maliki Hassani

ASKER
Thank you for your assistance.