Convert Date in SQL - sql

Select ReceiveDate,SentDate
From Customers
ReceiveDate SentDate
2012-11-29 19:17:00 2007-08-28 10:48:00
I want to change such format is : Month Day, Year (Example : Nov 29, 2012)
How can I do?
Thanks

I don't think there is a certain mysql function that does that.
But if you want to show the date on a site with the format (jan 24, 1998) then you can use php.
You make three queries one to take the day, one for the month and one for the year.
Then you make switch case for the month to associate it with its name (e.g. 03 -> Mar).

On SQL Server you can do using CONVERT and STUFF;
SELECT STUFF(CONVERT(VARCHAR(11), GETDATE(),100),7,1,', ')
--Results: Dec 10, 2012

Assuming SQL server you want some combination of DATEPART and DATENAME like this:
SELECT
DATENAME(month, '2012-11-29 19:17:00') + ' ' +
CAST(DATEPART(dd, '2012-11-29 19:17:00') AS VARCHAR)+ ', ' +
CAST(DATEPART(yyyy, '2012-11-29 19:17:00') AS VARCHAR);

You did not specify what RDBMS, but if you are using SQL Server, you can use:
select
convert(varchar(6), receivedate, 100)+', '+cast(year(receivedate) as char(4)) ReceiveDate,
convert(varchar(6), sentdate, 100)+', '+cast(year(sentdate) as char(4)) SentDate
from customers
See SQL Fiddle with Demo
Result:
| RECEIVEDATE | SENTDATE |
-------------------------------
| Nov 29, 2012 | Aug 28, 2007 |
If you are using MySQL, then you can use the Date_Format() function:
select
date_format(receivedate, '%b %e, %Y') ReceiveDate,
date_format(sentdate, '%b %e, %Y') SentDate
from customers
See SQL Fiddle with Demo

Related

SQL how to group by year end

