I have date and time format as
2011-12-31 05:12:23
And i need to change it as follows:
Monday # 2:23AM 21/11/2011
I am using this sql query:
SELECT * FROM cms_content order by ID DESC;
where i have the fields as follows in my table named cms_content:
id, idname, author, date, title, body
So how to do this so that i can get the desired result?
Try this:
SELECT DATE_FORMAT(`date`, '%W # %l:%i%p %d/%m/%Y') FROM cms_content
Read further here:
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
[edit : additional]
I noticed a difference between your sample and expected data output so here's a literal answer to your date format inquiry:
SELECT DATE_FORMAT( DATE_ADD('2011-12-31 05:12:23', INTERVAL '-40 2:49' DAY_MINUTE ) , '%W # %l:%i%p %d/%m/%Y' )
Related
I have a problem. I have a table like this (example data):
value
timestamp
22.12
2023-01-18T08:00:35.000Z
22.18
2023-01-18T09:13:12.000Z
22.15
2023-01-18T09:16:12.000Z
22.17
2023-01-18T09:49:35.000Z
16.12
2023-01-25T10:15:05.000Z
26.18
2023-01-25T10:40:05.000Z
25.52
2023-01-25T10:55:05.000Z
19.88
2023-01-26T11:40:05.000Z
16.12
2023-01-16T12:40:05.000Z
I'am getting an average of values and I'am grouping it by date. I use:
select cast(timestamp as date) as dt, AVG(value) as avg_val
from tbl_name
group by cast(timestamp as date);
And my result looks like this:
22.01384610396165 2023-01-18T23:00:00.000Z
Is it possible to get only data without time and timezone?
Yes, it is possible to get only the date (without time or timezone) from the timestamp. You can use the DATE() function in your query to convert the timestamp to a date. The syntax would be:
SELECT DATE(timestamp) AS dt
, AVG(value) AS avg_val
FROM tbl_name
GROUP BY DATE(timestamp);
This will return the result as:
22.01384610396165 2023-01-18
My data value like below
date_time
----------------
24:Sep:2016 22:19:35
22:Oct:2016 22:19:35
26:Sep:2016 22:19:35
28:Sep:2016 22:19:35
Here I am using a query to check data where matched by month like below.
SELECT * FROM `audit_log` WHERE MONTH(STR_TO_DATE(date_time, '%d:%M:%Y %H:%i:%s')) ='Oct'
But this not working for me. But for year if I use that working
SELECT * FROM `audit_log` WHERE YEAR(STR_TO_DATE(date_time, '%d:%M:%Y %H:%i:%s')) ='2016'
I got my answer.
I used long form month to search and in query used MONTHNAME
SELECT * FROM `audit_log` WHERE MONTHNAME(STR_TO_DATE(date_time, '%d:%M:%Y %H:%i:%s')) ='October'
in different process I can use MONTH though it's return number. I have to use month number to search in query. like bwlow
SELECT * FROM `audit_log` WHERE MONTH(STR_TO_DATE(date_time, '%d:%M:%Y %H:%i:%s')) ='10'
I have a column name Endtime of type nvarchar(50) and i am trying to get the max(Endtime) by using the below query:
select COALESCE(CONVERT(nVARCHAR(50), MAX(EndTime), 103), '-') AS EndTime from dbo.vwJobHistory
The results are fine as long as Year is same (12/31/2015) but as soon as i put some data with year (01/22/2016) , the query still shows the Max. Date of 2015 instead of 2016.
I have tried Max(cast(endtime as DateTime)) but this also gives conversion error
How can I resolve this? Thanks in advance.
I can't find an error in your above query, but here's an alternative way how you could write the query:
select top 1 COALESCE(CONVERT(nVARCHAR(50), EndTime, 103), '-') AS EndTime
from dbo.vwJobHistory
order by endtime desc
Update 1 (clarifying table structure):
It is currently unclear if your datetime data is currently stored inside a nvarchar(50) field or if you want to cast your datetime data as nvarchar(50).
Could you run the following query and update your question with the result?
SELECT
COLUMN_NAME, DATA_TYPE, col.CHARACTER_MAXIMUM_LENGTH
FROM INFORMATION_SCHEMA.COLUMNS col WHERE TABLE_NAME LIKE 'vwJobHistory'
Could you also post a sample row from your table using
SELECT endtime FROM dbo.vwJobHistory
Update 2 (convert string to datetime, apply sort on converted field)
Taken from your comment, it seems your date strings are stored with SQL format 109, so let's try to convert the nvarchar back to datetime, apply sort to it and output the result:
SELECT * FROM (
SELECT
convert(datetime, EndTime, 109) endtimeconverted,
*
FROM vwJobHistory
) xyz
ORDER BY endtimeconverted DESC
I am using JasperReports/iReport to do my report. I have wrote a query for the client to key in the date,but there is 1 issue, the date is come along with the time portion.
How can I ignore the time portion for the client to key in the data?
date rate
2/2/2014 12:56:21.0000 PM ABC
4/2/2014 12:56:21.0000 PM EFG
5/2/2014 12:56:21.0000 PM HIJ
...
I have assign from customer to design with the parameter of year,month, and day.In other words, when the customer key in the year,month,and day,
the data will show this way:
2/2/2014 12:56:21.0000 PM ABC
The query i have wrote is show below:
select date,rate
from mytable
where rownum=1
and tran_dt<=(select date
from mytable
where year(date)=$P{year}
and month(date)=$P{month}
and day(date)=$P{day}
)
order by
date
I'm using pl/sql query...
SQL SERVER :
Use This
select convert(varchar(10), '2011-02-25 21:17:33.933', 120)
Like This
Select DATE_FORMAT(date,'%y-%m-%d'),rate from mytable
where rownum=1 and tran_dt<=(select tran_dt from bi01_trandetail where
year(date)=$P{year} and month(tran_dt)=$P{month} and day(tran_dt)=$P{day})
order by date
MYSQL
USE
DATE_FORMAT(date,'%y-%m-%d')
Select DATE_FORMAT(date,'%y-%m-%d'),rate from mytable
where rownum=1 and tran_dt<=(select tran_dt from bi01_trandetail where
year(date)=$P{year} and month(tran_dt)=$P{month} and day(tran_dt)=$P{day})
order by date
OR
SELECT DATE('2003-12-31 01:02:03');
'2003-12-31'
Select date(date),rate from mytable
where rownum=1 and tran_dt<=(select tran_dt from bi01_trandetail where
year(date)=$P{year} and month(tran_dt)=$P{month} and day(tran_dt)=$P{day})
order by date
Your New Query:
select date,rate
from mytable
where rownum=1
and tran_dt<=(select DATE_FORMAT(date,'%y-%m-%d')
from mytable
where year(date)=$P{year}
and month(date)=$P{month}
and day(date)=$P{day}
)
order by
date
MYSQL DATE FUNCTION
You have to ways, from the DDBB...
With PL/SQL check the TO_CHAR doc, http://www.techonthenet.com/oracle/functions/to_char.php, (and this SO question, ;D: How to format a Date variable in PLSQL).
Your query will be:
SELECT TO_CHAR(date, 'MM/DD/YYYY'), rate from mytable ...
With MySQL (NOTE: I leave the MySQL answer because I answered first with it because it wasn't specified RMDB)
MySQL DATE_FORMAT function, getting only what you want:
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
from the web as example:
SELECT DATE_FORMAT('2009-10-04 22:23:00', '%W %M %Y');
-> 'Sunday October 2009'
Testing the case you proposed:
SELECT DATE_FORMAT('2014-03-02 23:45:35', '%e/%c/%y %H:%i:%s %p');
-> 2/3/14 23:45:35 PM
With no time portion:
SELECT DATE_FORMAT('2014-03-02 23:45:35', '%e/%c/%y');
-> 2/3/14
Your query will be:
select DATE_FORMAT(date, '%e/%c/%y') ,rate from mytable ...
... and the other way is with the date functions from your server scripts, but that is another history and I'd need what language you use, XD
Thank you all for the reply, i have found the way to solved this question.
Let i share the query,
select rate,trunc(date) as EFFDT from mytable
Where rownum=1
and
date <= to_date(:DAY||'/'||:MTH||'/'||:YR, 'DD/MM/YYYY')
order by date desc
I need a mysql query to extract the year from the following date format from a table in my database.
For eg :
subdateshow
----------------
01/17/2009
01/17/2009
01/17/2009
01/17/2009
01/17/2009
the following query didn't working
select YEAR ( subdateshow ) from table
The column type is varchar. Is there any way to solve this?
Since your subdateshow is a VARCHAR column instead of the proper DATE, TIMESTAMP or DATETIME column you have to convert the string to date before you can use YEAR on it:
SELECT YEAR(STR_TO_DATE(subdateshow, "%m/%d/%Y")) from table
See http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date
SELECT EXTRACT(YEAR FROM subdateshow) FROM tbl_name;
You can try this:
SELECT EXTRACT(YEAR FROM field) FROM table WHERE id=1
This should work:
SELECT YEAR(STR_TO_DATE(subdateshow, '%m/%d/%Y')) FROM table;
eg:
SELECT YEAR(STR_TO_DATE('01/17/2009', '%m/%d/%Y')); /* shows "2009" */
try this code:
SELECT DATE_FORMAT(FROM_UNIXTIME(field), '%Y') FROM table
This should work if the date format is consistent:
select SUBSTRING_INDEX( subdateshow,"/",-1 ) from table
TRY:
SELECT EXTRACT(YEAR FROM (STR_TO_DATE(subdateshow, '%d/%m/%Y')));
try this code:
SELECT YEAR( str_to_date( subdateshow, '%m/%d/%Y' ) ) AS Mydate