Convert from yyyymmdd to month year format - sql-server-2008

Count the dateloc per month then convert from yyyymmdd to month year format.
Please see image below.
I am using MS SQL 2008. any help will do

SELECT
UPPER(DATENAME(M,dateloc))+' '+CONVERT(VARCHAR(50),DATEPART(YEAR,dateloc)) as Mounth_Year,
cnt as Total_Count,
amt as Total_Amount
FROM
(SELECT dateloc, count(*) as cnt,sum(amount) as amt FROM datebase.iniatl_table
GROUP BY dateloc)
if amount is money it will be ok. Is string try sum(convert(money,amount))

SELECT
CASE
WHEN MONTH(CONVERT(DATETIME,DATELOC,112)) = 1 THEN 'January'
END + ' ' + CAST(MONTH(CONVERT(DATETIME,DATELOC,112)) AS VARCHAR(100))
FROM [TABLE]

Related

How can I concat month column and year column to yearmonth column

I have an SQL table with columns (month, year, and yearmonth)
Month year
5 2020
6 2020
11 2020
I want to insert yearmonth column values in this format
yearmonth
202005
202006
202011
I've tried couple of Datetime functions, but it didn't work.
I think that it is simpler to use string functions:
select t.*, concat(year, lpad(month, 2, '0')) as yearmonth
from mytable t
lpad() adds the "missing" 0 to the month part if needed, which you can then concatenate with the year.
On the other hand, if you want a numeric result, arithmetics is the way to go:
select t.*, year * 100 + month as yearmonth
from mytable t
If you want a string, I might use:
select cast(year * 100 + month as char)

Quarter Equivalent for MySQL Data Summation

I am using the following mySQL query for summing up "volume" column for each month
SELECT DATE_FORMAT(date, "%m-%Y") AS Month,
SUM(vol)
FROM tbl
GROUP BY DATE_FORMAT(date, "%m-%Y")
order by Month
It produces the right output as expected on monthly basis:
(Above is image capture from excel)
Now, I want to change it to quarterly (i.e. 3-monthly) summation, or six-monthly summation.
What changes are needed to the above query?
I am unable to find any equivalent of %m for quarter or for six-month, hence the question.
If you want this per quarter, you can do:
select year(date) yr, quarter(date) qr, sum(vol) sum_vol
from tbl
group by yr, qr
order by yr, qr
Or if you want to concatenate the year and quarter in a single column:
select concat(year(date), '-Q', quarter(date)) yr_qr, sum(vol) sum_vol
from tbl
group by yr_qr
order by yr_qr
To split by semester, you need a little more work:
select
concat(year(date), '-S', case when month(date) between 1 and 6 then 1 else 2 end) yr_sr,
sum(vol)
from tbl
group by yr_sr
order by yr_sr

Mysql Query Select Statement Help Needed

I have a query of mysql where i fetch how much has the total sale gone ,
but it shows previous dates [Date] => 2014-01-22 [TotalSales] => 7 ,
It shows this way , how can i make it , so that it shows todays date and shows the sale up till now
SELECT DATE(order_time) AS Date, SUM(Quantity) AS TotalSales
FROM ss_orders,ss_ordered_carts
GROUP BY date;
Then add a where clause, comparing your order_time with today's date.
like this:
SELECT DATE(order_time) AS Date, SUM(Quantity) AS TotalSales
FROM ss_orders,ss_ordered_carts
WHERE DATE(order_time) = DATE(NOW())
group by date;

print data per day, week, month, year

I have data in below format.
iPAddress + timeVisited
=========================
varchar + timestamp
What I wanted to fetch is
Report that have total IP per day (system date)
Report that have total IP per week (if today is tue, I need this week and not last 7 days)
Report that have total IP per month (current month... if today is 18 then from 1-18 of this month)
Report that have total IP per year
Any idea how to get this done?
To get all the records for today
select iPAddress from table_name where date(timeVisited) = curdate();
To get all the records of current week
select iPAddress from table_name where week(timeVisited) = week(curdate()) and year(timeVisited) = year(curdate());
To get all the records for current month
select iPAddress from table_name where month(timeVisited) = month(curdate()) and year(timeVisited) = year(curdate());
And to get all the records of current year
select iPAddress from table_name where year(timeVisited) = year(curdate());
Hope it helps.
Below is what I used
Current date
select count(*) from myTable where DATE(myTime) = DATE(NOW());
Current week
select count(*) from myTable where YEAR(myTime) = YEAR(NOW()) AND WEEKOFYEAR(myTime) = WEEKOFYEAR(NOW());
Current month
select count(*) from myTable where YEAR(myTime) = YEAR(NOW()) AND MONTH(myTime) = MONTH(NOW());
Current year
select count(*) from myTable where YEAR(myTime) = YEAR(NOW());
Demo at sqlfiddle
Note:
YEAR(myTime) = YEAR(NOW()) in current week and current month plays very important role.
Think of date as 2012-10-30 and 2013-10-30.

MySQL select rows with date like

In MySQL I have this query
SELECT DISTINCT date, descr FROM book ORDER BY date
Date is in format yyyy-mm-dd
I want to select only the the books from January 2012. I have tried to use like but that does not work.
Any ideas?
Using DATE_FORMAT function
SELECT DISTINCT date, descr FROM book
WHERE DATE_FORMAT(date, '%Y %m') = DATE_FORMAT('2012-01-01', '%Y %m')
ORDER BY date
Or using MONTH and YEAR functions
SELECT DISTINCT date, descr FROM book
WHERE Month(date) = Month('2012-01-01')
AND Year(date) = Year('2012-01-01')
ORDER BY date;
Or using BETWEEN functions
SELECT DISTINCT date, descr FROM book
WHERE date BETWEEN '2012-01-01'
AND '2012-01-31'
ORDER BY date;
Or using <= and >= operators
SELECT DISTINCT date, descr FROM book
WHERE date >= '2012-01-01'
AND date <= '2012-01-31'
ORDER BY date;
See this SQLFiddle
You can use >= and <= operators here. Check the below code:
SELECT *
FROM book
WHERE date >= '2012-01-01' AND date <= '2012-01-31'
Try this:
SELECT DISTINCT date, descr FROM book WHERE YEAR(date) = '2012' and MONTH(date) = '1'
This works if your "date"-column is a MySQL date field.
If you are adamant that you want to use the LIKE syntax, you can convert the date to CHAR first:
SELECT DISTINCT date, descr FROM book WHERE CAST(date AS char) LIKE '2012-01%' ORDER BY date;
Using like also works. With #hims056 fiddle, you can test it:
SELECT DISTINCT ID, date FROM book
WHERE date LIKE '2012-01%'
ORDER BY date;
However, it's not usual to use a like for date filtering, for me it's more natural to use >= and <= , or between. Also, there's a performance benefit.
SELECT DISTINCT date, descr
FROM book
WHERE YEAR = DATE(NOW()) AND MONTH(date) = '1'
This will give you this years books