Plsql date function delete data in table - plsqldeveloper

Good morning
In one table. I have LOADDATETIME column name- DATE datatype.. And value will be in the "13/06/2022 18:46:23"
I need to delete rows in the table by using Loaddatetime column..
How can I delete. Could u pls tell anyone

It is kind of unclear what you actually have. Title mentions "PL/SQL" which reads as "Oracle" (it is procedural extension to its SQL). Tags you used (PL/SQL Developer and TOAD) are just tools we use to access various databases - and it doesn't have to be Oracle. Code depends on database you use, not a tool.
Anyway, I'll presume you do use Oracle (and suggest you to fix tags and be more specific next time).
This ("13/06/2022 18:46:23") is just representation of that column's value; Oracle stores it in its internal format (irrelevant for this discussion).
If you want to delete rows regarding to loaddatetime column, then it should be part of the where clause.
There are various options; you never said what exactly you want to delete, so here are a few examples.
To delete rows with exactly that date value (use to_date function which will - according to provided format model - convert that string into a valid date datatype value):
delete from your_table
where loaddatetime = to_date('13/06/2022 18:46:23', 'dd/mm/yyyy hh24:mi:ss');
To delete rows between two dates (I'm using date literals in this case):
delete from your_table
where loaddatetime between date '2022-06-01' and date '2022-06-15';
And so forth; for a more precise suggestion, be more specific.

Related

null and not null mysql

Hi I have started moving access into mysql and I was wondering if there is a constraint or something I could use to be able to make a column not null and still have empty values in it?
This is not my own database, if it was I would just fill in the empty fields and then change the column to not null.
Yes, there are various approaches for modelling missing information without using nulls.
You can choose a value to represent missing. It's quite hard to genrealize so here are a few examples. For the end_date attribute in an open-ended period (i.e. has started but is in progress and not yet finished), use a far-future date such as 9999-12-31. For a person_middle_name attributte, Joe Celko suggests placing metadata values in double-curly braces e.g. {{NK}} for 'not known', {{NA}} for 'not applicable', etc.
Another somewhat intuitive approach for modelling missing information is by the absence of a row in a table. If an employee is unsalaried then do not add a row for them in the Payroll table, thus making them distinct from a salaried employee who is currently receiving no salary represented by a salary_amount of zero in the Payroll table.
A further approach is by the presence of a row in a table. You could have tables for Salaried, Unsalaried and SalaryUnknown and ensure every employee has one row in exactly one of these tables (perhaps enforced in MySQL using triggers and/or procedures).

mysql - storing a range of values

I have a resource that has a availability field that lists what hours of a day its available for use?
eg. res1 available between 0-8,19-23 hours on a day, the range here can be comma separated values of hour ranges. e.g are 0-23 for 24 hour access, 0-5,19-23 or 0-5,12-15,19-23
What's the best way to store this one? Is char a good option? When the resource is being accessed, my php needs to check the current hour with the hour defined here and then decide whether to allow this access or not. Can I ask mysql to tell me if the current hour is in the range specified here?
I'd store item availability in a separate table, where for each row I'd have (given your example):
id, startHour, endHour, resourceId
And I'd just use integers for the start and end times. You can then do queries against a join to see availability given a certain hour of the day using HOUR(NOW()) or what have you.
(On the other hand, I would've preferred a non-relational database like MongoDb for this kind of data)
1) create a table for resource availability, normalized.
CREATE TABLE res_avail
{
ra_resource_id int,
ra_start TIME,
ra_end TIME
# add appropriate keys for optimization here
)
2) populate with ($resource_id, '$start_time', '$end_time') for each range in your list (use explode())
3) then, you can query: (for example, PHP)
sql = "SELECT ra_resource_id FROM res_avail where ('$time' BETWEEN ra_start AND ra_end)";
....
I know this is an old question, but since v5.7 MySQL supports storing values in JSON format. This means you can store all ranges in one JSON field. This is great if you want to display opening times in your front-end using JavaScript. But it's not the best solution when you want to show all places that are currently open, because querying on a JSON field means a full table scan. But it would be okay if you only need to check on for one place at the time. For example, you load a page showing the details of one place and display whether it's open or closed.

how to convert multiple mysql column string to date

please i have a modified_by column in mysql table with string like this "40,1280825613|40,1280825617". the "40" is the id of the user that made changes to the record and "1280825613" is the date string. The | separates different periods of change.
How do i change the entire column to a readable date without affecting the user id. either mysql or php solution is welcome. Thank you so much.
I'd recommend a PHP script. You'll need to make two columns modified_by to retain the user id and modified for the timestamp. If there are multiple modified_by for each record you'll probably want to make a separate table, i.e. revisions. This would be the best way to store the data relationship. I'd also recommend not storing formatted data. You should already see why that's not a good idea. The database is the raw data, use PHP to format it.
Once you have that setup, just:
Select the data from the old table.
Loop over the records
explode() the column on |
Loop over the array
explode() the element on ,
Insert into new columns/table
Forgive me, but I'd rather teach you how to fish.

