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
Related
I am trying to pull the new customers from each month from an SQL database. I've tried this:
SELECT COUNT (Name)
FROM Customer
WHERE Date_created BETWEEN CONVERT(date, getdate()) AND CONVERT(date, getdate()) - (30)
From your query I think this would do it simpler :
SELECT COUNT (Name) FROM Customer WHERE MONTH(Date_created)= MONTH(GETDATE())
although am not sure this is what you expect as your question could be interpreted in several ways
Edit : taking account of different years:
SELECT COUNT (Name) FROM Customer
WHERE MONTH(Date_created)= MONTH(GETDATE())
AND YEAR(Date_created)= YEAR(GETDATE())
Standard SQL:
select
extract(year from Date_created) as yr
,extract(month from Date_created) as mth
,count(*)
from Customer
group by
extract(year from Date_created) as yr
,extract(month from Date_created) as mth
order by yr, mth
Replace EXTRACT with a matching function in you DBMS, e.g. for SQL Server datepart(year, date)
You could use convert(varchar(6), getdate(), 112) to get the month in yyyymm format:
SELECT convert(varchar(6), getdate(), 112) as Month
, count(*)
FROM Customer
GROUP BY
convert(varchar(6), getdate(), 112)
I'm not a fan of using BETWEEN with dates (see this blog What do BETWEEN and the Devil Have in Common). However, the problem with your query is that the dates are in the wrong order. The smaller value has to go first:
SELECT COUNT(Name)
FROM Customer
WHERE Date_created BETWEEN CONVERT(date, getdate() - 30) AND CONVERT(date, getdate())
This is better written as :
SELECT COUNT(Name)
FROM Customer
WHERE Date_Created >= CONVERT(date, getdate() - 30) AND
Date_Created < CONVERT(date, getdate());
I'm not sure if this satisfies your definition of "month", but at least the query will return 30 days worth of creates.
SELECT Count(*)
FROM table A
WHERE CONVERT (DATE, GETDATE()) IN
(CONVERT(DATE, time_created), CONVERT(DATE, time_updated))
I tried or after the , but it didn't work out.
Are you using SQL Server? If so, is this what you intend?
SELECT Count(*)
FROM table A
WHERE CONVERT(DATE, time_created) = CONVERT(DATE, GETDATE()) OR
CONVERT(DATE, time_updated) = CONVERT(DATE, GETDATE());
If so, then your original query should also work.
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
OS - WindowsXP
SQL Server Management Studio 2008 R2
I am trying to get a count of the number of projects based on the release date, NOT date and TIME. However, some projects have the same release date but a different time and are treated as a separate project. As long as the project has the same date, I want it to be counted.
'Releasedate' on the server is a "datetime"
Select ProjectName, count(ProjectName) as Count, (Releasedate)
From DBTable Where Releasedate >= convert(nvarchar,(getdate()))
Group by projectname,releasedt
Current Results:
ProjectName Count Releasedate
Project_Nm_1 1 2010-03-27 00:00:00
Project_Nm_1 1 2010-03-27 08:00:00
Project_Nm_2 1 2010-03-27 00:00:00
Project_Nm_2 1 2010-03-27 08:00:00
I would like to see:
Project_Nm_1 2 2010-03-27
Project_Nm_2 2 2010-03-27
SQL Server 2008 introduced the new DATE datatype which does exactly what you're looking for - handle only the date, without any time. So just CAST your field to DATE and you should be fine:
SELECT
ProjectName, COUNT(ProjectName) as Count, CAST(Releasedate AS DATE)
FROM
dbo.DBTable
WHERE
CAST(Releasedate AS DATE) >= CAST(GETDATE() AS DATE)
GROUP BY
projectname, CAST(ReleaseDate as DATE)
To group by date only, try using the CONVERT function:
GROUP BY projectname, CONVERT(nvarchar, Releasedate, 101)
You'll want to use the same CONVERT function call in the select column list so that query's output shows just the date, as well.
Take a look at:
http://codingforums.com/showthread.php?t=56536
You can get the date portion of the ReleaseDate datetime using
DateAdd(dd, 0, DateDiff(dd, 0, ReleaseDate))
Therefore your query becomes
SELECT
ProjectName,
Count(ProjectName) as Count,
DateAdd(dd, 0, DateDiff(dd, 0, ReleaseDate)) as ReleaseDate
FROM
DBTable
WHERE
ReleaseDate >= getdate()
GROUP BY
ProjectName,
DateAdd(dd, 0, DateDiff(dd, 0, ReleaseDate))
If you find yourself stripping the time from datetimes frequently, then encapsulate it in a UDF.
Select
ProjectName, count(ProjectName) as CountProjects, Releasedate
From
DBTable
Where
Releasedate >= convert(nvarchar, getdate())
Group by
projectname,releasedt
Order by
CountProjects desc
P.S. Don't use built-in functions while choosing alias of any column
You could simply GROUP BY the parts of the date you want using DATEPART
SELECT
ProjectName,
Count(*),
DATEPART(year, Releasedate) as ReleaseYear,
DATEPART(month, Releasedate) as ReleaseMonth,
DATEPART(day, Releasedate) as ReleaseDay
FROM
DBTable
WHERE
Releasedate >= convert(nvarchar,(getdate()))
GROUP BY
ProjectName,
DATEPART(year, Releasedate),
DATEPART(month, Releasedate),
DATEPART(day, Releasedate)
I'll leave combining those parts into one field as an exercise for you if you want, but this will ignore the time portion of Releasedate when grouping
To explain:
declare #dt datetime
set #dt = '2010/12/22 12:34:56'
print #dt
print convert(char(8), #dt, 112)
Result:
Dec 22 2010 12:34PM
20101222
So, use
GROUP BY convert(char(8), releasedt, 112)