Avatar of Perumal RM
Perumal RM asked on

SQL query help

I have one table with two cols

col1                                                                  col2
-----                                                                  -----
MK109 - Sales Training (All Staff)

What I want is to
1. update col2 with date format
2. If there is any data other than dd/mm/yyyy format (regex) make it to null

I used the below suggested

update source_trim set date_of_birth_1 = to_date(date_of_birth,'dd/mm/yyyy HH:MI AM')
where regexp_like(date_of_birth,'(0[1-9]|[12][0-9]|30|31)/(0[1-9]|1[12])/[0-9]{4}');

It updates the values for example 19/MAY/75, what i want is to append 12:00 AM as well. If its present in col1, i would use that, else I might simply append 12:00 AM.

How do i do that.
Oracle DatabaseSQL

Avatar of undefined
Last Comment
Guy Hengel [angelIII / a3]

8/22/2022 - Mon
Sean Stuber

since col2 is a date,  the 12:00am  or 00:00 is already part of the value.

There is nothing more to add.

Whenever a DATE value doesn't have time specified it always defaults to 00:00:00  (12am)
Sean Stuber

If you're trying to see the time portion of your date then you need to format it into text

for example:

to_char(col2,'yyyy-mm-dd hh24:mi:ss')


to_char(col2,'dd/mm/yyyy hh:mi AM')
Perumal RM

My col2 is varchar.

So its better i append those...
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Guy Hengel [angelIII / a3]

posting my comment from your duplicate question:

udpate yourtable 
  set col2 = case when col1 like '[0-3][0-9]/[0-1][0-9]/[0-9][0-9][0-9][0-9]% 
then replace(col1 , ' at ', ' ' )
else null end  

Open in new window

Perumal RM

Didnt worked. Pasting my original query here

update source_trim
set last_action_date_1 = case when last_action_date like '[0-3][0-9]/[0-1][0-9]/[0-9][0-9][0-9][0-9]%
then replace(last_action_date,' at ',' ')
else null end;

Error starting at line 3 in command:
update source_trim  
set last_action_date_1 = case when last_action_date like '[0-3][0-9]/[0-1][0-9]/[0-9][0-9][0-9][0-9]%  
then replace(last_action_date,' at ',' ')
else null end;
Error at Command Line:5 Column:32
Error report:
SQL Error: ORA-00905: missing keyword
00905. 00000 -  "missing keyword"
Perumal RM

update source_trim
set last_action_date_1=replace(last_action_date,' at ',' ');

I used this and it worked...
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Guy Hengel [angelIII / a3]

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
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.
See how we're fighting big data
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