How to create a date in mysql - 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

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')

MySQL how to get Month and Year with separation like date format

I have "datetime" field on my database,
but what I want is only take YEAR-MONTH e.g ("2012-02")
I've tried using function YEAR_MONTH but the output without "-" which is similar like "201202".
Is there any way I could make that select?
You can use the date_format function to specify how to format your columns:
SELECT DATE_FORMAT(my_datetime_field, '%Y-%m')
FROM my_table
You want to give False in second Param in Select
select("DATE_FORMAT(now(), '%Y-%m') AS dated_now", FALSE);
Try this:
SELECT DATE_FORMAT(now(),'%Y-%m');
Try this
SELECT DATE_FORMAT(now(),'%Y %m');
You can also check the below link
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

Converting date field to return only Day field

I am working for a POS company, i have a query in my program that gets date from multiple tables and one of the fields it returns is a date field that returns the the date in the format yyyy-mm-dd. Is there a way to get my query to bring this date field in the form 'dd'? if yes how?
You are looking for the date_format() function (see here):
select date_format(<datefield>, '%d');
This returns the value as a string. If you want it as a number, just use day():
select day(<datefield>)
You are looking for this:-
EXTRACT(unit FROM date)
or try this:-
SELECT DAY(your_date_field) AS dtDay FROM your_table
select DATE_FORMAT(your_date_column,' %d ') FROM your_table
As you mentioned Delphi, why not a Delphi solution:
open the query in a dataset
get the corresponding field
get the value with AsDateTime
use function DayOf from System.DateUtils to retreive the day
in case you need a dataset field for that, create a calulated field
move the day calculation into the OnCalcFields event of the dataset
There might still be plenty of other ways to do it.

How to check date and time individually in Mysql?

In my SQL table have column like startDate - DATETIME(data type) And it gives format like
`2013-08-08 06:30:00`
And i have two fields dateonly - 2013-08-08(value) timeonly - 06:30:00(value).
Now i want to query to check these two fields values with database value.
Hear two comparisons first check dateonly field is greater then or not and next check timeonly field is greater then or not.
is this possible in Mysql?
please help me in writing query
for date you can use this
WHERE DATE_FORMAT(datecolumn,'%Y-%m-%d') > 'some date here'
for time
WHERE DATE_FORMAT(datetimecolumn,%T) < 'sometime'
or use functions date(datetimecolumn) , time(datetimecolumn)
try this
select * from Demo d where DATE(d.startDate)< 'dateonly'
or
select * from Demo d where DATE_FORMAT(d.startDate ,'%Y-%m-%d')< 'dateonly'
I think you want this two functions:
Date(startDate) and Time(startDate) which can be found here: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

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;