I am not well advanced in SQL. Maybe someone could help me with this little problem.
I need to summarize data by year end. I know I can use
SELECT Year(Mydate) as year,
Sum(Amount) as amount
FROM table1
GROUP BY Year(Mydate)
Mydate here is full date column (i.e. 15/6/2020). However in this case I get year as year number. I need to get year as year end date, i.e. 12/31/2021. How can I do that?
Try format your field using DATE_FORMAT: (DATE_FORMAT(Mydate, '%Y/12/31')
https://www.w3schools.com/sql/func_mysql_date_format.asp
Try:
SELECT
Year(Mydate) as year,
Convert(datetime, Str(Year(Mydate)) + ' Dec 31', 103) as eoyDec31a
Convert(char(10), Convert(datetime, Str(Year(Mydate)) + ' Dec 31', 103), 103) As eoyDec31b,
Max(Mydate) As eoyMax,
Sum(Amount) as amount
FROM table1
GROUP BY
Year(Mydate),
Convert(datetime, Str(Year(Mydate)) + ' Dec 31', 103),
Convert(char(10), Convert(datetime, Str(Year(Mydate)) + ' Dec 31', 103), 103)
Order By 1
If Dec 31 occurs in the table for every year, just the eoyMax column will do. If not the end of year has to be created from the parts: 2012 dec 31 etc.
eoyDec31a is the american format and eoyDec31b is the date string in british format.
The Convert function converts from string to datetime and back.
OK, this seems to solve my issue:
SELECT DATEFROMPARTS(Year(Mydate),12,31) AS year,
Sum(Amount) as amount
FROM table1
GROUP BY DATEFROMPARTS(Year(Mydate),12,31)

Sort by text (nvarchar)

Not sure how to sort with column 'DateText' column like this:
April 2020
March 2001
May 2020
June 2021
December 2021
Expect result
December 2021
June 2021
March 2001
May 2020
April 2020
I tried this but no luck
ORDER BY
CASE WHEN 1 = ISNUMERIC(TextDate)
THEN CAST(TextDate AS INT)
END
Happily, your format can be converted to a date:
order by convert(date, textdate)
You actually want desc for your example.
To build a little on the good answer and advice given here, once the data is in a date datatype there are useful functions you can use to get specific information out of your date, such as the day of the week or the month name and so much more. These can then also be used to order your dates in many different ways should you require it!
Below I have made a little demo of the DATEPART and DATENAME functions, there are far more than this too.
https://www.w3schools.com/sql/func_sqlserver_datename.asp
This was written and tested in T-SQL for MS SQL Server
DECLARE #Dates TABLE(textdate nvarchar(15))
INSERT INTO #Dates
VALUES ('April 2020'),
('March 2001'),
('May 2020'),
('June 2021'),
('December 2021')
SELECT *,
CONVERT(date, textdate) AS datetype
INTO #dates
FROM #Dates
ORDER BY CONVERT(date, textdate) desc
SELECT *,
DATEPART(DD,datetype) AS DayNum,
DATEPART(MM,datetype) AS MonthNum,
DATEPART(YYYY,datetype) AS YearNum,
DATENAME(WEEKDAY, datetype) AS DayName,
DATENAME(MONTH, datetype) AS MonthName
FROM #dates
ORDER BY datetype desc
DROP TABLE #dates

How to get month and year from date in SQL

I need help to get month and year from a date which is in yyyymmdd format.
The dates look like this, '20150102', the output should be Jan 2015.
I want the output as on single value, currently using datepart function Im getting output in 2 different columns for month and year
This may help:
SQL Server:
SELECT FORMAT (GETDATE(), 'MMM yyyy') -- Jul 2019
SELECT FORMAT (GETDATE(), 'MMMM yyyy') -- July 2019
SELECT RIGHT(CONVERT(VARCHAR(11), GETDATE(), 106), 8) -- Jul 2019
For more details: https://www.tutorialgateway.org/sql-date-format/
MySQL:
SELECT DATE_FORMAT("20150102", "%M %Y"); -- January 2015
SELECT DATE_FORMAT("20150102", "%b %Y"); -- Jan 2015
SELECT YEAR(date) AS 'year', MONTH(date) AS 'month'
For more details: http://www.sqlines.com/mysql-to-oracle/date_format
MySQL will recognize the date string as a date, so you can use date_format() to get the format you want:
select date_format('20150102', '%b %Y')
there is a list of all differnet sql date formats sql date formats

last date of Financial year (I.e 2017-03-31)

Hi please let me know how to extract the last day of Financial year in sql server.my financial year start from 2016-04-01 to 2017-03-31
Closest you can use is End Of Month for that you need to provide one date to that month as below:
select eomonth('2017-03-01')
To get the last day of the financial year for any date, you need to find the last of march if before march, or the last of march next year if after march:
declare #yourdate datetime = getdate();
select case when month(#yourdate) < 4 then CONVERT(datetime,cast(YEAR(#yourdate) as char(4)) + '-03-31' ,120)
else CONVERT(datetime,cast(YEAR(#yourdate) + 1 as char(4)) + '-03-31' ,120)
end as financial_year_end
Edit:
If you want last date derived based on from_date, then use something like this
Rextester Demo
select
case when datepart(mm,from_date) <=3 then
cast(concat(year(from_date),'-03-31') as datetime)
else
dateadd(year,1,cast(concat(year(from_date),'-03-31') as datetime))
end as last_date_fin
from
(select '2017-04-30' as from_date union all
select '2017-01-13') t;
This way from_date between Jan - Mar will give same year's 31st march. Else it will give next year's 31st March.
Previous answer:
http://rextester.com/AXVM26769
If you want to get last day of march for same year as passed, then use
select cast(concat(given_year,'-03-31') as datetime)
from
(select '2017' as given_year) t
If you want to pass 2016 and then get 2017-03-31 then use. You can change the year in derived table and change the output based on that.
select dateadd(year,1,cast(concat(given_year,'-03-31') as datetime))
from
(select '2016' as given_year) t;
This Code will work to find the last date of Financial Year.
For Previous Year case matches and 'THEN' part will Execute and for current year 'ELSE'
part will execute.
select CASE WHEN (MONTH(GETDATE())) <= 3
THEN convert(varchar(4), YEAR(GETDATE())-1) + '-' + '03-31'
ELSE convert(varchar(4),YEAR(GETDATE()))+ '-' + '03-31'
end
> LastDayOfYearFY] =
> eomonth( dateadd(month, 5,
> dateadd(year, datepart(year, (dateadd(month, 6, [date])) ) -1900, 0)))
Idea extension taken from return-first-day-of-financial-year
You can select all the dates order them descendant and take the first one.
SELECT date
FROM table
ORDER BY date desc
LIMIT 1;

How to group daily data on weekly basis using sql

I am trying to group the number of hours that employees worked for the last 4 weeks but I want to group them on a weekly basis. For example:
WEEK HOURS
Feb 24 to March 2 55
March 3 to March 9 40
March 10 to March 16 48
March 17 to March 23 37
This is what I have so far, please help. thanks
SET DATEFIRST 1
SELECT CAST(MIN( [DT]) AS VARCHAR(20))+' TO '+CAST (MAX([DT]) AS VARCHAR(20)) AS DATE,
SUM(HOURS) AS NUM_HRS
FROM MyTable
GROUP BY DATEPART(WEEK,[DT])
HAVING COUNT(DISTINCT[DT])=7
Create a Calendar auxilliary table, with Year, Month, Week, Date columns (you can also add holidays and other interesting stuff to it, it has many potential uses) and populate it for the period of interest.
After that, it's as easy as this:
SELECT sum(hours), cast(min(date) as varchar), cast(max(date) as varchar)
FROM Calendar c
LEFT OUTER JOIN MyTable h on h.Date = c.date
GROUP BY year, week
ORDER BY year, week
SET DATEFIRST 1
SELECT DATEPART(WEEK,DT) AS WEEK,
SUM(HOURS) AS NUM_HRS
FROM MyTable
WHERE DT >= DATEADD(WEEK, -4, GetDate()),
GROUP BY DATEPART(WEEK,[DT])
Try something like
SELECT
DATEADD(DD,
CONVERT(INT, (DATEDIFF(DD, '1/1/1900', t.DT)/7)) * 7,
'1/1/1900') [WeekBeginDate],
DATEADD(DD,
(CONVERT(INT, (DATEDIFF(DD, '1/1/1900', t.DT)/7)) * 7) + 6,
'1/1/1900') [WeekEndDate],
SUM(HOURS) AS NUM_HRS
FROM MyTable t
GROUP BY CONVERT(INT, DATEDIFF(DD, '1/1/1900', t.DT)/7)
Though this is the brute force trick, I think in your case it will work.
EDIT : Modified the query a little bit, the error was caused because of the order in which DATEDIFF calculates the difference.
Also here is a SQL FIDDLE with a working example.
EDIT 2 : Updated the Fiddle with the Date Format. To customize the date format, this article would help.