MySQL Database Design Questions

I am currently working on a web service that stores and displays money currency data.
I have two MySQL tables, CurrencyTable and CurrencyValueTable.
The CurrencyTable holds the names of the currencies as well as their description and so forth, like so:
CREATE TABLE CurrencyTable ( name VARCHAR(20), description TEXT, .... );
The CurrencyValueTable holds the values of the currencies during the day - a new value is inserted every 2 minutes when the market is open. The table looks like this:
CREATE TABLE CurrencyValueTable ( currency_name VARCHAR(20), value FLOAT, 'datetime' DATETIME, ....);
I have two questions regarding this design:
1) I have more than 200 currencies. Is it better to have a separate CurrencyValueTable for each currency or hold them all in one table?
2) I need to be able to show the current (latest) value of the currency. Is it better to just insert such a field to the CurrencyTable and update it every two minutes or is it better to use a statement like:
SELECT value FROM CurrencyValueTable ORDER BY 'datetime' DESC LIMIT 1
The second option seems slower.. I am leaning towards the first one (which is also easier to implement).
Any input would be greatly appreciated!!
p.s. - please ignore SQL syntax / other errors, I typed it off the top of my head..
Thanks!
To your questions:
I would use one table. Especially if you need to report on or compare data from multiple currencies, it will be incredibly improved by sticking to one table.
If you don't have a need to track the history of each currency's value, then go ahead and just update a single value -- but in that case, why even have a separate table? You can just add "latest value" as a field in the currency table and update it there. If you do need to track history, then you will need the two tables and the SQL you posted will work.
As an aside, instead of FLOAT I would use DECIMAL(10,2). After MySQL 5.0, this will actually have improved results when it comes to currency handling with rounding.
It is better to have one table holding all currencies
If there is need for historical prices, then the table needs to hold them. A reasonable compromise in many situations is to split the price table into a full list of historical prices and another table which only has the current prices.
Using data type float can be troublesome. Please be sure you know what you are doing. If not, use a database currency data type.
As your webservice is transactional it is better if you'd have to access less tables at the same time. Since you will be reading and writing a lot, I would suggest having a single table.
Its better to insert a field to the CurrencyTable and update it rather than hitting two tables for a single request.

SQL Select Query with single field as formatted and remaining fields as wildcard

I'm trying to fetch records from a table in MySQL 5.5 (Community Server) which has 22 fields in total, the first field is of type DATETIME, and I want that field formatted with DATE_FORMAT() method, while I want to fetch remaining fields as they are. So basically what I'm trying to do is something like this
SELECT DATE_FORMAT(fdate,'%r %d-%m-%Y'),* FROM userdetails;
While I know this is invalid syntax, and currently I'm accomplishing this by selecting all the remaining fields manually which obviously makes the query longer. So is there a more elegant way to do the same? I also thought to use two different queries where 1st will fetch the formatted date and second will be usual
SELECT * FROM userdetails;
But since I fetch records sorted by date field itself, I guess using two different queries may lead to conflicts on order of records returned.
You can use
SELECT DATE_FORMAT(fdate,'%r %d-%m-%Y'), ud.*
FROM userdetails ud
which will select all columns (including the original fdate) along with the formatted value.
There is no way to select "almost all columns" except listing them explicitly.
Why don't you do the date formatting in the application code? Let the database handle the data and let the application handle presentation.
In MySQL you can reverse it and put the * first and then columns you want to specify. You'll just get two fdate columns. If you're using hashes in perl or associative arrays in PHP, or the equivalent in other languages, to fetch the rows then you'd end up having that index overwritten.
Best way to deal with it is to name the column:
SELECT *, DATE_FORMAT(fdate,'%r %d-%m-%Y') AS formatted_date FROM userdetails;
Now you essentially have two date columns, you just used the named one when you want to use your date.
DATA_FORMAT(fdate,'%r %d-%m-%Y').....
wont work
DATE_FORMAT(fdate,'%r %d-%m-%Y')....
SELECT DATE_FORMAT(fdate,'%r %d-%m-%Y') AS `formattedDate` , * FROM userdetails;
That should do the trick. Can't se why your first effort fails, you should check the field names it returned - suspect you'll find one called DATE_FORMAT( ....