Split a column time into 2 (Time,Date) - sql

how can i reformat this code to split results column time into 2 (Time,Date)
SELECT
DATEADD(mi, DATEDIFF(mi, 0, DATEADD(s, 2, dbo.roundtime
(CallStartTime, 1
))), 0) AS [Time], COUNT(DISTINCT [CallID]) AS [Sum of Calls]
FROM [Database].[dbo].[table]
WHERE CallStartTime BETWEEN '8/2/2015' AND '2/2/2016'
AND
CompanyID NOT IN
(718, 722, 1769, 1886, 2518, 2519, 2533, 2544, 2766, 2877, 3603, 3604, 4785, 5674, 5675, 5676, 5677, 5678, 5679, 5680, 5681,
5682, 5683, 5684, 5685, 5686, 5687, 5688, 5689, 5690, 5691, 5692, 9021, 9492, 9884, 4785, 11059, 11777, 11779, 13194
)

Convert is your friend:
select convert(char(10), getdate(), 120) dateComponent
, convert(char(5), getdate(), 108) timeComponent
Edit starts here:
For your question:
select convert(char(10), time, 120) dateComponent
, convert(char(5), time, 108) timeComponent
, "sum of calls"
from
(sql from the question goes here) temp

Related

T-SQL - Merge two tables without create temp table

The prefer result i want I have those 2 T-SQL queries:
SELECT
CONVERT(NVARCHAR, AL.FECHA, 103) AS [DATE],
CONVERT(NVARCHAR, AL.HORA, 108) AS [HOUR],
SUM(CAST(AL.TOTALNETO AS money)) AS [AMOUNT]
FROM
ALBVENTACAB AL
WHERE
CONVERT(NVARCHAR, AL.FECHA, 103) = '10/09/2020'
-- AND CONVERT(nvarchar, GETDATE(), 103) AND
AND CONVERT(nvarchar, AL.HORA, 108) BETWEEN (SELECT CONVERT(nvarchar, AL.HORA, 108)) AND CONVERT(nvarchar, GETDATE(), 108)
GROUP BY
AL.FECHA, AL.HORA
ORDER BY
AL.FECHA
SELECT
CONVERT(NVARCHAR, AL.FECHA, 103) AS [DATE],
CONVERT(NVARCHAR, AL.HORA, 108) AS [HOUR],
SUM(CAST(AL.TOTALNETO AS money)) AS [AMOUNT]
FROM
ALBVENTACAB AL
WHERE
FECHA = CAST(DATEADD(DD, -7, GETDATE()) AS date)
AND CONVERT(nvarchar, AL.HORA, 108) BETWEEN (SELECT CONVERT(nvarchar, AL.HORA, 108)) AND CONVERT(nvarchar, GETDATE(), 108)
GROUP BY
AL.FECHA, AL.HORA
ORDER BY
AL.FECHA
Individually, their results are correct. I want to create a result as shown in the screenshot.
I tried to create a CTE, but that didn't work. What is the proper way?
Thank you!
First, the query I think you're looking for is something like this. The Friday date (#fecha) was removed to a variable of type DATE. Instead of 2 query, 1 for the week, and 1 for the particular Friday, it's now 1 query. The friday sales are broken out using conditional aggregation into a new column called [FECHA_AMOUNT]. Note: this only works if the variable #fecha falls within the last 7 days.
declare #fecha date='20201009';
SELECT
CONVERT(NVARCHAR, AL.FECHA, 103) AS [DATE],
CONVERT(NVARCHAR, AL.HORA, 108) AS [HOUR],
SUM(CAST(AL.TOTALNETO AS money)) AS [AMOUNT],
SUM(case when cast(AL.FECHA as date)=#fecha then CAST(AL.TOTALNETO AS money)
else cast(0 as money) end) AS [FECHA_AMOUNT]
FROM
ALBVENTACAB AL
WHERE
FECHA = CAST(DATEADD(DD, -7, GETDATE()) AS date)
AND CONVERT(nvarchar, AL.HORA, 108) BETWEEN (SELECT CONVERT(nvarchar, AL.HORA, 108))
AND CONVERT(nvarchar, GETDATE(), 108)
GROUP BY
AL.FECHA, AL.HORA
ORDER BY
AL.FECHA;
Finally, as note in the comments. When converting it's always good to use lengths. Also, the money data type is not considered ideal. Fwiw

Filter by input date sql

