Approximate date column - postgresql

One of my customers would like to have a custom date column, where he could store the year only, a combination of month and year (without the day), or a classic date with day, month and year.
It should be possible to use this field for sorting the data. A "month-year" date should be considered as "01-month-year" for the sort, and a "year" date should be treated as "01-01-year" for the sort.
I could imagine two solutions to that:
Store the date in the standard "day-month-year" format, and keep in a separate column how the date was entered ("year", "month-year", "day-month-year"), so the approximate date can be displayed exactly how it was entered.
Use some sort of custom date column in the postgresql database.
Has anyone experience with that?

You could use date-time functions to extract date components. I don't think it has any sense to create additional columns. Also, some databases allow to create indexes by functions.

Related

PowerBI, DAX. Is there a way to set an upper limit to a slicer?

I'm trying to create some measures using DAX on an underlying data model which I would then use for creating some visuals (line chart, bar chart etc.). Each table in the data model has a data field (date&time) that contains repeated dates and times (more occurencies with the same date&time). To use the metrics in the visuals I need the measures to be grouped for date (only date, not for time).
To achieve the task I've created a calendar table (using CALENDAR in DAX) to set-up a set of dates (only dates) to which refer at for every table in the data model and created relations (dates that points to calendar date) for every table. I set the upper limit of the calendar table to be a year ahead of the max date in the dataset, because some measures need to be evaluated in the future.
In the visual pages, I put a slicer that points to the calendar table dates, but the date interval is too wide. I need the slicer to have a more narrow interval, let's say, only "for the present and the past" but I prefer not to add another calculated table.
In your opinion, is there a way to limit the slicer without changing its reference to the calendar table?
Thanks!
For the calendar you may just use CALENDARAUTO(). It will expand automatically as the data model expands.
For the slicer, just select it, open the filter pane and define a filter with the upper limit you want.
You can use combination of 3 different slicer for your purpose. This is just another option that you can have a look on. There will be 3 slicer for Year, Month & Date. So Date slicer is you final slicer, with 2 other slicer on top of it to generate necessary Dates in the slicer. This way, you are not making any static restriction to generate the Date list but giving the user full control on the Date range. Here below is a sample how the slicers can be look in practical.
User can select any year or multiple year, any month or multiple month. Finally the date slicer will hold Date values accordingly.

Loading date or datetime into date dimension

Let's say I have a date dimension and from my business requirements I know that the most granular I would need to go is to examine the specific day of the month that an event occurred.
The data I am given provides me with the exact time that an event occurred (YYYY-MM-DD HH:MM:SS). I have two opitons:
Before loading the data into the date dimension, slice the HH:MM:SS from the date.
Create the time attributes in my date dimension and insert the full date time.
The way I see it, I should go with the option 1. This would remove redundant data and save some space. However, if I go with option 2, should the business requirements ever change or if my manager suddenly wants to be more granular I wouldn't need to modify my original design. Which option is more commonly used? Are there more options that I did not consider?
Update - follow up question
I receive new data every month. If I used a pre built date dimension with all the dates would I then need to run my script every month to populate the table with new dates of that month or would I have a continuous process where by every day insert into the table one row, which would be that date?
I would agree with you and avoid option 2. A standard date dimension table is at the individual date level. If you did need to analyse by time of day, you could create an additional time of day dimension at the level of a second in a single day, and link to that from your fact table.
Your date dimension should be created by script automatically, rather than from the dates that events occurred. This allows you to analyse across a range of events from other facts, and on dates where no events occur, using a standard, prebuilt dimension.
I would also include the full date/time stamp as a column in the fact table, along with the 'DateKey' to the dimension table. This would allow you some visibility/analysis of the timestamp, you would not lose the data, and would still allow you to analyse by the date dimension.
Update - follow up question
Your pre-built date dimension (the standard way of doing it) would usually contain some dates in the future. There's no reason not to, for example, include another 5 years of dates in the table. But if you'd like it to gradually grow over time, you could have a script that is run once a day, once a month, or once a year to add new dates. Its totally up to you! There are many example scripts for building date dimensions- just google date dimension script. They exist for the language of your choice, e.g. SQL, C#, Power Query, etc.

How to add and retrieve date and time in DB mysql?

First of all im a starter,iam using eclipse.
I want to add current date and time of login in to db and search a day and find out the time between login and logout.
°What is the data type for the date colum in mysql ?
°Is it necessary separate column for date and time ?
°which one i want to import, java.util.date or java.sql.date ?
°In Java code simple date format or calender is better ?
Advanced thanks.....
You might want to read this:
Should I use field 'datetime' or 'timestamp'?
For example, if you have mysql populate the log record's date/time (using "DEFAULT CURRENT_TIMESTAMP" in your field definition), you will want to use timestamp. For certain situations where you fill a date value from your application, you may wish to use datetime. Be careful with timezones.
Here are the date functions in mysql:
https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html
DATEDIFF(), for example, will calculate the number of days between two datetimes. If you use unix timestamps, you can use standard arithmetic between the values to find the number of seconds between them, and then calculate minutes, hours, days, etc. by dividing appropriately.
This answer is focused on how to handle the dates in mysql. Not enough info to provide guidance on java.

Should I break down a date field into month, day, and year in core data to easily group and fetch date by month and year?

How do I perform an equivalent of SQL extract/fetch records by month and year from a date in swift?
Two requirements are as follows:
List records by Month and Year
September 2015
October 2015
Display transactions falling under the Year and Month.
Edit: I do mean core data. Rephrased "equivalent in SQL" to equivalent of SQL" to clarify.
CoreData will store dates as doubles, which is the internal format for NSDate.
If you have a small database, just store the values as NSDate and filter based on that.
However, if you have a large database, you may want to denormalize your data a bit more.
You could store another field, say in "yyyymmdd" format, which gives you most of the filtering you require.
For finer grained control, you can also keep separate attributes for year, month, and day.
You may want to setup those fields as indexes as well, if you do lots of searching over vast amounts of data.
Remember, CoreData uses SQLite under the hood for most databases, but it's behavior characteristics are very different.
There is no universal "best" way, as it depends entirely on your data and how you use it.

db2 getting business dates between given dates

I have a table called "Publicholidays" where in dates are stored as Varchar.
My query should fetch all values from say table xxxx between the user selected dates that exclude the weekends(sat,sun), public holidays. I am new to DB2 so can anyone suggest me ideas please
Note: in DB dates are stored as String.
Mistake #1 - Storing dates as strings. Let's hope you have at least stored them YYYY-MM-DD and not MM-DD-YYYY.
Mistake #2 - Instead of a "Publicholidays" table, you need a Calendar (aka Dates or date conversion) table. It should have a record for every day along with a few flag columns BUSINESS_DAY, WEEKEND, PUBLIC_HOLIDAY. Alternatively, you could have a single DAY_TYPE column with values for business day, weekend and holiday. You'll also want to have a STRING_DATE column to make conversion between your string date and a true date easier.
Google SQL Calender table and you'll find lots of examples and discussions.
Lastly, strongly consider fixing your DB to store dates in a date column.