Oracle CLOB and concatenate

Ran into an PL/SQL error when dealing with a clob variable. The following PL/SQL will cause an error as c_info is more than 32k. This should not be a problem as c_info is a clob. However, because the concatenate (||) is in there, the program is treating it as a varchar.

DECLARE
c_info CLOB;
BEGIN
FOR i IN 1 .. 32000
LOOP
c_info := c_info ||to_char(i);
END LOOP;
EXCEPTION
WHEN OTHERS
THEN
DECLARE
V_ATTACH_ERROR_CHAR_COUNT NUMBER;
BEGIN
V_ATTACH_ERROR_CHAR_COUNT := DBMS_LOB.getlength (c_info);
DBMS_OUTPUT.PUT_LINE (
‘Variable Length:’
|| V_ATTACH_ERROR_CHAR_COUNT);
END;
END;

The fix for this is to change this
c_info := c_info || to_char(i);
To this:
c_info := c_info || to_clob(to_char(i));

If there are additional variables needed in the concatenate string, then each section needs to be within a to_clob to keep everything as a clob.

Special thanks to https://stackoverflow.com/questions/49906725/using-clob-gives-ora-06502-pl-sql-numeric-or-value-error/49906909#49906909 for the solution!

Remove timestamp from file name

This script took a bit of work…

I receive files that are in the format of:
file_MMDDYYYYHHMIS.csv

Example: file_09022022102012345.csv

OK, so I’m not quite sure what the last digits are…all I knew is that I had to strip 9 digits out of the filename in order to just get file_date.csv

Here’s the code I used:

for file in *.csv
do
# For future reference, look for 9 numbers followed by a
# .csv and then replace that with .csv
# sed requires the {}s to be escaped, along with the .
new=$(echo $file | sed -e ‘s/[0-9]{9}.csv$/.csv/’)
# echo OLD$file
# echo NEW$new
mv $file $new
done

Oracle install issue – Resolved!

I was trying to install the Oracle Client on a computer. I downloaded it to “C:\Oracle Download”. Don’t do that. You’ll get a “Error: Unable to access jarfile C:\Oracle” error when you try to install. Renaming the download directory to “C:\Oracle_Download” fixed the issue.

Oracle seqno column using Oracle Sequence

Sometimes the value in an Oracle Column is based on the value of an Oracle Sequence. This is a quick and easy way to make a unique key. The column value is usually automatically determined using a table trigger.

However, it can sometimes get out of sync. Here’s the code to fix it:

DECLARE
  last_used  NUMBER;
  curr_seq   NUMBER;
BEGIN
  SELECT MAX(pk_val) INTO last_used FROM your_table;

  LOOP
    SELECT your_seq.NEXTVAL INTO curr_seq FROM dual;
    IF curr_seq >= last_used THEN EXIT;
    END IF;
  END LOOP;
END;

Credit goes to: https://stackoverflow.com/questions/6099108/best-way-to-reset-an-oracle-sequence-to-the-next-value-in-an-existing-column

Find Unix File by Date or Time

I needed to find all files in my Unix directory that were created within a certain timeframe.

Here’s how to find all that were created on a set date:

find -type f -name “*” -newermt “2019-07-01” ! -newermt “2019-07-02” -ls

Here’s how to find within a time frame:

find -type f -name “*” -newermt “2019-07-01 00:00:00” ! -newermt “2019-07-01 08:00:00” -ls

It finds all files “newer than” the first value, but then excludes any files that are “newer than” the second value.

Find UNIX files in the past day that contains a keyword

Trying to find a file that was modified in the past day that contains a certain keyword…

Here’s the trick:

find -type f -name “<filename search string>” -mtime -1 -exec grep -l “<keyword>” {} \;

Where filename search string is any help you can give it to filter down the file type, such as *.txt or s* and keyword is the phrase you are searching for.

mtime -1 could be modified to be any time frame, such as the past week (mtime -7).

CSS Buttons

I was looking for a GIF image of a + and a X with a box around it.  After some searching, I found an easier way to customize one…use CSS.

Here’s the class code:


.red_button {
background-color: red;
border: none;
color: white;
padding: 2px 2px;
text-align: center;
text-decoration: none;
display: inline-block;
margin: 2px 2px;
cursor: pointer;
font-size: 10px;
width: 20px;
height: 20px;
}

.green_button {
background-color: green;
border: none;
border-radius: 10px;
color: white;
padding: 2px 2px;
text-align: center;
text-decoration: none;
display: inline-block;
margin: 2px 2px;
cursor: pointer;
font-size: 10px;
width: 20px;
height: 20px;
}

The red button is a square and the green one is a circle.

Find WiFi Password

More than a few times now I have had to look up how to find the password for a WiFi network saved on my laptop.  It always takes a while to find a website that accurately/correctly tells the steps.  So, here they are so I can find them again:

  1. Start
  2. Type CMD
  3. Right click on “Command Prompt” an select “Run as Administrator”
  4. To get the names of all saved networks: netsh wlan show profiles
  5. To get the password: netsh wlan show profile name=”name_of_network” key=clear

This will then show the contents of the saved keys, including the password.

Daylight Savings Time Check in Oracle

I needed a way to find if a given date is within a DST date range.  We observe DST here, so I needed to know if I needed to adjust the time or not.  The main purpose was so I could convert a date/time to GMT, which does not observe DST.

First column is current date/time
DST_START is the day DST starts
DST_END is the day DST ends
DST_CHECK will be “Y” if we are currently in DST, “N” if not.
CURRENT_TIME_GMT converts the current time to what it would be in GMT

SELECT SYSDATE,
NEXT_DAY (
TO_DATE (TO_CHAR (SYSDATE, 'YYYY') || '/03/01 02:00 AM',
'YYYY/MM/DD HH:MI AM')
- 1,
'SUN')
+ 7
dst_start,
NEXT_DAY (
TO_DATE (TO_CHAR (SYSDATE, 'YYYY') || '/11/01 02:00 AM',
'YYYY/MM/DD HH:MI AM')
- 1,
'SUN')
dst_end,
CASE
WHEN SYSDATE >=
NEXT_DAY (
TO_DATE (
TO_CHAR (SYSDATE, 'YYYY')
|| '/03/01 02:00 AM',
'YYYY/MM/DD HH:MI AM')
- 1,
'SUN')
+ 7
AND SYSDATE <
NEXT_DAY (
TO_DATE (
TO_CHAR (SYSDATE, 'YYYY') || '/11/01 02:00 AM',
'YYYY/MM/DD HH:MI AM')
- 1,
'SUN')
THEN
'Y'
ELSE
'N'
END
AS dst_check,
NEW_TIME (
SYSDATE,
CASE
WHEN SYSDATE >=
NEXT_DAY (
TO_DATE (
TO_CHAR (SYSDATE, 'YYYY')
|| '/03/01 02:00 AM',
'YYYY/MM/DD HH:MI AM')
- 1,
'SUN')
+ 7
AND SYSDATE <
NEXT_DAY (
TO_DATE (
TO_CHAR (SYSDATE, 'YYYY')
|| '/11/01 02:00 AM',
'YYYY/MM/DD HH:MI AM')
- 1,
'SUN')
THEN
'CDT'
ELSE
'CST'
END,
'GMT')
AS current_time_gmt
FROM DUAL;

To give credit, I used the info from:
https://www.techonthenet.com/oracle/functions/new_time.php
https://stackoverflow.com/questions/287563/in-oracle-how-can-i-detect-the-date-on-which-daylight-savings-time-begins-end