Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a TDate column for date. I would like to group my data by month and year so in my select query, I have columns TMonth and TYear (Select DATEPART(mm, TDate) as TMonth, DATEPART(yyyy, TDate) As TYear from MyTable). Both are now integers. Now, I would like to combine it in one column using a select query with mm/dd/yyyy format. dd should be the last day of TMonth.
How can it be done using a select sql query? Please help. Thank you in advance. :)
"Modern" SQL Server versions, since 2012, have a datefromparts function you could use to create a date:
SELECT DATEFROMPARTS(TYear, TMonth, 1)
FROM mytable
Got it by using
SELECT DATEPART(mm, TDate) as TMonth, DATEPART(yyyy, TDate) As TYear ,
DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, TDate) + 1, 0)) From MyTable
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a daily stock transaction table
T1(symbol, transDate, closingPrice, PrevQtrChange).
The last column is empty. I need an update statement that, for a given symbol, will get the closing price from the previous quarters transaction. Because of weekends, holidays, etc, i can't do a self join on the date being date-90 days. I could do it with a cursor, but ugh. And, the table contains millions of rows, so a cursor would be extremely slow, even with an index.
I'm a C/C++ programmer so while I know some SQL, doing this efficiently is something I'm unsure of.
Thanks in advance.
You can use window functions. The idea for the previous price is:
select t.*,
last_value(closingPrice) over
(partition by symbol
order by transDate
range between unbounded preceding and interval 90 day preceding
) as prev_quarterprice
from t;
You can then incorporate this into an update:
update t join
(select t.*,
last_value(closingPrice) over
(partition by symbol
order by transDate
range between unbounded preceding and interval 90 day preceding
) as prev_quarterprice
from t
) tt
on tt.symbol = t.symbol and tt.transDate = t.transDate
set t.PrevQtrChange = closingprice - tt.prev_quarterprice
where tt.PrevQtrChange is null ;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm working on a homework assignment, and all has been well until I got to this point. My professor wants me to pull only dates in MARCH, APRIL, and MAY, without using the BETWEEN operator.
NOTE: I'm not getting any errors. I am using EDUPE, which runs MySQL, but has small variances where some things simply won't work.
Question was, is there a way to make the code I have function properly? Or am I going in the wrong direction?
/*Exercise Six*/
SELECT order_id as "Order ID", DATE_FORMAT(order_date, '%M-%d-%Y') as "Order Date"
FROM orders
WHERE order_date IN ('March%', 'April%', 'May%')
ORDER BY order_date ASC;
You can try with date_format again:
WHERE DATE_FORMAT(order_date, '%M') IN ('March', 'April', 'May')
Or just monthname():
WHERE MONTHNAME(order_date) IN ('March', 'April', 'May')
I'm not sure if this is the most efficient way, but you can do this with "union":
select order_ID, order_date from orders
where order_date Like '%Mar%'
union
select order_ID, order_date from orders
where order_date Like '%Apr%'
union
select order_ID, order_date from orders
where order_date Like '%May%'
EDIT: I prefer Otashin's answer.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a column which stores datetime like 2011-01-01 01:01:01 .
I need to get list of date:
2011-01-01
2011-02-01
Is there any way I can list down date from datetime in a table?
If the column is really a datetime, just use the date() function:
select date(column)
from table t;
Actually, this also works if the column is a string, assuming it is in YYYY-MM-DD format.
If you want a unique list of dates:
select distinct date(column)
from table t;
select substr(field,1,10)
For just a list of the dates.
SELECT DATE_FORMAT(field, '%Y-%m-%d')
SQL
select to_char(date_column,'YYYY-MM-DD') from table;
MySql
SELECT STR_TO_DATE(date_column,'%Y-%m-%d') from table;
You can use DATE_FORMAT().
Syntax:
SELECT DATE_FORMAT('2011-10-10 19:46:00', '%M %d, %Y');
Try this code
SELECT CONVERT(date,Column_Name) As Date FROM Table_Name
thank you for all answers.
what i really need is year. sorry for the misleading question.
the correct query for that is:
select distinct YEAR(created_on) as years from contacts
thank you
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to be able to find all employees whose review is due in the next 45 days. I have the MS SQL:
Select *
from Employee
where DATEDIFF(yy,HireDate,GetDate()+45) > DATEDIFF(yy,HireDate,GetDate())
How does this convert to MySQL?
try:
Select *
from Employee
where DATEDIFF(HireDate,DATE_ADD(CURDATE(), INTERVAL 45 DAY)) > DATEDIFF(HireDate,CURDATE())
I think you can just use CurDate instead of GetDate, so it'd just be:
Select * from Employee where DATEDIFF(HireDate,CurDate()) > DATEDIFF(HireDate,CurDate())
You should only need to check DATEDIFF once. Just get the number of days and compare.
SELECT *
FROM Employee
WHERE DATEDIFF(CURDATE(), HireDate) <= 45
Docs for MySQL's DATEDIFF (and other date/time functions): http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In my database in table_a every row has a date_created like "2011-04-17"
Now some of these dates are in the past, but my question is how can I retrieve the latest date that has not yet passed?
Try this one
SELECT * FROM table_a WHERE CURDATE() <= date_created
CURDATE()
Returns the current date as a value in 'YYYY-MM-DD' or YYYYMMDD
format, depending on whether the function is used in a string or
numeric context.
Can you try this,
SELECT * FROM table_a WHERE date_created >= CURDATE()
IF date_created is a date datatype then you can use
SELECT *
FROM table_a
WHERE date_created >= NOW()
LIMIT 1