How to select only workday [closed] - mysql

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have 2 columns, date and price. The input will be 2 different dates and I want to get date and price between those (only workdays, no need to consider weekend days). How can I make this query in SQL? I really have no clue, thanks.

SELECT date, price
FROM table
WHERE date BETWEEN #start AND #end
AND WEEKDAY(date) < 5

where date_field > #start_date
and date_field < #end_date
You could also use the "between" keyword, but I always forget if that's inclusive or not.
As far as weekday goes, be very careful about using weekday(date_field) (as Barmar) suggested in the where clause as it could trigger that function being run on every row in the table which would negate any indexes you have. It's possible to use it and it be okay if the filter above is pretty stringent I think - you'll have to play with it and look at the explain statements.

There's a lot more to it than meets the eye. Really you need a calendar table per year that shows the workdays and excludes the weekends and public holidays ... which of course is region-dependent ...

Related

WHERE not working [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Any thoughts on why WHERE clause would not work? I have made sure I have double checked the coding and spelling etc. It will not work with a simple WHERE clause, and I have tried using different operators, operators that I know are in the table. I have tried using trailing spaces, I have copied and pasted the output from SELECT DISTINCT column_name FROM table. In short I have really made sure that it is not just some dumb error on my part before posting this question.
Here are the specs of the column that is giving me trouble. UTF8_General_Ci and it VARCHAR(100).
I have never had any problems with a WHERE clause, not like this at least. Any thought? Thanks in advance
Here is the code;
SELECT site_specialty
FROM site WHERE site_specialty='INTERNAL MEDICINE'
there is no error message, it just comes up blank 0 rows returned
try this
SELECT site_specialty FROM site WHERE site_specialty LIKE '%INTERNAL MEDICINE%'
Before giving up entirely, I would try:
SELECT site_specialty
FROM site
WHERE upper(site_specialty) like '%INTERNAL%MEDICINE%';

how to get last updated column name from mysql [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Is it possible to get a particular updated column name from a record?
if so, please help me. since i am creating a history table to store a updated column and their values from rest of the tables.
Please suggest me a correct way to deal this....
I believe it is not possible.
However, you can create an update trigger which monitors the table's columns and have that trigger insert records in your history table. Hope this helps.
The only way for doing this is Using something like a Log file, in this case you will create a table that will contains the updated columns each time there is an update.
In this way you will be able to get the last record that will contains the table_name and the column_name.
And you can use a query looks like that:
select id, column_name, table_name
from Log_table
order by id desc
limit 1

Get current month birthday list form employee table [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I have an employee table with dob. Every month i want the list of current month birthday list.
I want the query for the current month employee birthday list. The dob file date like this '31-01-1986' and so on. Please help me to get this.
Since your dob column contains values like '31-01-1986', it must be a string datatype rather than a temporal one. This makes it difficult (and slow) to perform date operations such as you desire; you may be better off doing a pure string manipulation instead:
SELECT *
FROM employee
WHERE CAST(SUBSTRING(dob, 4, 2) AS UNSIGNED) = MONTH(CURRENT_DATE)
I recommend that you convert your table to use an appropriate temporal datatype, such as DATE:
ALTER TABLE employee ADD COLUMN new_dob DATE AFTER dob;
UPDATE employee SET new_dob = STR_TO_DATE(dob, '%d-%m-%Y');
ALTER TABLE employee DROP COLUMN dob, CHANGE new_dob dob DATE;
(Remember also to adjust indexes, as desired).
Note that you will accordingly need to update your application code to work with DATE literals, including the previous statement:
SELECT *
FROM employee
WHERE MONTH(dob) = MONTH(CURRENT_DATE)
Use this
WHERE month(dob) = month(now)

Can I select record in MYSQL: Sorted according to the date/time created, even if table dont have date/time stamp in it? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I want to sort records in my table according to the date/time created.
But i don't have any data/time stamp column in my table.
Is there a way in which i can still select records according to the date/time created or modified???
I am using MYSQL.
I tried SELECT * from user ORDER BY created_at but this also not working.
As other commenters have pointed out, the short answer is no. MySQL doesn't allow you to sort by the time at which a record was created unless you have explicitly stored that value in the database. Here's a possible way out for the future:
ALTER TABLE users ADD COLUMN created_at DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00';
ALTER TABLE users ADD INDEX created_at (created_at);
Then your future INSERT queries will add a NOW() argument to populate the created_at column.
If you do that, then when you retrieve records, if they have a "created at" value equal to the default, you'll know the record was created before you modified your INSERT query. You could go through and manually update those records, or assign them all today's date for simplicity.

Mysql query to find mobile phone numbers [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I have a mysql table with a numbers column, this contains a list of UK mobile phone numbers (beginning in 07) and some UK landline numbers (beginning in 01).
How could I query for just the land line numbers?
I know I could use some kind of regex in my application, but if possible I would rather do it using a query.
Any help would be appreciated!
Select * from MyTable where phoneNumber like '01%';
Try
Select * from `someTable` where `phoneNumber` like '01%';
Use the Like operator in your query. For example "select number from tbl where number Like '01%'"
Try to read something about the WHERE condition and LIKE comparison.
For example here: The SQL LIKE Operator