Returning Month Name in SQL Server Query - sql

Using SQL Server 2008, I have a query that is used to create a view and I'm trying to display a month's name instead of an integer.
In my database, the datetime is in a column called OrderDateTime. The lines in the query that return the date is:
DATENAME(yyyy, S0.OrderDateTime) AS OrderYear,
DATEPART(MONTH, S0.OrderDateTime) AS OrderMonth
This returns a column of years and a column of months as integers. I want to return the month names (Jan, Feb, etc). I've tried:
CONVERT(varchar(3), DATEPART(MONTH, S0.OrderDateTime) AS OrderMonth
This is obviously is incorrect, as I get
Incorrect syntax near 'AS'
message. What is the proper syntax for my query?

This will give you the full name of the month.
select datename(month, S0.OrderDateTime)
If you only want the first three letters you can use this
select convert(char(3), S0.OrderDateTime, 0)

Have you tried DATENAME(MONTH, S0.OrderDateTime) ?

Change:
CONVERT(varchar(3), DATEPART(MONTH, S0.OrderDateTime) AS OrderMonth
To:
CONVERT(varchar(3), DATENAME(MONTH, S0.OrderDateTime)) AS OrderMonth

Try this:
SELECT LEFT(DATENAME(MONTH,Getdate()),3)

DECLARE #iMonth INT=12
SELECT CHOOSE(#iMonth,'JANUARY','FEBRUARY','MARCH','APRIL','MAY','JUNE','JULY','AUGUST','SEPTEMBER','OCTOBER','NOVEMBER','DECEMBER')

Select SUBSTRING (convert(varchar,S0.OrderDateTime,100),1,3) from your Table Name

In SQL Server 2012 it is possible to use FORMAT(#mydate, 'MMMM') AS MonthName

This will give you what u are requesting for:
select convert(varchar(3),datename(month, S0.OrderDateTime))

SELECT MONTHNAME( `col1` ) FROM `table_name`

for me DATENAME was not accessable due to company restrictions.... but this worked very easy too.
FORMAT(date, 'MMMM') AS month

Without hitting db we can fetch all months name.
WITH CTE_Sample1 AS
(
Select 0 as MonthNumber
UNION ALL
select MonthNumber+1 FROM CTE_Sample1
WHERE MonthNumber+1<12
)
Select DateName( month , DateAdd( month , MonthNumber ,0 ) ) from CTE_Sample1

basically this ...
declare #currentdate datetime = getdate()
select left(datename(month,DATEADD(MONTH, -1, GETDATE())),3)
union all
select left(datename(month,(DATEADD(MONTH, -2, GETDATE()))),3)
union all
select left(datename(month,(DATEADD(MONTH, -3, GETDATE()))),3)

Try the following
SELECT
Format( ('YourDateColumn'),'MMM') as MonthName
FROM YourTableName

Related

sql query to Select Day of month

I want a query where the output should be only day of month
for.e.g Consider today's date(04-07-2016).The output should be "04"
I have been trying with multiple queries which returns complete date.Whereas I need simply date(01-30).
please guide for the same.
Try the followig
SELECT datepart(dd, GETDATE())
It will helps to format the date also.
if you want left pad with zero for single digit then try the following query
SELECT RIGHT('0'+ convert(varchar,datepart(DD, GETDATE())) ,2)
For SQL-Server (using a DateTime column):
select datepart(day, datecolumn)
For SQL-Server (using today):
select datepart(day, getdate())
If you want a 2 digit date (04 and not just 4), then use this:
select right('0' + rtrim(day(getdate())), 2);
You can use the below query for today,
SELECT RIGHT('00'+CAST (datepart(DAY, getdate()) AS VARCHAR(2)),2)
For a column in DB,
SELECT RIGHT('00'+CAST (datepart(DAY, <<YourColumn>>) AS VARCHAR(2)),2) From <<YourTable>>

How to convert date to text Month?

How do I convert the date to a month spelled out?
For example.
01/08/2015 = January
I have it code in excel =TEXT(value, "MMMM") but we are shifting away from Excel to SQL Server.
Thanks
You need to use the DATENAME function.
For example:
SELECT DATENAME(mm, DateCol) AS [Month]
FROM MyTable
SELECT DATENAME(MONTH, GETDATE())
This is one way:
SELECT DATENAME(month, youDateField)) as Month
FROM YourTable
select datename(month,convert(varchar,yourField,106)) as month
from yourTable

Is it possible to use datepart in datediff?

Is it possible to use datepart in datediff
SELECT datediff (YEAR,2015 ,(datepart(YEAR,Person.DateOfBirth))) AS Exp from Person
please help me
The arguments for datediff should be dates, for example:
datediff(year, '20150101', Person.DateOfBirth)
When using year, please remember that it tracks the change of year, so it's not suitable for calculating age, for example
select datediff(year, '20150101', '20150201')
Returns 0, but
select datediff(year, '20141231', '20150201')
returns 1.
yes,
but the formation like this..
datediff(result In(Year/Month/Day),toDate,Fromdate)
'2015' in datediff is '2015-01-01'
try: select CAST('2015' as date)
SELECT datediff(YEAR,'2015',Person.DateOfBirth) AS Exp from Person--result in year
SELECT datediff(Month,'2015',Person.DateOfBirth) AS Exp from Person--result in month
SELECT datediff(Day,'2015',Person.DateOfBirth) AS Exp from Person--result in day

MS SQL select a datetime by month?

How do you select a datetime column by month?
TABLE A
TITLE MY_DATETIME
blah 2011-03-26 05:44:43.213
SELECT *
FROM TABLE A
WHERE MY_DATETIME = "August"
SELECT *
FROM TABLE A
WHERE DATEPART(month, MY_DATETIME) = 8
Or, less reliably based on language setting:
...
WHERE DATENAME(month, MY_DATETIME) = 'August'
I use EXTRACT function instead of DATEPART.
DATEPART function doesn't exist in my mysql.
SELECT *
FROM TABLE A
WHERE DATENAME(MONTH, MY_DATETIME) = 'August'
Other option:
CONVERT(VARCHAR(10), My Date, 23) like '2020-10-%'

SQL Server Server query - Count distinct DateTime field

Supposing we have the following records in an SQL Server table.
Date
19/5/2009 12:00:00 pm
19/5/2009 12:15:22 pm
20/5/2009 11:38:00 am
What is the SQL syntax for getting something like this one?
Date Count
19/5/2009 2
20/5/2009 1
You need to do any grouping on a Date only version of your datefield, such as this.
SELECT
CONVERT(VARCHAR(10), YourDateColumn, 101),
COUNT(*)
FROM
YourTable
GROUP BY
CONVERT(VARCHAR(10), YourDateColumn, 101)
I usually do this though, as it avoids conversion to varchar.
SELECT
DATEPART(yy, YourDateColumn),
DATEPART(mm, YourDateColumn),
DATEPART(dd, YourDateColumn),
COUNT(*)
FROM
YourTable
GROUP BY
DATEPART(yy, YourDateColumn),
DATEPART(mm, YourDateColumn),
DATEPART(dd, YourDateColumn)
EDIT: Another way to get just the date part of a datetime
DATEADD(d, 0, DATEDIFF(d, 0, YourDateColumn))
That would depend on your database engine. For SQL Server 2008 (and future versions), you can use the date type to do this.
select
convert(date, date_column_name) as Date,
count(1) as Count
from table_name
group by convert(date, date_column_name)
Depends on your DBMS. Example for Mysql:
SELECT DATE_FORMAT(dateColumn, '%e/%c/%Y') as `date`, COUNT(*)
FROM YourTable
GROUP BY `date`
What RDBMS are you on? Using Sybase, your query would look like this:
select date(datetimeColumn) as myDate, count(*) as myTotal
from thisTable
Group by myDate
Order by myTotal, myDate
After Googling found this one too...
SELECT CAST(FLOOR(CAST(Expr1 AS FLOAT)) AS DATEtime) AS Expr1,
COUNT(*) AS Expr2
FROM MY_TABLE
GROUP BY
CAST(FLOOR(CAST(Expr1 AS FLOAT)) AS DATEtime)
The cons?
High speed execution
The results returned are in the original locale. Ex for Greek 19/5/2009
Thank you all