Can anyone please explain the below SQL statement from Oracle DB:
select decode(TRIM(to_char(SYSDATE,'Day')),'Monday','3','1') from dual
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
First of all, let’s start with the
to_char.to_char(SYSDATE,'Day')will give you the day of the week it is today.to_charlets you convert a date (in this case today’s date since you’ve specifiedsysdate) into a string of a certain format. Take a look here for some other examples of date formats you can use:http://www.techonthenet.com/oracle/functions/to_char.php
trimremoves leading and trailing whitespace.Now for the
decode. You can think ofdecodelike an if else statement. Take a look at:http://www.techonthenet.com/oracle/functions/decode.php
In your particular example, you could read this statement as: if today is Monday return 3 else return 1.
decodealso allows you to do things a bit more complex like this:This would read: if today is Monday return 3, else if today is Tuesday return 5, else return 1