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
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
I want to select all pat_id and its pat_date_hired in the patient table, and then insert it in the appointment table.
pat_id is the same value as pat_id in the appointment table, and pat_date_hired will be the value of app_date, but the year must be changed to the current year.
For example: If pat_date_hired = 10-26-2014, then app_date must be 10-26-2016.
Here is the idea:
insert into appointment (pat_id, app_date, app_type)
values
(*select the pat_id and pat_date_hired and change the year to current year*, "PE")
I'm new to MySQL. I have very limited knowledge. Any help will be appreciated.
try using DATE_FORMAT to change the year
example as below
insert into appoinment (pat_id, app_date, app_type) value(
SELECT pat_id FROM patient_table where pat_id = appointment_table.pat_id
,
DATE_FORMAT(SELECT pat_date_hired FROM patient_table where pat_id = appointment_table_pat_id, YEAR(current_DATE)+'/%m/%d'))
This is the logic I would use in SQL Server (you would need to find the replace for the logic of generating app_date. Similar thing should be available in MySql):
INSERT INTO appointment (pat_id, app_date, app_type)
SELECT
pat_id
,DATEADD(yy, YEAR(GETDATE()) - YEAR(pat_date_hired), pat_date)
, <value for app_type or app_type column>
FROM patient_table
In first place, I would give you advice on the date format you are using; month-day-year won´t be recognized by mysql; you would be better using Y-m-d
Said that; 1st step, use STR_TO_DATE(date_string, format) to convert your date to a valid mysql date; 2nd step, use YEAR(NOW()) to get current year; 3rd step: concat them. Your query would look like the following:
insert into appointment (pat_id, app_date, app_type)
select pat_id,
CONCAT(
YEAR(NOW()),
"-",
DATE_FORMAT(STR_TO_DATE("10-26-2016","%m-%d-%Y"), "%m-%d")
),
"PE")
Note that, I made the query so it would return "Y-m-d" date format, if you wanted it to be "m-d-Y" change this
CONCAT(
YEAR(NOW()),
"-",
DATE_FORMAT(STR_TO_DATE("10-26-2016","%m-%d-%Y"), "%m-%d")
)
to this
CONCAT(
DATE_FORMAT(STR_TO_DATE("10-26-2016","%m-%d-%Y"), "%m-%d"),
"-",
YEAR(NOW()),
)
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 made a SQL Statement and I want to use the date but without the time.
My Select is:
SELECT DATEPART(dw, [Order].PerformDate)
And my Group by is:
GROUP BY [Order].PerformDate
Now how can I ignore the time?
You can use CONVERT function of SQL
select datepart(dw, CONVERT(DATE,[Order].PerformDate))
GROUP BY CONVERT(DATE,[Order].PerformDate)
Cast datetime value to date:
select cast(`Order`.PerformDate as date) as PerformDate
GROUP BY says "I want one result row per ____". In your case one row per PerformDate. If PerformDate is a datetime, but you only want to have one result row per date without time, then you must extract the date part:
group by cast(performdate as date)
You also want to display the weekday with datepart(dw, performdate) but this is no longer possible, because PerformDate is no longer available. Only its date part is. So:
select datepart(dw, cast(performdate as date))
from ...
group by cast(performdate as date);
Another one method:
select date_format(`order`.PerformDate, '%Y%m%d')
...
group by 1
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' )