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-%'
Related
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 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
I have query:
SELECT name
FROM (
SELECT name FROM
Hist_answer
WHERE id_city='34324' AND datetime >= DATE_SUB(CURRENT_DATE, INTERVAL 1 MONTH)
UNION ALL
SELECT name FROM
Hist_internet
WHERE id_city='34324' AND datetime >= DATE_SUB(CURRENT_DATE, INTERVAL 1 MONTH)
) x
GROUP BY name ORDER BY name
But DATE_SUB is a MySQL function and I need function for MsSQL 2008
Tell me please how to select data from 30 days by using MsSQL 2008?
P.S.: Data type of datetime is smalldatetime
You should be using DATEADD is Sql server so if try this simple select you will see the affect
Select DATEADD(Month, -1, getdate())
Result
2013-04-20 14:08:07.177
in your case try this query
SELECT name
FROM (
SELECT name FROM
Hist_answer
WHERE id_city='34324' AND datetime >= DATEADD(month,-1,GETDATE())
UNION ALL
SELECT name FROM
Hist_internet
WHERE id_city='34324' AND datetime >= DATEADD(month,-1,GETDATE())
) x
GROUP BY name ORDER BY name
Try this : Using this you can select date by last 30 days,
SELECT DATEADD(DAY,-30,GETDATE())
For those who could not get DATEADD to work, try this instead: ( NOW( ) - INTERVAL 1 MONTH )
Short version for easy use:
SELECT *
FROM [TableName] t
WHERE t.[DateColumnName] >= DATEADD(month, -1, GETDATE())
DATEADD and GETDATE are available in SQL Server starting with 2008 version.
MSDN documentation: GETDATE and DATEADD.
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
I want to be able to select all database rows where the month and year are the same as what I am searching for. Since the DATE field has year, month, and day, how do I search with year and month?
SELECT *
FROM tblTableName
WHERE Month(ColumnDate) = Month(MyDate)
AND Year(ColumnDate) = Year(MyDate)
SELECT *
FROM myTable
WHERE ( YEAR(myfield) = '2009')
AND ( MONTH(myfield) = '1')
The most efficient is to create the start and end date of the range that you want, so that you compare the dates as a single value instead of extracting the year and month properties from each date.
Example:
select SomeField
from SomeTable
where SomeDate >= ? and SomeDate < ?
(Note that the first comparison is inclusive and the seond is exclusive.)
Create the start and end date to use as parameters: (example in C#)
DateTime start = new DateTime(date.Year, date.Month, 1)
DateTIme end = start.AddMonths(1);
In MySQL:
SELECT *
FROM mytable
WHERE date >= STR_TO_DATE(CONCAT(EXTRACT(YEAR_MONTH FROM #mydate), '01'), '%Y%m%d')
AND date < STR_TO_DATE(CONCAT(EXTRACT(YEAR_MONTH FROM #mydate), '01'), '%Y%m%d') + INTERVAL 1 MONTH
This will efficiently use an index on the date field.
That would depend on what database backend you are using. IN SQl Server I would use
where year(datefield) = #year and month (datefield) - #month
to do this.
or you could build a where clause by creating a date range
where datefield between 20090101 and 20090201
You will want to use MONTH() and YEAR() in mysql.