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.
Related
I want to get the query result (e.g. to populate table) of last 7 dates (without times). I know that we can select some scalars without FROM statement. So I ended up with following solution:
select DATEADD (DAY, 0, CONVERT(date, GetDate()))
Union
select DATEADD (DAY,-1, CONVERT(date, GetDate()))
Union
select DATEADD (DAY,-2, CONVERT(date, GetDate()))
Union
select DATEADD (DAY,-3, CONVERT(date, GetDate()))
Union
select DATEADD (DAY,-4, CONVERT(date, GetDate()))
Union
select DATEADD (DAY,-5, CONVERT(date, GetDate()))
Union
select DATEADD (DAY,-6, CONVERT(date, GetDate()))
Please point me to better (and more elegant) solution if there is one.
The VALUES, table value constructor, is a little more concise.
select dateadd(day, x.num, convert(date, getdate()))
from (
values (0), (-1), (-2), (-3), (-4), (-5), (-6)
) x (num)
order by x.num;
Returns:
Date
2021-11-18
2021-11-19
2021-11-20
2021-11-21
2021-11-22
2021-11-23
2021-11-24
You can also expand on the values table by using either a permanent numbers/tally table (always handy) or by generating an artibrary list of numbers from any suitable table, you can then create a list of dates for any period
select dateadd(day, n.v, convert(date, getdate()))
from (
select top (30) v=-1 * Row_Number() over(order by (select 1))
from master.dbo.spt_values
)n
order by n.v;
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.
I wanted to check the records from back 10 days to current date when I was trying to run this query:
select top 1 *
from tbl_refrence_master
where ref_id='4110118488'
and CONVERT(date, ref_entry_date) between DATEADD(day, -10, convert(ref_entry_date, GETDATE()))
and CONVERT(date, getdate())
But I'm getting this error:
Type ref_entry_date is not a defined system type.
I can do the same through my source code but I wanted to do by this SQL query.
Why I am getting this error?
What is ref_entry_date? You should be using:
select top 1 rm.*
from tbl_refrence_master rm
where ref_id = '4110118488' and
CONVERT(date, ref_entry_date) between DATEADD(day, -10, GETDATE()) and CONVERT(date, getdate());
Or, because I don't like to use between with dates or datetimes:
select top 1 rm.*
from tbl_refrence_master rm
where ref_id = '4110118488' and
ref_entry_date >= DATEADD(day, -10, CONVERT(date, GETDATE()) ) and
ref_entry_date >= CONVERT(date, getdate());
SELECT top 1 *
FROM tbl_refrence_master
WHERE ref_id='4110118488'
AND ISDATE(ref_entry_date) = 1
AND DATEDIFF(d,ref_entry_date,GETDATE()) BETWEEN 0 AND 10
I'm trying to get the count of each distinct field in my database. For example, we are using something called sourceCodes - I want to be able to see how many of each different sourceCode there is in my database. So far, I have this
SELECT sourceCode, COUNT(DISTINCT sourceCode)
FROM [SecureOrders]
WHERE DateTime >= DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)
AND DateTime < DATEADD(day, DATEDIFF(day, 0, GETDATE()), 1)
GROUP BY sourceCode
(I'm trying to display the name of the sourceCode first, and then the count). So far though, the only thing I ever get in my second column is "1"...and I'm positive there are more than one. I know I worded this question really poorly, but I can't really figure out any other way to say it. Can anybody see why this is happening?
The "distinct" in your sample is not being applied at the correct place. By grouping by SourceCode, you are already getting distinct values from that column.
So, you only need to count the rows in each group:
SELECT sourceCode, COUNT(*)
FROM [SecureOrders]
WHERE DateTime >= DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)
AND DateTime < DATEADD(day, DATEDIFF(day, 0, GETDATE()), 1)
GROUP BY sourceCode
Remove the DISTINCT:
SELECT sourceCode, COUNT(sourceCode)
FROM [SecureOrders]
WHERE DateTime >= DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)
AND DateTime < DATEADD(day, DATEDIFF(day, 0, GETDATE()), 1)
GROUP BY sourceCode
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