Create Dynamic Date in MySQL - mysql

I am trying to create a dynamic date in mysql. I have written it for mssql but having troubles with mysql.
createdate(getyear(currentdate()) ,10,01)
I have tried STR_TO_Date() but i can't make the year dynamic. Any thoughts ?

If you're looking for a MySQL-only solution to getting the current year,
SELECT EXTRACT(year FROM NOW());
should do what you want.
Or for the whole date,
SELECT CONCAT(YEAR(NOW()),'-10-01');

That worked:
SELECT STR_TO_DATE(CONCAT(10,31,EXTRACT(YEAR FROM CURDATE())), '%m%d%Y') ;

Related

How to subtract two sql timestamp fields then return time difference in hours

I'm using open source db, metabase, and I tried datediff() function and it returns in day (datediff(d1,d2)),
but I want it returns in hours:
select d1,d2,d1-d2
and it returns
'2020/01/30 T00:00:00.000Z' '2020/01/28 T19:17:39.000Z' 25,158,264
how could I do to change it in hours? Thanks in advance.
btw timestampdiff does not work in my db version, so I might need another solution
According to the manual for TIMESTAMPDIFF
SELECT d1,d2,TIMESTAMPDIFF(HOUR, d1, d2) FROM table
SELECT
TIMESTAMPDIFF(HOUR,d1,d2)
AS 'Difference in Hours';
I take it that you are using MySQL.
This is the syntax:
select TIMEDIFF('2020/01/30 T00:00:00.000Z','2020/01/28 T19:17:39.000Z')

How to create a date in mysql

I need to create a date in a mysql query - but just the year part, I have tried:
SELECT STR_TO_DATE('01,5,'year(now()),'%d,%m,%Y');
But that does not work.
All I need to do is change the year part - any ideas how to do this?
The STR_TO_DATE function does not accept two strings as its first argument, you need to concatenate the day and month with your year expression, using CONCAT.
Try this (SQLFiddle =):
SELECT STR_TO_DATE(CONCAT('01,5,',YEAR(NOW())),'%d,%m,%Y');
If you know the two other parts just use CONCAT()
SELECT CONCAT(YEAR(CURDATE()), '-05-01')
Fiddle

How to display a day on a given date using an SQL SELECT query in MySql?

I'm not much familiar with MySql, since much of the times, I had been working with Oracle. In Oracle, a day [Starting from Monday to Sunday] on a specified date can simply be displayed using the following SQL SELECT query.
SELECT TO_CHAR(TO_DATE('9-AUG-1988','DD-MON-YYYY'),'DAY')FROM DUAL;
It shows the day [It can be any starting from Monday to Sunday] on the specified date in the specified format in Oracle. I took some Gooling but I couldn't. How is the same thing achieved in MySql?
You can try this:
SELECT DAYNAME(your_date) FROM your_table
USING DAYNAME() FUNCTION, like:
mysql> SELECT DAYNAME('2007-02-03');
-> 'Saturday'
SELECT DAY(DATE_COLUMN_NAME) FROM TABLE_NAME;

help with date query in access

i have field Tdate (text type) on my Table MyTbl
i need query that sort by date, i try this:
select * from MyTbl order by Tdate
but because Tdate is Text i get wrong results
how to fix it ? is there any convert to date in access ?
thanks in advance
You can try using the CDate function like this:
select * from MyTbl order by CDate(Tdate)
Watch out for potential problems with the date format, e.g. "01/02/2011" could mean 1st Feb or 2nd Jan, the CDate function will use the locale settings on your system.
I'd run a query to update your text field to use a non-ambiguous, sortable format that works in text, e.g., ISO format, YYYY-MM-DD. A better long-term solution is to change the data type of the field so it's a date so you wouldn't have to muck about with these kinds of problems.

MySQL - Convert MM/DD/YY to Unix timestamp

Is there an easy (single query) way to do this?
I'm reading those values from a column in a table and I think that the column itself is defined as a string (can't be helped, i'm afraid).
Use UNIX_TIMESTAMP;
SELECT UNIX_TIMESTAMP('2007-11-30 10:30:19');
Update:
SELECT UNIX_TIMESTAMP(CAST(fieldName AS DATE));
SELECT UNIX_TIMESTAMP(STR_TO_DATE('08/05/10','%m/%d/%y'));
SELECT '12/31/10',
STR_TO_DATE('12/31/10', '%m/%d/%y'),
UNIX_TIMESTAMP(STR_TO_DATE('12/31/10', '%m/%d/%y'))
Both functions are covered here: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html