Solved

Oracle trigger for the day name of the week

Posted on 2013-12-12
8
891 Views
Last Modified: 2013-12-16
I am saving values to a table and I have a date that I need to get the DayOfWeek saved to another column. I want to do it in a proc or maybe in a trigger? I am not sure what the syntax is. I know in MySQL I can use DAYNAME('12/12/2103') and it would return Thursday but Im not sure what it is in Oracle.

Thanks
JK
0
Comment
Question by:jknj72
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 74

Expert Comment

by:sdstuber
ID: 39715137
to_char(sysdate,'fmDay')
0
 
LVL 77

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 250 total points
ID: 39715139
try this:
select to_char(to_date('12/12/2103','MM/DD/YYYY'),'Day') from dual;

It's all about the format masks.  The online docs for your version will show you what they all are.
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 250 total points
ID: 39715148
you might not want to use 'Day'  because that will right pad the names

'fmDay' will have them trimmed

for a trigger to do it, it might look something like this...


CREATE OR REPLACE TRIGGER trg_your_table_briu
    BEFORE INSERT OR UPDATE
    ON yourtable
    FOR EACH ROW
BEGIN
     :new.dayofweek := to_char(:new.your_date_column,'fmDay');
END;
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39715166
If you are using 11g or above, I would look at using Virtual Columns.

These are derived when the column is selected and not permanently stored. Then you don't have to worry about triggers or procedures.

Check the example in the docs:
http://docs.oracle.com/cd/E11882_01/server.112/e25494/tables.htm#ADMIN11004

...
 hrly_rate  NUMBER(7,2) GENERATED ALWAYS AS (sal/2080),
...

hrly_rate is derived based on the sal column's value.
0
 
LVL 32

Expert Comment

by:awking00
ID: 39715300
I calculated 12/12/2103 to be a Wednesday. Did I do something wrong?
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39715414
>>Did I do something wrong?

The typo of 2013?
0
 
LVL 12

Expert Comment

by:stefan73
ID: 39715450
Why do you want to store the weekday in a column? This results in de-normalized data. Use a view (or virtual column) for this.
0
 

Author Closing Comment

by:jknj72
ID: 39721398
I decided to split points with you two. Thank you both for your help.
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question