In a table I have timestamp column. The time in the timestamp looks as shown below.
2015-08-14 18:07:36
To select in format of HH:MM AM/PM I used as shown below
select TIME_FORMAT(timestamp_column, '%h:%i %p') from table_name;
What I need?
If the record inserted today(on present day) it have to retrieve timestamp in format of HH:MM AM/PM. If record is inserted on previous days then retrieve the timestamp as DD/MM/YY.
How can I write such a sql query. Is it possible?
select case when date(timestamp_column) = curdate()
then TIME_FORMAT(timestamp_column, '%h:%i %p')
else DATE_FORMAT(timestamp_column, '%d/%m/%y')
end
from table_name;
Related
In Mysql, we have a function `
curdate()
` which returns today's date.
i.e when execute
select curdate();
it returns date as,
2018-06-22
I want to append the time stamp to this date like
hh:mm:ss
How can I do it? Please help me.
I want to query database with attached timestamp.
something like this,
select * from user where user_created_on >= (curdate() 00:00:00)
timestamp is
00:00:00.
Just concatenate time section:
SELECT * FROM user WHERE user_created_on >= CONCAT(CURDATE(), ' 00:00:00');
select * from user where user_created_on >= getdate()
or
select * from user where user_created_on >= now()
both of these will have that timestamp with hours,mins, and seconds
ps: there is a curtime() that gets the timestamp as well as convert(varchar, curdate(), 108) will too :)
if you want all hours of the day you can use
where data_inicio between (CONCAT(CURDATE(), ' 00:00:00')) and (CONCAT(CURDATE(), ' 23:59:59'));
I'm a bit confused on how to filter records by date formats.
I have date column of date(yyyy-mm-dd) data type in date table.
Ex:
date
-----
2017-01-29
2017-01-30
I'm want to change the format (dd-mm-yyyy). I'm using this code
SELECT
DATE_FORMAT(`date`, '%d-%m-%Y') as date
FROM
dim_date;
date
-----
29-01-2017
30-01-2017
I want to filter the records with the(dd-mm-yyyy) format. So I tried this code.
SELECT
DATE_FORMAT(`date`, '%d-%m-%Y') date
FROM
dim_date
WHERE DATE_FORMAT(`date`, '%d-%m-%Y') BETWEEN '20-04-2015' AND '06-09-2017';
Results
Nothing
But if I try to filter with the original format (yyyy-mm-dd) It Works.
SELECT DATE_FORMAT(`date`, '%d-%m-%Y') date
FROM dim_date
WHERE DATE_FORMAT(`date`, '%Y-%m-%d') BETWEEN '2015-04-20' AND '2017-01-07';
Why is this weird behavior in Mysql? Am I missing something here?
I also tried with this format DATE_FORMAT(date, '%d-%c-%Y') , DATE_FORMAT(date, '%d-%l-%Y') & DATE_FORMAT(date, '%e-%l-%Y')
No happy face, Please let me known.
Thanks
Max
with your query you convert the date in string and then you are comparing values using between in wrong order ('20' > '06' ) so don't work.
Between require first the min value and second the max value.
If you are working with date you sould convert the string date
so the where between work correcly and avoid the string behavior is filter
SELECT date
FROM dim_date
WHERE date BETWEEN str_to_dat('20-04-2015', '%d-%m-%Y')
AND str_to_date('06-09-2017', '%d-%,-%Y');
I have simple problem with SQL query.. I would like to search fields, where dates begin and ends in specific date and time.
Field is defined as CHAR and have structure: DD.MM.YYYY hh:mm:ss and I cannot change it.
I tried to declare some variables and search by this, also tried by Converting.
This is what I tried and didn't work:
SELECT date FROM table WHERE date BETWEEN '1.01.2017 00:00:00' AND '1.02.2017 23:59:59'
SELECT date FROM table WHERE date >= '1.01.2017 00:00:00' AND date <= '1.02.2017 00:00:00'
SELECT date FROM table WHERE date >= Convert(DATETIME, '1.01.2017', 104) AND date <= Convert(DATETIME, '1.02.2017', 104)
Always after this query I get all dates, not this what I asked.
Your field is not in a format that can be compared lexicographically as strings, so you need to convert it to a date. But it's not in the format that MySQL can parse by default (it expects dates in the format YYYY-MM-DD). So use STR_TO_DATE() to parse it with a format string.
SELECT date
FROM table
WHERE STR_TO_DATE(date, '%d.%m.%Y %H:%i:%s') BETWEEN '2017-01-01 00:00' AND '2017-01-02 23:59:59'
I solved my problem. Problem was with that HOW this fields are storage in DB.
I thought that they are storage like: DD.MM.YYYY hh:mm:ss, but it was only structure. In DB they are storage like: YYYYMMDDhhmmss and after changes in WHERE query line it works.
SELECT date FROM table WHERE date >= '20170101000000' AND date <= '20170101235959'
SELECT date FROM table WHERE STR_TO_DATE(date, '%d.%m.%Y %H:%i:%s') BETWEEN '2017-01-01 00:00:00' AND '2017-02-01 23:59:59'
OR
You can use STR_TO_DATE() function.
SELECT date FROM table WHERE STR_TO_DATE(date, '%d.%m.%Y %H:%i:%s') BETWEEN STR_TO_DATE('1.01.2017 00:00:00','%d.%m.%Y %H:%i:%s') AND STR_TO_DATE('1.02.2017 23:59:59','%d.%m.%Y %H:%i:%s')
You can try above query.
I'm looking to create a date time field in a MySQL script that has a specific date and time.
I've tried using
CONCAT(DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 9 DAY) ,'%Y%m%d'), ' 13:00:00')
but it doesn't insert correctly.
How can I achieve this so that it will insert a date time with the time as above?
It inserts the record as 0000-00-00 00:00:00 with the above
mysql accepts datetime values in yyyy-mm-dd hh:mm:ss format. In your formula you do not separate the year, month, day values, hance the result is not in the date format mysql expects the dates in. Change it to:
CONCAT(DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 9 DAY) ,'%Y-%m-%d'), ' 13:00:00')
But I do not really understand why you need to do the formatting, just use CURDATE() function instead of the NOW():
CONCAT(DATE_SUB(CURDATE(), INTERVAL 9 DAY), ' 13:00:00')
I used DATETIME datatype to store date and time in my Database. It saved date as
YY-MM-DD HH:MM:SS (2012-11-06 10:45:48)
I can retrieve the time from database as HH:MM:SS format but I want to retrieve it in HH:MM format.
How can I do this?
Check the DATE_FORMAT Function.
SELECT DATE_FORMAT(date_column, '%H:%i') FROM your_table
You will want to use DATE_FORMAT():
select date_format(yourDateColumn, '%h:%i') yourTime
from yourtable
See SQL Fiddle with Demo
SELECT DATE_FORMAT( NOW() , '%H:%i' ) AS time_column