How can I save only the day/month part of a date? - mysql

I want to save in a database table the month and day part of a date. That will be fixed regardless of the actual year so I don't need to save the year as well.
I could define a type like: char(5) and save it as: 01-01 but this would not "protect" from garbage strings.
Is there a way to define it better? E.g. does an enum for month and day make sense? (though still something like 02-30 could be defined which is of course meaningless)

select to_char(to_date('2011-01-17', 'yyyy-mm-dd'),'dd-mm') from dual;
17-01
It`s written in Oracle, you can use logic from the Query for SQL.

Related

SQL query with date filter but use system year

I´m creating a static reports for every month for every year from our ERP database, contract table. I've managed to get everything right but now I have to alter the monthly queries every year. Currently I use this to separate the months:
WHERE [Ending Date] >= '04.01.2021' and [Starting Date] <= '04.30.2021'
As said the output is right but what I want is the query to use system year, so only dates are input in the query and therefore when the year changes, it would change in the query automatically.
The question isn't too clear, but what I vaguely understood is you want to get the year dynamically (not sure if you want just the year or the whole date) but you can make use of YEAR() function (something like YEAR(CURDATE()) could get you the year of the current date). You can also take a look here for more useful functions.
I couldn't form a query or tell you what functions to exactly use because I doubt I'm fully understanding. Some functions you may make use of are PERIOD_ADD() and TIMESTAMPADD().

CURDATE functionality from db2 query

I'm trying to apply 'curdate()' functionality to a select statement from DB2. I'm used to MySQL but I'm still trying to get the hang of a lot of the DB2 functionality and how to essentially marry the two.
My query is complete except for one line. I'm trying to select based on a ship date, which is the column EXTD1H and I need to check it against today or curdate(). The problem is that column in DB2 is an integer format, not a date format, and I don't have the option of changing it. In prior inserts to mysql, I've been able to put it into Y-m-d format and I know I can trim the year using LEFT(EXTD1H, 4) but I have no idea how to modify my select so that I can say WHERE EXTD1H is today so that I'm only selecting records for this date.
Here's the query:
select
invnoz as ORDER,
fstatz as STATUS
from gportafl
/*where EXTD1H is curdate, hypothetically*/
AND FSTATZ <> 'S'
limit 20;
As you can see, I have a commented line where my issue is. I'm sure it's simple I just can't seem to find in the documentation exactly what I'm looking for, which is to be able to use that INT column to verify that selected records are from today.
UPDATE:
All values from the column are in YYYYMMDD format i.e.
20180202
but it should be 2018-02-02
It's best not to do operations on the columns, so the indexes are used.
You can typecast the current date to fit your data as follows:
WHERE extd1h = INTEGER(VARCHAR_FORMAT(CURRENT DATE,'YYYYMMDD'))

Using MySQL to return row between to strings with date, but without year

I am currently working on a project in which I want to store commemorative days (like January 8th's World Leprosy Day) in a database. At this moment they're stored in a table which contains:
- an ID
- the date as varchar (stored European style, e.d. "8-01" for January 8th)
- length of the commemorative day (as some span multiple days)
- and the name
The reason I am storing the date as varchar is because the year is irrelevant, and I'm a bit reluctant to just store a year (e.g. 2013) in the database and truncate it.
But here's the problem: I can't seem to find a way to construct a query that will get the rows between dates. I think it's because the way the dates are stored in the database.
I already tried (given day = "8-01")
SELECT * FROM comdays WHERE date(day) BETWEEN date("1-01") AND date("20-01")
But to no avail.
Is there a way to get this thing going with strings? Or do I have to change the date column into a MySQL DATE format?
Thanks in advance!
If you really want to keep non standard date field in MYSQL you will need to use the following format 0108-> mmdd this format allows calculations.
It might also be worth reading the following answers to similar question Save day and month in database

Storing day and month (without year)

I'm having trouble with figuring out the best way to store some data in my database. I've got to store DD/MM dates in a database, but I'm not sure of the best way to store this so that it can be easily sorted and searched.
Basically a user will be able to save important dates in the format DD/MM, which they will be reminded of closer to the day.
The DATE data type doesn't seem completely appropriate as it includes year, but I can't think of another way of storing this data. It would be possible to include a specific year to the end of all occasions, but this almost doesn't seem right.
I've got to store DD/MM dates in a database, but I'm not sure of the best way to store this so that it can be easily sorted and searched.
The best way to store date data, even if the year component is not required, is to use date. When you need to use it, you can remove the year, or replace it with the year being compared against (or current year).
Having it in date column facilitates sorting correctly, integrity, validation etc.
To cater for leap years, use a year like '0004' which allows '0004-02-29'. Using year 4 makes it slightly more complicated than year 0, but as an example, this turns the date '29-Feb' (year agnostic) into a date in this year for comparison with some other field
select
adddate(
subdate(cast('0004-02-29' as date),
interval 4 year),
interval year(curdate()) year)
result: 2011-02-28
Are these dates recurring? If not, how will you keep track of when one has "expired"? If the answer is "the app will manually remove the dates once they have expired", then why not simply store the DD/MM date as the next available instance of that date? For example:
01/02 becomes 2012-02-01, and 04\07 becomes 2011-07-04
The built-in date/time functions are so useful that I strongly recommend you not use varchars or tinyints.
If you really really want to drop the year, then just make TWO columns, one for day and another for month. Then store them separately.
CREATE TABLE `table-name` (
`Day` tinyint NOT NULL,
`Month` tinyint NOT NULL
);
But, it's much better to just use the Date type and then ignore the year in your code.

Sorting by month in MS Access

I am using an MS Access db to track some tasks during the year. Each task has a due Month. I do not want to use exact dates as the convention in my team is to refer to the month. I don't want to store the dates in a date format as team members will be entering the due month by hand.
Is it possible to sort my fields in date order if the date is stored as a text string month? (eg. January, February rather than 31/01/2009, 28/02/2009).
If so, what would such a query look like?
Thanks in advance.
If you are storing only the month name, your will first need to convert to a date to get a month number, use a lookup table (MonthNo, MonthName) or use the Switch function. Here is an example of converting to a date:
SELECT Month(CDate(Year(Date()) & "/" & [MonthNameField] & "/1")) AS MonthNo
FROM Table
However, there is probably a good argument for storing a date based on the month name entered, this would prevent any confusion about years.
This should work
SELECT *
FROM TableName
OrderBy Month(date_field)
I would store the month as an integer 1-12 then you can easily sort them.
I would make a date field.
I would store 1/1/2009 for January 2009, 2/1/2009 for February 2009, and so forth. For display purposes, I'd format it so that it displayed only the month (or Month + Year -- can't imagine how you wouldn't want the year).
This makes it possible to take advantage of date operations on the field without messy conversions of text to date formats.
Thank you all for your responses. Sorry for the delay in responding - I'm working on this issue again now.
For clarity, the DB is to be used to track a schedule of events within a 12 month period. The year does not need to be stored as everything in the DB is referring to the same year. A new copy of the DB will be made at the beginning of 2010.
I'm really keen to actually store the month as a word rather than any kind of value or date field as when bulk adding tasks I will likely edit the table directly rather than use a form.
I realise this is dead but google brought me here while i was looking so thought I would add to it:
I had this problem myself (Access 2010) and found a decent answer here: http://www.vbforums.com/showthread.php?503841-How-to-Convert-MonthName-to-Value(Microsoft-access-2003)
So what I did was have a Query which pulled out the DISTINCT months from my table. Then in the design view i added another column with MonthNo: Month(CDate("1 " & [Month]))and sorted the query on this column
hope this helps someone if not the OP.