convert MS Access sql statement to MS SQL statement - sql-server

What would be the conversion of this MS Access sql statement to MS SQL statement:
Select *
from iapt
where appt_id in (Select distinct appt_id
from iaptd where po_id in(Select distinct po_id
from irct
where verify_dtim = Date()-1))

Subtract one day from GETDATE() (today).
Select *
from iapt
where appt_id in (Select distinct appt_id
from iaptd where po_id in(Select distinct po_id
from irct
where verify_dtim = DATEADD(day, -1, convert(date, GETDATE())))

I guess you only need to change
Date() - 1
to
DATEADD(dd, -1, CAST(GETDATE() AS DATE))

Related

Transpose SQL Result

I have following sql query
SELECT [POINT],[Timestamp],AvgValue=AVG(ValueNumeric)
FROM [myDatabase].[dbo].[Value]
WHERE [Point] In (98,99,100,101,102,103,104,105)
AND Timestamp between DATEADD(DAY, -90, GETDATE()) and GETDATE()
Group by [timestamp],[Point]
order by [Timestamp], [Point]
which returns table as below
but I need to transpose this as below
Timestamp|Point1|Value1|Point2|Value2|Point3|Value3|
Is this doable?

TSQL Substract from report parameter to get previous month

I can really use some help. I'm trying to subtract 1 from a VARCHAR report parameter that is within a CTE. I'm working in SQL 2008 R2
This works fine:
AND MONTH(CAST(c.DATETIME AS DATE)) = CONVERT(INT,#Maand)
But this just doesn't seem to work:
AND MONTH(CAST(c.DATETIME AS DATE)) = CONVERT(INT,#Maand) -1
Thanks
Here is some of my code:
ALTER PROCEDURE [rpt].[usp_F_Telefonie_Bereikbaarheid](#Jaar AS VARCHAR(4), #Maand AS VARCHAR(2))
AS
BEGIN
WITH CTE AS(
SELECT
C.DATETIME AS Datum
,d.NAME
,COUNT(Callid) AS Aantal
,'Vorige' AS Gesprekken
,DURATIONSEC
FROM DM.dm.F_CDRS AS c
JOIN DM.dm.D_DEPARTMENTS AS d
ON d.DEPARTMENTID = c.DEPARTMENTID
WHERE Direction = 1
AND YEAR(CAST(c.DATETIME AS DATE)) = #Jaar
AND MONTH(CAST(c.DATETIME AS DATE))= CONVERT(INT,#Maand)
AND WAITTIMESEC <=10
GROUP BY LEFT(D.NAME, 2), D.NAME, C.DATETIME, DURATIONSEC
)
SELECT
,CASE CTE.Gesprekken
WHEN 'Vorige'
THEN SUM(CTE.Aantal)
END AS Vorige
FROM CTE
WHERE MONTH(CAST(CTE.Datum AS DATE)) = #Maand
AND YEAR(CAST(CTE.Datum AS DATE)) = #Jaar
GROUP BY CTE.NAME, CTE.Gesprekken, CTE.DURATIONSEC, CTE.Aantal
You need to convert varchar --> datetime and then use datediff function.
Instead this:
YEAR(CAST(c.DATETIME AS DATE)) = #Jaar
AND MONTH(CAST(c.DATETIME AS DATE))= CONVERT(INT,#Maand)
use:
datediff(month, cast(#Jaar + right('0' + #Maand, 2) + '01' as datetime), c.[datetime]) = 1

SQL Server syntax to return a query between 2 given dates

I have an SQL Server table that has a column called statusdate that's an ODBC date/time.
I need to create a SELECT query WHERE statusdate can return a range between 2 dates (a start date and an end date). I've been told to use DATEDIFF in my WHERE statement. Is this correct and what's the syntax?
If you are using SQL Server 2008/2012 and the date datatype you can use between.
select *
from YourTable
where statusdate between #StartDate and #EndDate
If you use datetime I suggest you use >= and < instead to make sure you get all values regardless of the time part.
select *
from YourTable
where statusdate >= #StartDate and
statusdate < dateadd(day, 1, #EndDate)
Update
I've been told to use DATEDIFF in my WHERE statement. Is this correct
No you should not use datediff for this.
and what's the syntax?
Here... but don't use it.
select *
from YourTable
where datediff(day, statusdate, #StartDate) = 0 and
datediff(day, statusdate, #EndDate) = 0
Why don't you just do a BETWEEN?:
SELECT *
FROM YourTable
WHERE statusdate BETWEEN StartDate AND EndDate

SQL Server GROUP BY datetime ignore hour minute and a select with a date and sum value

I have a table with two fields - datetime and int. I want to do a group by on the datetime only on the date ignoring the hour and minute. The SELECT statement should return a date that maps to the sum of the int of a single day.
SELECT CAST(Datetimefield AS DATE) as DateField, SUM(intfield) as SumField
FROM MyTable
GROUP BY CAST(Datetimefield AS DATE)
As he didn't specify which version of SQL server he uses (date type isn't available in 2005), one could also use
SELECT CONVERT(VARCHAR(10),date_column,112),SUM(num_col) AS summed
FROM table_name
GROUP BY CONVERT(VARCHAR(10),date_column,112)
I came researching the options that I would have to do this, however, I believe the method I use is the simplest:
SELECT COUNT(*),
DATEADD(dd, DATEDIFF(dd, 0, date_field),0) as dtgroup
FROM TABLE
GROUP BY DATEADD(dd, DATEDIFF(dd, 0, date_field),0)
ORDER BY dtgroup ASC;
-- I like this as the data type and the format remains consistent with a date time data type
;with cte as(
select
cast(utcdate as date) UtcDay, DATEPART(hour, utcdate) UtcHour, count(*) as Counts
from dbo.mytable cd
where utcdate between '2014-01-14' and '2014-01-15'
group by
cast(utcdate as date), DATEPART(hour, utcdate)
)
select dateadd(hour, utchour, cast(utcday as datetime)) as UTCDateHour, Counts
from cte
Personally i prefer the format function, allows you to simply change the date part very easily.
declare #format varchar(100) = 'yyyy/MM/dd'
select
format(the_date,#format),
sum(myfield)
from mytable
group by format(the_date,#format)
order by format(the_date,#format) desc;

sql server query group by item

I have a table with 3 columns,sql server 2008
item, count , date
I would like a query that selects the total count per item for current day with
date colums having the format 2011-03-30 09:00:00.000
select
date,item, sum(count) as total
from
table1
group by
date,item
EDIT:
select item, dateadd(day,datediff(day,0,date),0) as dateInserted, sum(count)
as total from Table1 group by item, dateadd(day,datediff(day,0,date),0) order by 1
the first select returns totals but grouped by the datetime instead of date...then the second returns totals correctly but I find some items appearing twice with the same date but different totals
Sounds like you're looking for 'current day' results?
select item, dateadd(day, datediff(day, 0, date), 0) as dateInserted,
sum(count) as total
from Table1
WHERE
dateadd(day, datediff(day, 0, [date]), 0) =
dateadd(day, datediff(day, 0, getdate()), 0)
group by
item, dateadd(day, datediff(day, 0, date), 0)
order by 1
If you're using SQL Server 2008, use the DATE datatype.
select item,
CAST([date] as DATE)) as dateInserted,
sum(count) as total
from Table1
WHERE
CAST([date] as DATE)) = CAST(getdate() as DATE)
group by
item, CAST([date] as DATE))
order by 1

Resources