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
Related
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
I am using
CONVERT(nvarchar, Date, 106)
it gives 04MAY2017
but i need 4MAY2017 FOR 04/05/2017
and 11MAY2017 FOR 11/05/2017
If CONVERT(nvarchar, Date, 106) gives 04MAY2017 and you if you want to remove the first character if it is 0 then you can use a CASE expression.
Query
SELECT CASE WHEN LEFT(CONVERT(nvarchar, Date, 106), 1) = '0'
THEN RIGHT(CONVERT(nvarchar, Date, 106), LEN(CONVERT(nvarchar, Date, 106)) - 1)
ELSE CONVERT(nvarchar, Date, 106) END
FROM your_table_name;
You could check when day-in-month < 10 then remove first character.
SELECT REPLACE(CASE
WHEN datepart(day,dateColumn) < 10
THEN STUFF(convert(nvarchar(20), dateColumn, 106), 1,1,'')
ELSE convert(nvarchar(20), dateColumn, 106)
END, ' ', '') AS dateText
FROM yourTable
I'm using SQL Server Compact 4.0 version, and although it might seem a simple thing to find in google, the examples I've tried none of them work.
My column signup_date is a DateTime with a value 04-09-2016 09:05:00.
What I've tried so far without success:
SELECT FORMAT(signup_date, 'Y-m-d') AS signup_date;
SELECT CONVERT(signup_date, GETDATE()) AS signup_date
SELECT CAST(data_registo, date) AS signup_date
I found that I could use DATEPART function, but that would force me to concat the values, is this the right path to follow? If so, how do I concat as Y-m-d?
SELECT DATEPART(month, signup_date)
SQL Server Compact has no date type.
If you don't want to see the time, convert the datetime value to a string:
SELECT CONVERT(nvarchar(10), GETDATE(), 120)
(This has been tested and actually works against SQL Server Compact)
you were actually on track with the CAST function just a slight error in the syntax. In the CAST function, there needs to be 'as' i.e CAST(data_registo as date)
SELECT CAST(data_registo as date) AS signup_date;
Most of the answers seek to achieve same thing but the explanation to the codes is not enough
CONVERT(date, Date_Updated, 120)
this code does the conversion with mssql. The first item 'date' is the datatype to return. it could be 'datetime', 'varchar', etc.
The second item 'Date_Updated' is the name of the column to be converted.
the last item '120' is the date style to be returned. There are various styles and the code entered will determine the output. '120' represent YYYY-MM-DD.
Hope this helps
The old fashioned way of doing this in SQL Server might work for your purposes:
select dateadd(day, datediff(day, 0, signup_date), 0)
The datediff() gets the number of days (as an integer) since time 0. The dateadd() adds this number back.
If you don't like 0 as a date, you can put any valid date in its place:
select dateadd(day, datediff(day, '2000-01-01', signup_date), '2000-01-01')
EDIT:
If you simply don't want to see the time, convert the date to a string:
select convert(nvarchar(10), signup_date, 120)
(I recommend the YYYY-MM-DD format, but others are available.)
I have tried this and many other solutions. I wanted a generic solution that would work with any LCID. My solution is a bit of convoluted code, but it works perfectly for me. It's a booking system where I needed to find out who was arriving on a particular date. ArriveDate is the column, d is the DATE I want.
SQL = "SELECT * FROM tablename WHERE dateadd(day, datediff(day, 0,
ArriveDate), 0)=' " & Format(d, "yyyy-MM-dd") & " ' "
This will return only date value in original datetime type. So you can do any comparison using the output
SELECT convert(datetime, CONVERT(nvarchar(10), GETDATE(), 120))
Just saw the Question today, a bit late I know :) but maybe this will help..,
select convert(date,(convert(varchar(20),'04-09-2016 09:05:00')))
select convert(nvarchar, getdate(), 1) = 09/25/19
select convert(nvarchar, getdate(), 2) = 19.09.25
select convert(nvarchar, getdate(), 3) = 25/09/19
select convert(nvarchar, getdate(), 4) = 25.09.19
select convert(nvarchar, getdate(), 5) = 25-09-19
select convert(nvarchar, getdate(), 6) = 25 Sep 19
select convert(nvarchar, getdate(), 7) = Sep 25, 19
select convert(nvarchar, getdate(), 10) = 09-25-19
select convert(nvarchar, getdate(), 11) = 19/09/25
select convert(nvarchar, getdate(), 12) = 190925
select convert(nvarchar, getdate(), 23) = 2019-09-25
select convert(nvarchar, getdate(), 101) = 09/25/2019
select convert(nvarchar, getdate(), 102) = 2019.09.25
select convert(nvarchar, getdate(), 103) = 25/09/2019
select convert(nvarchar, getdate(), 104) = 25.09.2019
select convert(nvarchar, getdate(), 105) = 25-09-2019
select convert(nvarchar, getdate(), 106) = 25 Sep 2019
select convert(nvarchar, getdate(), 107) = Sep 25, 2019
select convert(nvarchar, getdate(), 110) = 09-25-2019
select convert(nvarchar, getdate(), 111) = 2019/09/25
select convert(nvarchar, getdate(), 112) = 20190925
select convert(nvarchar, getdate(), 8) = 13:48:36
select convert(nvarchar, getdate(), 14) = 13:49:48:713
select convert(nvarchar, getdate(), 24) = 13:49:57
select convert(nvarchar, getdate(), 108) = 13:50:07
select convert(nvarchar, getdate(), 114) = 13:50:14:490
select convert(nvarchar, getdate(), 0) = Sep 25 2019 1:50PM
select convert(nvarchar, getdate(), 9) = Sep 25 2019 1:50:31:813PM
select convert(nvarchar, getdate(), 13) = 25 Sep 2019 13:50:39:307
select convert(nvarchar, getdate(), 20) = 2019-09-25 13:50:49
select convert(nvarchar, getdate(), 21) = 2019-09-25 13:50:58.923
select convert(nvarchar, getdate(), 22) = 09/25/19 1:51:07 PM
select convert(nvarchar, getdate(), 25) = 2019-09-25 13:51:14.473
select convert(nvarchar, getdate(), 100) = Sep 25 2019 1:51PM
select convert(nvarchar, getdate(), 109) = Sep 25 2019 1:51:32:227PM
select convert(nvarchar, getdate(), 113) = 25 Sep 2019 13:51:38:740
select convert(nvarchar, getdate(), 120) = 2019-09-25 13:51:50
select convert(nvarchar, getdate(), 121) = 2019-09-25 13:51:57.153
select convert(nvarchar, getdate(), 126) = 2019-09-25T13:52:03.627
Use this, i had the same problem
SELECT CAST(data_registo as date) AS "signup_date"
To get a string value, use CONVERT
select convert(varchar(10), signup_date, 11)
Check here for various formats:
https://msdn.microsoft.com/en-us/library/ms187928.aspx
To get a DATE, and just strip out the time, do this
Select Cast (signup_date as DATE)
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
How can i convert the datetime format below
2010-10-25 11:13:36.700
into
25-Oct-2010 or 2010-10-25 00:00:00.000
To get "25-Oct-2010"
Assuming the value is supplied as a string, not a DATETIME data type:
SELECT REPLACE(CONVERT(VARCHAR, CAST('2010-10-25 11:13:36.700' AS DATETIME), 106), ' ', '-')
See the CAST/CONVERT documentation for other formats, though the one you requested requires post-processing.
To get "2010-10-25 00:00:00.000"
The best performing means is to use DATEADD & DATEDIFF:
SELECT DATEADD(d, DATEDIFF(dd, 0, '2010-10-25 11:13:36.700'), 0)
References:
DATEADD
DATEDIFF
Testing
WITH sample AS (
SELECT CAST('2010-10-25 11:13:36.700' AS DATETIME) dt)
SELECT REPLACE(CONVERT(VARCHAR, s.dt, 106), ' ', '-') AS col1,
DATEADD(d, DATEDIFF(dd, 0, s.dt), 0) AS col2
FROM sample s
Returns:
col1 col2
-------------------------------------
25-Oct-2010 2010-10-25 00:00:00.000
Addendum
Being that you're on SQL Server 2005, you could make date formatting easier for yourself by creating a SQLCLR function that would allow you to use the .NET date formatting.
check this one
SELECT convert(VARCHAR, getdate(),106)
this will give you 25 Oct 2010
AND
SELECT REPLACE(convert(VARCHAR, getdate(),106), ' ' , '-')
this will give you 25-Oct-2010
Check if this can help you:
SELECT REPLACE(CONVERT(VARCHAR(11), GETDATE(), 106), ' ', '-')
You can get more info Here
PD. Stackoverflow can be too addicted sometimes, you might wanna try google before
select CONVERT(NVARCHAR(20),getDATE(),105)
Please try
SELECT CONVERT(varchar, GETDATE(), 1) --12/30/06
SELECT CONVERT(varchar, GETDATE(), 2) --06.12.30
SELECT CONVERT(varchar, GETDATE(), 3) --30/12/06
SELECT CONVERT(varchar, GETDATE(), 4) --30.12.06
SELECT CONVERT(varchar, GETDATE(), 5) --30-12-06
SELECT CONVERT(varchar, GETDATE(), 6) --30 Dec 06
SELECT CONVERT(varchar, GETDATE(), 7) --Dec 30, 06
SELECT CONVERT(varchar, GETDATE(), 10) --12-30-06
SELECT CONVERT(varchar, GETDATE(), 11) --06/12/30
SELECT CONVERT(varchar, GETDATE(), 12) --061230
SELECT CONVERT(varchar, GETDATE(), 23) --2006-12-30
SELECT CONVERT(varchar, GETDATE(), 101) --12/30/2006
SELECT CONVERT(varchar, GETDATE(), 102) --2006.12.30
SELECT CONVERT(varchar, GETDATE(), 103) --30/12/2006
SELECT CONVERT(varchar, GETDATE(), 104) --30.12.2006
SELECT CONVERT(varchar, GETDATE(), 105) --30-12-2006
SELECT CONVERT(varchar, GETDATE(), 106) --30 Dec 2006
SELECT CONVERT(varchar, GETDATE(), 107) --Dec 30, 2006
SELECT CONVERT(varchar, GETDATE(), 110) --12-30-2006
SELECT CONVERT(varchar, GETDATE(), 111) --2006/12/30
SELECT CONVERT(varchar, GETDATE(), 112) --20061230