SQL Date parameter for report - sql-server

I have the following table
SELECT product
FROM table
WHERE TRY_CAST(purchased AS Date) >= ’12/02/2021’
AND TRY_CAST(purchased AS Date) <= ’12/09/2021’
Running the query in SQL Server I am only getting 1 row. Date '12/09/2021' is not showing. Any idea how to fix that?

Related

Code required to run a query on the first day of each month

I would like to run a query on a MS SQL Server 2014 on the first day of the month, every month.
Would any of the following be ok?
SELECT *
FROM Table
WHERE CONVERT(VARCHAR(2),GETDATE(),104)=1
or
SELECT *
FROM Table
WHERE DATEPART(d,GETDATE())=1
or
SELECT column_a, MAX(column_b)
FROM Table
WHERE column_c=value
GROUP BY column_a
HAVING DATEPART(d,GETDATE())=1
Tnx

how to calculate all previous datas sum of a sql column on a date range report

I have a crystal report with debit credit columns using a sql command. This report contains a date to date filtering parameters. So the problem is if i filter the report to date range i need all the previous data sum using a sql command.
Select SUM(CAST(debit as DECIMAL(9,2)))- SUM(CAST(credit as DECIMAL(9,2)))
from sum_balance
where sum_date < sum_date
this is my code but i can't get the result from it. (e.g. : if the report starting from 2014-07-01 then i need the sum(debit - credit) of all previous data before 2014-07-01). Can anyone help me to find a solution for this. THe main thing is to add a brought forward balance using sql command on first row. If it is null then it should be 0.00.
Here is your sample table
CREATE TABLE #TEMP(DEBIT NUMERIC(20,2),CREDIT NUMERIC(20,2),DT VARCHAR(20))
INSERT INTO #TEMP
SELECT 1000 DEBIT,500 CREDIT,'2014-11-27' DT
UNION ALL
SELECT 2000 DEBIT,700 CREDIT,'2014-11-28' DT
UNION ALL
SELECT 3000 DEBIT,900 CREDIT,'2014-11-29' DT
I am updating answer for your updated requirement
QUERY 1
This will bring the total till current date, ie, for 2014-11-28 the amount will be (1000+2000)-(500+700), for 2014-11-29 the amount will be (1000+2000+3000)-(500+700+900)
SELECT T2.DEBIT,T2.CREDIT,T2.sum_date,
(SELECT SUM(DEBIT)-SUM(CREDIT) FROM sum_balance WHERE sum_date <= CAST(T2.sum_date AS DATE))
AMOUNT
FROM sum_balance T2
SQL FIDDLE
QUERY 2
This will bring the sum till previous day, that will be opening balance for today's date ie, for 2014-11-29 the amount will be (1000+2000)-(500+700). For easy understanding I have added the previous column also.
;WITH CTE AS
(
SELECT ROW_NUMBER() OVER(ORDER BY CAST(T2.sum_date AS DATE))RNO,
T2.DEBIT,T2.CREDIT,T2.sum_date,
ISNULL((SELECT SUM(DEBIT)-SUM(CREDIT) FROM sum_balance WHERE sum_date <= CAST(T2.sum_date AS DATE)),0)
AMOUNT
FROM sum_balance T2
)
SELECT C1.*,ISNULL(C2.AMOUNT,0) CARRYFORWARD
FROM CTE C1
LEFT JOIN CTE C2 ON C1.RNO=C2.RNO+1
SQL FIDDLE
You can use QUERY 2 and you will get opening balance till previous day in CARRYFORWARD column.
Please leave a message or comment for any changes.
When you need records previous than some date then you need to have that comparision date so that records can be extracted.
Your where clause where sum_date < sum_date won't work this way either you change the right side comparision operator in query or create a parameter in crystal so that user can enter the required end date during run time.
option 1:
E.g: where sum_date < currentdate
option 2:
Create a parameter and declare it in Record Selection Formula in crystal reports so that formed query will be something like
where sum_date < 2014-07-01
You can try this:-
SELECT SUM(CAST(debit as DECIMAL(9,2)))- SUM(CAST(credit as DECIMAL(9,2)))
FROM sum_balance
WHERE sum_date < (Select Max(sum_date) FROM sum_balance)

Getting different SQL Server results for same query

I tried to get data from SELECT query from SQL Server 2008 R2
My query is something like
Select id, name from table1 where date > 2012/8/1
union
Select id, name from table2 where date > 2012/8/1
I am executing this query on about 300K rows of data.
And I am getting different result every time.
Can anyone tell me the reason behind this?
You need to use unambiguous date formats and you need to delimit your dates with strings. Try:
WHERE [date] > '20120801'

Querying a table with a date/time filter in SQL Server 2008

I have a table in my database. This table is called Order. It has a structure like this
ID
CustomerID
OrderDate
Quantity
Price
I need to get all of the orders for the past 2 weeks. How do I do that? I don't understand how to work with dates in this manner in SQL.
Thank you!
You could incorporate something like this into your WHERE clause:
WHERE OrderDate >= DATEADD(day,-14,GetDate())
(i.e OrderDate is more recent than today minus 14 days.)
[I don't have access to SQL Server here so I can't check it - but it might work :)]
Edit: Depending on the exact datatype of OrderDate, I'm not sure what will happen in cases where you have e.g. an order half way through the day two weeks ago, so you might want to check what happens.
marnir answer is the way to do it but this is another option excluding OrderDate > today:
select * from Order
where [OrderDate]
BETWEEN DATEADD(dd, -14, GetDate()) AND GetDate()
Here is another possible way of retrieving orders placed in the last 2 weeks. This is assuming that OrderDate is a column of data type datetime. Screenshot #1 shows sample data in a table named dbo.[Order] similar to your requirements and output of below query against that table data. This query was tested in SQL Server 2008 R2 but is compatible with other SQL Server versions as well.
SELECT Id
, CustomerId
, OrderDate
, Quantity
, Price
FROM dbo.[Order]
WHERE DATEDIFF(WEEK, OrderDate, GETDATE()) <= 2
Hope that helps.
Screenshot #1

10 Month average

I'm replacing an Excel spreadsheet with a Microsoft SQL Server 2008 database and need to calculate this:
=AVERAGE(IF((A:A>A4-304)*(A:A<=A4),G:G))
Where column A is the date and column G is the value.
The heading for this field is:
10 month simple moving average
I suppose I will use something like:
SELECT RawData.*
,(SELECT Sum(X.AdjClose) AS SumAdjClose
FROM RawData X
WHERE DATEDIFF(d,X.RawDate,RawData.RawDate) <= 304
) AS SumAdjClose
FROM RawData
In SQL Server one option is to make the filters in the WHERE clause and the average in the SELECT
select avg(ValueCol)
from Table1
where DateCol between #Date-304 and #Date
Another option, in the same logic of the excel could be:
select avg(case when DateCol between #Date-304 and #Date then ValueCol else null)
from Table1

Resources