I'm trying to count the number of accounts opened on today's date in the SELECT Statement. I'm doing so with an IIF statement and using CAST to convert the DateTimeStamp to Date. That said, I'm having trouble figuring out where to date column (Open_Date) and how to check to see if it's is today's date. Would I place -1 right after the ) following as date or???
COUNT(IIF(CAST(GETDATE() AS date))), SHARE.MEMBER_NBR, null)) AS ALLNEWACCOUNTSTODAY
You can use case expression with Open_Date :
COUNT(CASE WHEN CONVERT(DATE, GETDATE()) = Open_Date THEN SHARE.MEMBER_NBR END) AS ALLNEWACCOUNTSTODAY
Can you provide the architectur please ?
Without it I would suggest that :
SELECT COUNT(*) FROM Accounts Where CONVERT(date,accountDate)=CONVERT(DATE,getdate());
It assumes that you have a column accountDate containing the date you added the account and that this column is into an accounts table.
Related
I have a timestamp in my big query looking like this: 30/01/2020 00:14:05
date is one of the column names of the table
I have already tried:
1. cast(PARSE_DATE('%Y%m%d', date) as DATE)
2. CAST(date as DATE)
In your case you need SELECT PARSE_DATETIME('%d/%m/%Y %H:%M:%S','30/01/2020 00:14:05')
or SELECT PARSE_DATE('%d/%m/%Y',SUBSTR('30/01/2020 00:14:05',1,10)) if you only need the date
I have a SQL Database varchar field which is called date_finish. This field has been setup as a varchar(50). The format of the date is set out like this: 07/06/2017 dd/mm/yyyy.
I'm trying to search the database for all dates over 1 year old using this statement:
SELECT CONVERT(datetime, date_finish, 103) AS DB_DATE, booking_code, cust_id, status
FROM repair_details
WHERE (date_finish > DATEADD(year, - 1, GETDATE()))
ORDER BY DB_DATE
There are some fields that are blank, cust_id is 0 and status aren't complete, so I added:
(date_finish <> '') AND (status = 'COMPLETE') AND (cust_id <> '0')
to the above statement.
In all cases I get an error:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
What am I doing wrong???
Convert to a date using an explicit format:
WHERE CONVERT(date, date_finish, 103) > DATEADD(year, - 1, GETDATE())
Obviously, this gets dates since one year ago -- based on your code. If you want older dates, then use < rather than >.
Then, fix the data! You should be storing date/time values using proper types. One method is:
update repair_details
set date_finish = CONVERT(date, date_finish, 103); -- sets to default date format on system
alter repair_details alter date_finish date;
You haven't done the date conversion in the WHERE clause - it is comparing a string to a date there.
The error is because, when the column date_finish appears to be null. As such, you need to have a condition:
((date_finish IS NOT NULL ) AND
(date_finish <> '' ) ) AND
(status = 'COMPLETE' ) AND
(cust_id <> '0' )
I can't seem to get GROUP BY to work when converting a smalldatetime to date. I have the following query:
SELECT CAST(XDate AS DATE) as 'TheDate', SUM (TheCount) as count
FROM TheTable
WHERE XDate >= '4/1/2018' AND XDate < '5/1/2018'
GROUP BY CAST(XDate AS DATE), TheCount
This query returns over 300,000 rows. Last I checked, April only had 30 days in it, so I would expect at most to get 30 rows with a sum for each day. It is like SQL is still taking the time portion into consideration. I am not sure what I am missing. Can someone point me in the right direction?
Remove thecount from the group by and fix the date constants:
SELECT CAST(XDate AS DATE) as TheDate, SUM(TheCount) as count
FROM TheTable
WHERE XDate >= '2018-04-01' AND XDate < '2018-05-01'
GROUP BY CAST(XDate AS DATE)
ORDER BY TheDate;
I also discourage you from putting single quotes around column aliases. First, choose identifier names that do not need to be escaped (such as TheDate). Second, only use single quotes for string and date constants.
I want to subtract 2 dates in MS SQL Server.
Example:
Current date Last used date
'2016-03-30' '2015-02-03'
Current date refers to today's date, "Last used date" is a measure.
How to write a query in SQL Server?
I have this but doesn't work (it says "Operand data type is invalid for subtract operator")
select
CONVERT(DATE, GETDATE()) - CONVERT(DATE, LastUsedDate)
from
databasename
SELECT DATEDIFF(day,'2014-06-05','2014-08-05') AS DiffDate
Output DiffDate 61
More practice please refer below W3 school:
https://www.w3schools.com/sql/func_sqlserver_datediff.asp
Here you don't have to cast GETDATE() to date, as it is already datetime datatype. So your query will be as follows
SELECT DATEDIFF(day,CAST(LastUsedDate as date),GETDATE()) AS DifferneceDays
FROM TableName
The normal function to use is datediff():
select datediff(day, cast('2016-02-03' as date), cast('2016-03-30' as date))
You can subtract datetime values, but not dates. Alas.
Im having trouble on a query with a datetime field.
Its because i convert the datetime field to -varchar(10),..,103- so i can apply a where clause with date field on 103 format instead of datetime but then when i use the where clause it doesnt show results or group the way i need to, because the datetime field was converted to string.
Here is a simplified query for the example:
select ddate,SUM(ntotal) as Income from Inc_orders
where nbranch=2
and convert(varchar(10),ddate,103)
between '01/06/2010' and '31/06/2010'
group by convert(varchar(10),ddate,103)
order by ddfecha desc
ddate is the datetime field
ntotal is integer
nbranch is foreign key
Then what happens is that i get results from another 103 date range
01/10/2009 4447.0000
02/01/2010 26267.8000
02/02/2010 20498.0000
02/04/2010 22565.1000
02/05/2010 20539.0000
02/11/2010 33934.3000
02/12/2009 33587.4000
What i pretend to look it like is :
01/06/2010 29327.7000
02/06/2010 31170.4000
03/06/2010 37737.7000
04/06/2010 25109.6000
06/06/2010 20819.7000
10/06/2010 44703.9000
14/06/2010 21755.1000
15/06/2010 39369.3000
05/06/2010 29552.2000
07/06/2010 35305.9000
08/06/2010 30628.6000
..........
31/06/2010 18677.6000
A solution is not using datepart, month, or year functions because i need the
parameter to look like a calendar to apply a datetimepicker calentad combo object on it.
Do not use CONVERT(VARCHAR, DateField, 103) to remove the time from DATETIME it is inefficient and also causes problems when sorting.
Depending on the version of SQL-Server you are using there are 2 options that are usually regarded as the best. For SQL-Server 2008 and upwards use CAST(DateField AS DATE), for previous versions use DATEADD(DAY, 0, DATEDIFF(DAY, 0, DateField))
Because you are converting Ddate to a VARCHAR in this line:
convert(varchar(10),ddate,103) between '01/06/2010' and '31/06/2010'
you are removing the implicit conversion of '01/06/2010' and '31/06/2010' to dates. This means '02/01/2000' is greater than '01/01/2012' because you are comparing strings not dates. If you remove the time from Ddate and keep the expression in a date(time) format, '01/06/2010' and '31/06/2010' are implicitly converted to dates.
To illustrate this simply you can run this simple query:
SELECT CASE WHEN '02/06/2000' BETWEEN '01/06/2012' AND '03/06/2012' THEN 1 ELSE 0 END [String Comparison],
CASE WHEN CONVERT(DATETIME, '02/06/2000') BETWEEN '01/06/2012' AND '03/06/2012' THEN 1 ELSE 0 END [Date Comparison]
So your query would end up something like this:
SET DATEFORMAT DMY
SELECT CAST(DDate AS DATE) Ddate,
SUM(ntotal) as Income
FROM Inc_orders
WHERE nbranch=2
AND CAST(DDate AS DATE) BETWEEN '01/06/2010' AND '31/06/2010'
GROUP BY CAST(DDate AS DATE)
ORDER BY DDate
Or
SELECT DATEADD(DAY, 0, DATEDIFF(DAY, 0, DDate)) Ddate,
SUM(ntotal) as Income
FROM Inc_orders
WHERE nbranch=2
AND DATEADD(DAY, 0, DATEDIFF(DAY, 0, DDate)) BETWEEN '01/06/2010' AND '31/06/2010'
GROUP BY DATEADD(DAY, 0, DATEDIFF(DAY, 0, DDate))
ORDER BY DDate
ADDENDUM
I am not sure if Ddate contains a time, so using the above to remove the time may not be relevant, however the part about comparing strings in the where clause remains relevant. In addition there are very few occassions when it should be necessary to present your date to your application in string format. It would be better to keep the date as a date and format it within you application layer (whatever this may be).
Don't convert the date to a string, just leave it as a date:
select ddate,SUM(ntotal) as Income from Inc_orders
where nbranch=2
and ddate between '2010-06-01' and '2010-06-31'
group by nbranch
order by ddate
You can convert ddate in the select list, if you want it to display in a particular way.
You may convert document Date by :
ISNULL(CONVERT(varchar(12),tranInwardHeader.DocumentDate,103),'') AS DocumentDate
Do not use CONVERT nor in GROUP BY, nor in WHERE. use convert on SELECT list of fields
UPDATE:
See valid and recommended formats for DATE CONSTANTS at Microsoft. Please select a any format with "DATEFORMAT dependent: NO" and "Multilanguage: YES" in order to NOT have any problems in any language/setup
UPDATE:
i was writing just the same query #Blorgbeard when I saw: it wont work, because you cant group only by nbranch and have ddate on select list, also nbranch on group by has no sense because, only valid value is 2. You need to redefine your query/needs
I guess, your query may be:
select ddate,SUM(ntotal) as Income from Inc_orders
where nbranch=2
and ddate between '20100601' and '20100631'
group by ddate
order by ddate