I have to filter a query result by date using the dd-MM-YY format as input, when I run the query it gives me the empty table, how can I solve?
select Data, string_agg(Ore, ' ') as Ore
from (
select FORMAT(DataCreazione, 'dd/MM/yyyy', 'it-IT') as Data,
CONCAT(DATEPART(HOUR,DataCreazione), ':', DATEPART(MINUTE, DataCreazione)) as
Ore
from Marcatura
where IdUtente = #IdUtente
and (Stato='Ingresso' or Stato='Uscita')
and (CONVERT(datetime, DataCreazione, 103)
between CONVERT(datetime, #Start, 103)
and CONVERT(datetime, #End, 103))
) t
group by Data
order by CONVERT(datetime, Data, 103) desc
​
INPUT VALUE:
#IdUtente=2
#End='14-09-19'
#Start='05-02-19'
Your start and end date are all valid SQL format. You can cast it to datetime, but we need to tell SQL which format is your original date.
cast(CONVERT(VARCHAR(10), CONVERT(date, #Start, 5), 23) as datetime)
complete query:
select Data, string_agg(Ore, ' ') as Ore
from (
select FORMAT(DataCreazione, 'dd/MM/yyyy', 'it-IT') as Data,
CONCAT(DATEPART(HOUR,DataCreazione), ':', DATEPART(MINUTE, DataCreazione)) as
Ore
from Marcatura
where IdUtente = #IdUtente
and (Stato='Ingresso' or Stato='Uscita')
and cast(DataCreazion as DateTime)
between cast(CONVERT(VARCHAR(10), CONVERT(date, #Start, 5), 23) as datetime)
and cast(CONVERT(VARCHAR(10), CONVERT(date, #End, 5), 23) as datetime)
) t
group by Data
order by CONVERT(datetime, Data, 5) desc
Instead you can set connection level property like below.
SET DATEFORMAT DMY;
SELECT Data
,string_agg(Ore, ' ') AS Ore
FROM (
SELECT FORMAT(DataCreazione, 'dd/MM/yyyy', 'it-IT') AS Data
,CONCAT (DATEPART(HOUR, DataCreazione),':',DATEPART(MINUTE, DataCreazione)) AS Ore
FROM Marcatura
WHERE IdUtente = #IdUtente
AND (Stato = 'Ingresso' OR Stato = 'Uscita')
AND (
DataCreazione BETWEEN #Start and #End
)
) t
GROUP BY Data
ORDER BY CONVERT(DATETIME, Data, 103) DESC

mssql 2012 how display specific value

i have query
SELECT DATEDIFF(day,
CONVERT(char(10), GetDate(),101),
CONVERT(char(10), fieldname,101))
from tablename
where isdate(fieldname)=1
the output of query is
how i can display only value like 6 from the result???
SELECT DATEDIFF(day, CONVERT(char(10), GetDate(),101), CONVERT(char(10), fieldname,101))
FROM lablename
WHERE isdate(fieldname)=1
AND DATEDIFF(day, CONVERT(char(10), GetDate(),101), CONVERT(char(10), fieldname,101)) = 6
edit 1
SELECT * FROM
( SELECT DATEDIFF(day, CONVERT(char(10), GetDate(),101), CONVERT(char(10), fieldname,101)) AS DateDiff
FROM lablename
WHERE isdate(fieldname)=1
) A
WHERE DateDiff = 6

SQL Server: better / faster way of combining multiple counts in select

I am using the below stored procedure in order to combine multiple counts.
This works fine so far and returns the following XML, i.e. a count of records for each of the last 6 months.
As I am pretty new to SQL and all these counts are done on the same table I was wondering if there is a better / faster way to achieve the same.
Example result (XML):
<ranks>
<groupCount>18</groupCount>
<groupCount>15</groupCount>
<groupCount>21</groupCount>
<groupCount>13</groupCount>
<groupCount>15</groupCount>
<groupCount>19</groupCount>
</ranks>
My stored procedure:
BEGIN
SET NOCOUNT ON;
SELECT COUNT(*) AS groupCount
FROM Log_PE
WHERE CONVERT(DATE, dateEsc, 120) >= CONVERT(DATE, CONVERT(VARCHAR(6), GETDATE(), 112) + '01', 112)
UNION ALL
SELECT COUNT(*) AS groupCount
FROM Log_PE
WHERE CONVERT(DATE, dateEsc, 120) >= CONVERT(DATE, CONVERT(VARCHAR(6), DATEADD(month, -1, GETDATE()), 112) + '01', 112)
UNION ALL
SELECT COUNT(*) AS groupCount
FROM Log_PE
WHERE CONVERT(DATE, dateEsc, 120) >= CONVERT(DATE, CONVERT(VARCHAR(6), DATEADD(month, -2, GETDATE()), 112) + '01', 112)
UNION ALL
SELECT COUNT(*) AS groupCount
FROM Log_PE
WHERE CONVERT(DATE, dateEsc, 120) >= CONVERT(DATE, CONVERT(VARCHAR(6), DATEADD(month, -3, GETDATE()), 112) + '01', 112)
UNION ALL
SELECT COUNT(*) AS groupCount
FROM Log_PE
WHERE CONVERT(DATE, dateEsc, 120) >= CONVERT(DATE, CONVERT(VARCHAR(6), DATEADD(month, -4, GETDATE()), 112) + '01', 112)
UNION ALL
SELECT COUNT(*) AS groupCount
FROM Log_PE
WHERE CONVERT(DATE, dateEsc, 120) >= CONVERT(DATE, CONVERT(VARCHAR(6), DATEADD(month, -5, GETDATE()), 112) + '01', 112)
FOR XML PATH(''), ROOT('ranks')
END
Many thanks for any help with this, Tim.
Your requirements seem to conflict with what you are doing in your SQL
select CONVERT(DATE, CONVERT(VARCHAR(6), GETDATE(), 112) + '01', 112)
will get the first day of the current month
select CONVERT(DATE, CONVERT(VARCHAR(6), GETDATE(), 112) + '02', 112)
will get the second day of the current month
To get the count of the last 6 complete months of records grouped by month
SELECT COUNT(*) AS groupCount
FROM Log_PE
WHERE dateEsc >= CAST(DATEADD(day, 1, DATEADD(month, -6, DATEADD(day, day(GETDATE())*-1, GETDATE()))) as DATE) --the first day of the month, 6 months ago
AND dateEsc < DATEADD(day, (day(GETDATE())*-1)+1, GETDATE()) -- the first day of current month
GROUP BY year(dateEsc), month(dateEsc)
ORDER BY year(dateEsc), month(dateEsc)
FOR XML PATH(''), ROOT('ranks')
Here is a SQL Fiddle: http://www.sqlfiddle.com/#!3/3ff71/7

T-SQL select daily totals BUT daily cut off is not midnight

I need to count number of sales, and sum the totals of sales by date, easy. But the curve pitch is - I need the "cut off" to be 6pm not midnight.
6pm the day before until 6pm day of.
What's throwing me is "grouping". My counts are only pulling only the true date not "6pm" info.
Sort30 Day30 Total Counter
-------- ---------- --------------------- -----------
20120810 08/10/2012 675.32 9
20120809 08/09/2012 1314.68 16
Query:
SELECT top 30 CONVERT(VARCHAR(8), chickendate, 112) AS varSort30,
CONVERT(VARCHAR(10), chickendate, 101) AS varDay30,
SUM(CAST(transAmount AS money)) AS varTotal,
Count(chickendate) AS varCounter
FROM CHICKEN
WHERE
(chickendate >= dateadd(hour, 18, dateadd(day, datediff(day, 0, chickendate), -1)) AND
chickendate < dateadd(hour, 18, dateadd(day, datediff(day, 0, chickendate), 0)))
GROUP BY CONVERT(VARCHAR(8), chickendate, 112),
CONVERT(VARCHAR(10), chickendate, 101)
ORDER BY CONVERT(VARCHAR(8), chickendate, 112) DESC
Going round and round, I feel its something staring me in the face. Thanks.
If my logic is correct, this should give you the correct results:
SELECT TOP 30
CONVERT(VARCHAR(8), modifiedChickenDate, 112) AS varSort30,
CONVERT(VARCHAR(10), modifiedChickenDate, 101) AS varDay30,
SUM(CAST(transAmount AS money)) AS varTotal,
COUNT(modifiedChickenDate) AS varCounter
FROM (
SELECT
transAmount,
DATEADD(HOUR, 6, chickendate) AS modifiedChickenDate
FROM CHICKEN
) sub
GROUP BY
CONVERT(VARCHAR(8), modifiedChickenDate, 112) AS varSort30,
CONVERT(VARCHAR(10), modifiedChickenDate, 101) AS varDay30,
ORDER BY
CONVERT(VARCHAR(8), modifiedChickenDate, 112) AS varSort30
If chickendate is a DATETIME instead of just a DATE, then you can use
GROUP BY CONVERT(VARCHAR(10), DATEADD(hh, 6, chickendate), 101)
to advance the date 6 hours (making your cutoff 6 hours earlier than midnight, or 6pm), then group on the Day of year. This only works if you're storing time information, which I'm not sure you are. Post some schema for the necessary tables. But I think you're looking for...
SELECT TOP 30 CONVERT(VARCHAR(10), DATEADD(hh, 6, chickendate), 101) as Date
, SUM(CAST(transAmount as money)) AS Total
, Count(*) as Counter
FROM Chicken
GROUP BY CONVERT(VARCHAR(10), DATEADD(hh, 6, chickendate), 101)
ORDER BY CONVERT(VARCHAR(10), DATEADD(hh, 6, chickendate), 101) DESC
How about:
GROUP BY CONVERT(VARCHAR(8), dateadd(hour, 6, chickendate), 112)
So '2012-08-09 18:00:00' is grouped by 20120810, and '2012-08-09 17:59:59' is grouped by 20120809.