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()),
)
Related
I want to make a query where I get people birthday's in a current month, I have problems with MONTH() it does not work for me, the date format in the database is: 04/18/1990, and I want to compare if the month current date('m') is equal to the month of the database.
Any suggestions?
you can try this one
SELECT * FROM table_name where extract(month from BirthDate) = date("m");
where date("m") return current month and BirthDate is column name in which you have stored birthdate.
Try this
"select month(str_to_date(birthdate), '%Y/%m/%d')
from users
where month(str_to_date(birthdate), '%Y/%m/%d') = '$month'"
Here, i m assuming your delimeter is '/', Please set your delimeter according to your string date i.e 2013/05/04 or 2013,05,04 or 2013-05-04
could be you have some convertion issue try using
select month(str_to_date(my_col, '%m/%d/%Y'))
from my_table
where month(str_to_date(my_col, '%m/%d/%Y')) = month(now())
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
table - fu_plan_user (id_plan_user,id_user, id_plan, valid_from, valid_to, ...)
table - fu_user (id_user, name, company....)
table - fu_plan (id_plan, name, duration, ...)
This is the table structure somewhat.
I want to produce a list of accounts that have a close(valid_to) date which is the end of a month(30th day) and this date should be in the future, like grater than todays date (NOW()). The name of the company should not contain "trial" word.
The result should contain the following fields
id_user, name (from table fu_plan), company (from table fu_user), valid_to (from table fu_plan_user)
Something like this
Raw Query (not correct)
SELECT usr.id_user, payplan.name, usr.foretag, planUser.da_valid_to
from fu_plan_user planUser, fu_user usr, fu_plan payplan left join
fu_user usr
on planUser.id_user=usr.id_user
where planUser.da_valid_to > now() and
planUser.da_valid_to >= DATEADD(d, -30, getdate()) ;
After your explanation and only if your database is MySQL, i suggest to use this query:
SELECT user.id_user, plan.name, user.company, pbyu.valid_to
FROM fu_plan_user AS fbyu
JOIN fu_user AS user
ON user.id_user = fbyu.id_user
JOIN fu_plan AS plan
ON plan.id_plan = fbyu.id_plan
WHERE pbyu.valid_to > NOW()
AND LAST_DAY(pbyu.valid_to) = pbyu.valid_to
AND user.company NOT LIKE '%trial%';
If company isn't only lowercase values, you should use LOWER().
LAST_DAY() function will get the last day from month of valid_to date.
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 have a table which contain records of people according to month and year.
table have multiple columns in which there are two columns which are fldmonth & fldyear, which contain month no and year respectively.
Now I want to fetch data between months of different year. (E.g. 3-2012 to 6-2013)
I am using following query, but not getting proper record.
SELECT * FROM 'table' WHERE
user_id = 'id' AND
STR_TO_DATE(CONCAT('fldyear', 'fldmonth', '01'), '%Y%m%d') BETWEEN
STR_TO_DATE(CONCAT('2012', '3', '01'), '%Y%m%d') AND
STR_TO_DATE(CONCAT('2013', '6','01'), '%Y%m%d');
Table Schema :
user_id varchar(100), fldmonth smallint(2), fldyear mediumint(4)
(table name & userid given here are just for example)
Please need help.
Note: I used %c also in date format because month are in 1,2,..12 format. But still am getting empty result set
SELECT * FROM tbl
WHERE USERID=1 and
STR_TO_DATE(CONCAT(fldyear,'-',LPAD(fldmonth,2,'00'),'-',LPAD(fldate,2,'00')), '%Y-%m-%d')
BETWEEN
STR_TO_DATE(CONCAT(2012,'-',LPAD(03,2,'00'),'-',LPAD(01,2,'00')), '%Y-%m-%d') AND
STR_TO_DATE(CONCAT(2013,'-',LPAD(06,2,'00'),'-',LPAD(01,2,'00')), '%Y-%m-%d');
Working Fiddle
Remove Single quote from column names.
Try this:
SELECT *
FROM table
WHERE user_id = 'id' AND
STR_TO_DATE(CONCAT(fldyear, fldmonth, '01'), '%Y%c%d') BETWEEN '2012-03-01' AND '2013-06-01';
Saharsh Shah is right, but what you need is to add brackets () too in your condition
change the condition with
WHERE user_id = 'id' AND
( STR_TO_DATE(CONCAT(fldyear, fldmonth, '01'), '%Y%m%d')
BETWEEN '2012-03-01' AND '2013-06-30' );