Showing posts with label SQL Server. Show all posts
Showing posts with label SQL Server. Show all posts

Friday, 22 November 2019

Get Month name from given Date in SQL or DateName function in SQL

DATENAME function :


It will return a specified part of a given date in string format. Here we will get month name from DATENAME function. 

Query as below:

SELECT  DATENAME(month,GETDATE()) as MonthName---Month Name
SELECT  DATENAME(mm,GETDATE()) as MonthName---Month Name
SELECT  DATENAME(m,GETDATE()) as MonthName---Month Name
SELECT  left(datename(month,getdate()),3)as MonthName-- Month Name first three Abbreviation
SELECT  DatePart(mm,GETDATE()) as MonthNumber -- Month Number


Output screen shot as below:-

Hope you enjoyed the post. Your valuable feedback, question, or comments about this post are always welcome.


Monday, 18 November 2019

Use of PIVOT in SQL

Introduction
Here, we will learn how to use PIVOT in SQL. It is very useful technique to transform or display data Row level to Column.

Description with Query


Suppose, we are working on large data and we want to analyze the data year wise and month wise for reporting purpose in that case PIVOT will help us to make it easy.


Help of PIVOT we can transform or display data row level to column level. It is also useful for to make multidimensional reports.


SQL Query as below:


Select * from CustomerData



SQL PIVOT Query:

--Get monthwise customer data
SELECT *

FROM (

SELECT name, mobileno, left(datename(month,dtDate),3)as [month], amount
FROM CustomerData

) as s
PIVOT
(
SUM(amount) FOR [month] IN (jan, feb, mar, apr,may, jun,
jul, aug, sep, oct, nov, dec)



)AS pvt


PIVOT Result:


After execution, query result set screen as above. You can pass year in query then you can get particular year month wise data.

Conclusion

  • Pivot help us to transform or display data row to column level.
  • It help us to generate Multi-Dimensional report.
  • We can generate Month & year wise summary of large data.
Hope you enjoyed the post. Your valuable feedback, question, or comments about this post are always welcome.