Displays the date in MySQL Workbench - mysql

How to change the display of dates in MySQL Workbench
Recorded as 16-01-2014 now
But is necessary to do so: 16-01-2014

You'll need to change your SELECT statements to get your dates rendered differently.
Change
SELECT datestamp
to
SELECT DATE_FORMAT(datestamp,'%d-%m-%Y')
MySQL Workbench is a client program that accesses MySQL database servers. You use the workbench to write queries to send to a MySQL server to get back data.
If you want your dates presented in a particular way, that's part of the queries you write. If you use the queries built in to the workbench, you can't change the date format it displays.
Don't give in to the temptation to change the data type of your date and time columns in your table to VARCHAR(), and then fill them with values in the format you prefer. If you do that, you'll lose the ability to search your table on date ranges.

Related

Django querying against external database with different timezone

I have a Django API application running on postgres with TIME_ZONE='America/New_York' and USE_TZ = True.
For a daily report, I need to query another database, MySQL, and compare records from the postgres DB to check for some updates. They should contain the same number of results. The MySQL DB's timezone is UTC however. How can I perform SELECT queries against the MySQL DB to have it match the same date range on my postgres DB?
Example:
These two queries should return the same number of results
# Django/Postgres with TIME_ZONE='America/New_York'`
MyObject.objects.filter(created_on__date=date(2016, 9, 17))
# External MySQL Databse in UTC
sql.execute('SELECT * from MY_TABLE where created_on BETWEEN "2016-09-17" AND "2016-09-18"')
What you need is to convert the date in MySQL to match America/New_York time zone.
The function to achieve that would be CONVERT_TZ() and since you need named time zone first you need to set up time zone tables.
If you are uncertain whether named time zones are available issue below query - if it returns zero the table is empty so using named time zones is unavailable and you need to populate them (I've mentioned a link to documentation above).
SELECT COUNT(*) FROM mysql.time_zone_name;
mysql_tzinfo_to_sql is used to populate time zones tables.
If your time zone is not a moving one then you can go with less safe, hardcored approach (not recommended) by explicitly typing the time difference like so:
mysql> SELECT CONVERT_TZ('2016-09-17 10:00:00','+00:00','+06:00');
-> '2016-09-17 16:00:00' -- Result

Convert timezone for all MySQL queries for a specific user

I work on an existing project that stores dates to a MySQL DB. The dates are stored as UTC since all users so far were in GTM+0 and no conversion was needed.
I now need to modify the code so that users from other time zones can use the system. The users choose their timezone when they register to the system, so I have a table holding the timezone for each user.
I know I can use CONVERT_TZ() when I extract and store the dates, but to do so means to go through all the queries and add this function.
When I do:
SET ##session.time_zone:='+7:00';
select now();
The result changes with the timezone variable.
When I do:
SET ##session.time_zone:='+7:00';
select myDate from myTable;
The result stays the same, returning what is stored in the DB.
Is there any way I can change the connection string or is there a session variable I can use that will affect the queries without having to add CONVERT_TZ to every single query?
Edit: this is not a duplicate of Should I use field 'datetime' or 'timestamp'? since using timestamp means I need to change all the date field in the DB, while I try to change something more global so I will not have to do massive changes the Db fields or the code.

phpMyAdmin auto insert and display only date

I've got a table with a date-type field
browser transform: text/plain: dateformat
transform option: 0,'%d-%b-%Y','local'
When I execute my query it stores 01-Jan-1970 (default value) and on page it shows me 0000-00-00
What I want to do is to store in database and in page only the date and dateformat Y-m-d like 27.02.2016.
You've got a couple of things going on that I should address first.
The phpMyAdmin transform feature affects how you insert or view data from within phpMyAdmin only. It doesn't change how the data is stored internally with MySQL and it doesn't change how other applications interact with MySQL. So when you talk about displaying in your blog or storing in MySQL, those aren't affected by the transformations you've configured.
Next, you don't appear to be setting the post date, which means you're probably getting '0000-00-00 00:00:00' stored in the column. The exception would be if you allow NULL or set a default value. You can also get zeroes if you insert invalid dates.
The appropriate thing to is use the MySQL type and format the display on output -- either in SQL or in your application; I usually do it in my application. How to do that will depend on which programming language your application uses.
When inserting, you can use NOW() to insert the current time without having to compute it yourself.

How to get last access date for a SQL Server database?

I have a development server that is getting crowded.
I would like to see what date the databases have been accessed to determine what ones can be deleted. Is there a way to do this?
The only thing I found when searching was for postgredb:
How to get last access/modification date of a PostgreSQL database?
If you have a table that always gets values inserted you can add a trigger to the update/insert. Inside this trigger you can set the current timestamp in a dedicated database, including the name of the database from which the insert took place.
This way the only requirement of your database is that it supports triggers.

Problem with Date field after migration from mysql to oracle

I have lately migrate my JIRA database from mysql to oracle,
my problem is the field "created" exists in the jiraissue and changegroup tables,on this field I effectuate many calculation but I was surprised by the difference of the format of the fields.
in mysql database the field creation has the type timeStamp so it has the follwing format:
and in Oracle database it has the type date and the format like the following:
How can I resolve this problem?
The format of your displayed Oracle DATE column is due to your IDE that you are viewing it through.
Oracle stores all portions of a date, to display the full date stored use this:
SELECT TO_CHAR(created, 'DD-MON-YYYY HH24:MI:SS')
FROM jiraissue;
This will show you the full date that has been stored including the time portion. To store timestamps you need the column to be designated as a timestamp datatype column.
Ollie.
EDIT: You could change the NLS Date Format of your IDE to always show the full date format in it's settings somewhere.
Dates are not held as formatted text in Oracle. What you are seeing is a tool's (Toad's?) default formatting of the date to display it to you. This can be changed via a preference somewhere. It could be that the time component has been lost in migration, but that is unlikely. Try running this SQL to see:
select to_char (created, 'YYYY-MM-DD HH24:MI:SS') from jiraissue;
That should show the dates just as they appeared in MySQL.