SELECT COUNT(DISTINCT DT) FROM Event
SELECTConvert(Varchar,DT,101),Count(*))FROM EventGroup byConvert(Varchar,DT,101)|||
SELECTCOUNT(DISTINCTDAY(DT)+' /'+MONTH(DT)+' /'+YEAR(DT))FROMEvent
SELECT COUNT(DISTINCT DT) FROM Event
SELECTConvert(Varchar,DT,101),Count(*))FROM EventGroup byConvert(Varchar,DT,101)|||
SELECTCOUNT(DISTINCTDAY(DT)+' /'+MONTH(DT)+' /'+YEAR(DT))FROMEvent
CREATE PROCEDURE sp_GetContactScheduleDates
@.MonthFrom int,
@.YearFrom int,
@.MonthTo int,
@.YearTo int,
@.DaysInMonth int
AS
Select distinct s.ScheduleMonth, s.ScheduleYear
From OnCall_Schedules s
Where CAST(cast(s.ScheduleMonth as nvarchar) + '/' + cast(s.ScheduleDate as nvarchar) + '/' + cast(s.ScheduleYear as nvarchar) as smalldatetime)
>= CAST(cast(@.MonthFrom as nvarchar) + '/' + cast('01' as nvarchar) + '/' + cast(@.YearFrom as nvarchar) as smalldatetime)
And CAST(cast(s.ScheduleMonth as nvarchar) + '/' + cast(s.ScheduleDate as nvarchar) + '/' + cast(s.ScheduleYear as nvarchar) as smalldatetime)
<= CAST(cast(@.MonthTo as nvarchar) + '/' + cast(@.DaysInMonth as nvarchar) + '/' + cast(@.YearTo as nvarchar) as smalldatetime)
Order by s.ScheduleYear, s.ScheduleMonth
GO
However, this only brings back those dates that are in the table. I need to get ALL dates within the range.
For example, the OnCall_Schedules table contains schedules that are saved by the user. If no one has ever saved a schedule at any time in May 2004 and the range of dates entered is January 2004 to June 2004, then May 2004 will not be returned. I need to get back all dates within that range regardless if it has something scheduled or not. How can this be done?
Note - I do not want to set up any dummy records or create a table with valid dates as the user will be allowed to choose any range of dates and we do not want to have to maintain anything.
Can some sort of function be used? What would the code look like?I would create a table variable with one field that will hold the date. The do a loop to populate it. I'd make sure @.startdate and @.enddate have the time stripped off. Not tested, but should work with minor tweaks.
|||ooo that's a nice loop. :)
set @.date = @.startdate
set @.x = datediff(d, @.startdate, @.enddate)
set @.y = 0
While @.y <= @.x
Begin
insert into @.table (datefield) values (dateadd(d, @.y, @.startdate))
set @.y = @.y + 1
End
This is more ASP SElect .
I need to return all the rows. Where the ESNnumber only returns the most recent one that is associsted with the Asset.
Basically, I need the info most current ESN number only.
They are 19,00 rows of each ESN number but it returns 40,000. Duplicates.
SELECT TOP (100) PERCENT dbo.AssetType.Description, dbo.AssetCustomAttributeDef.Name AS [Custom Asset], dbo.ESN.EsnNumber AS [ESN #],
dbo.AssetAttribute.AssetDescription AS [Description Detail], dbo.Asset.Barcode, dbo.Asset.SKU,
dbo.InventoryOrigin.WarehouseDescription AS [Inventory (W/H)], dbo.ESN.DateImplemented, dbo.ESNTracking.TraceTime,
dbo.ESNTracking.PreviousTraceTime, dbo.ESNTracking.HasMoved, dbo.ESNTracking.DistanceMiles, dbo.ESNTracking.Direction,
dbo.ESNTracking.Landmark, dbo.ESNTracking.FemaLocation AS Fema, dbo.ESNTracking.ReportTime AS [Report Time],
dbo.ESNTracking.CurrLocStreet AS Address, dbo.ESNTracking.CurrLocCity AS City, dbo.ESNTracking.CurrLocState AS State,
dbo.ESNTracking.CurrLocZip AS Zipcode, dbo.ESNTracking.CurrLocCounty AS County, dbo.ESNTracking.MapUrl AS [Map Link],
dbo.ESNTracking.ReplaceByDate AS [Replace Batt.], dbo.ESNTracking.CurrMileFromStratix AS [From Stratix Now],
dbo.ESNTracking.PrevMileFromStratix AS [From STratix Then]
FROM dbo.AssetType INNER JOIN
dbo.Asset ON dbo.AssetType.AssetTypeId = dbo.Asset.AssetTypeId INNER JOIN
dbo.InventoryOrigin ON dbo.Asset.WarehouseId = dbo.InventoryOrigin.WarehouseId INNER JOIN
dbo.AssetAttribute ON dbo.Asset.AssetAttributeId = dbo.AssetAttribute.AssetAttributeId INNER JOIN
dbo.EsnAsset ON dbo.Asset.AssetId = dbo.EsnAsset.AssetId INNER JOIN
dbo.ESN ON dbo.EsnAsset.EsnId = dbo.ESN.EsnId LEFT OUTER JOIN
dbo.ESNTracking ON dbo.EsnAsset.EsnId = dbo.ESNTracking.EsnId LEFT OUTER JOIN
dbo.AssetVehicle ON dbo.EsnAsset.AssetId = dbo.AssetVehicle.AssetId LEFT OUTER JOIN
dbo.AssetCustomAttribute ON dbo.EsnAsset.AssetId = dbo.AssetCustomAttribute.AssetId LEFT OUTER JOIN
dbo.AssetCustomAttributeDef ON dbo.AssetCustomAttribute.AssetTypeId = dbo.AssetCustomAttributeDef.AssetTypeId
ORDER BY dbo.AssetType.Description
If I have understood this correctly...
1. Drop a MULTICAST into your flow.
2. On one output, use AGGREGATE to work out the max ESN Number per asset.
3. Join that back to the other output using MERGE JOIN, joining on ESN Number and Asset
Does that work?
-Jamie
|||Could you help me please.
I need help.
writiing the query with thos parameters
I am writing a C# application that uses a SQL server database to hold its data. I need to create a stored procedure that returns a particular row's primary key value. This is no problem if the primary key is an INT. But my primary key is a unique identifier, and the stored procedure doesn't want to let me return any values that aren't INTs. Can someone please tell me how to get around this?
Thanks in advance.
ScottYou'll have to declare it as an outparameter.
And if you want something easier to handle you can convert it to
a varchar using CONVERT(myguid,VARCHAR)
Regards
Fredr!k|||Fredrik2000,
Thank you so much. That is exactly what I needed. Also, for anyone else out there, it is actually in the format:
CONVERT(VARCHAR(36), myguid)
where of course 36 is the number of characters allocated for the datatype.
Sc0tt|||Ahh, I always get the order mixed up (didn't have a copy of books online at the computer
I'm posting from...)
Nice to hear you got it working.
Regards
Fredr!k
hi leonardo ,
try this
select columns from tablename where convert(varchar, column_with_date_datatype, 103) = convert(varchar,getdate(),103)
hope it helps
regards,
satish
|||Hi Leo,
U can also use the split function to receive the date.
I think the now.tostring gives date<space>time
so,
dim dat() as string
dat=split(now.tostring)
Msgbox(dat(0).tostring)
dat(1)-->time
-PSK
|||Thanks for the help
but what i want is to build up a query in the SQL Server that returns today's date in SQL Statment
Select JoinDate
From Employee
Where **********;
how to have a result for a today's date , a week before and a month before ...
|||you could use
dateadd() function in that case, for more help refer to sql books online.
thanks,
satish.
|||You could get the start of the day like so DATEADD(day, DATEDIFF(day, 0, GetDate()), 0)..
This means you would do something like...
SELECT * FROM table WHERE Created >= DATEADD(day, DATEDIFF(day, 0, GetDate()), 0)
Enjoy, Steve
Hi.
I have a stored procedure "sp1" which returns a value (with the sql statement Return @.ReturnValue).
Is it possible for my asp.net page to retrieve this return value, and to do it declaratively (meaning without writing code to connect to the database in the code behind). If it is possible to do it like this please tell me how, and if not please tell me how to do it with code.
Thanks in advance .
i do not know what will you sp return but i suppose that it is and INT
so you write this way;
int retrunvalue=sqlcommad.excutenonequery();
so the returned value will be passed to you int.
hope this will help
|||this is sample code, it can help you:
Here is a sample sproc that populates output parameters
from the Northwind Products table:
CREATE PROCEDURE CustOrderOne
@.CustomerID nchar(5),
@.ProductName varchar(50) output,
@.Quantity int output
AS
SELECT TOP 1 @.ProductName=PRODUCTNAME, @.Quantity =quantity
FROM Products P, [Order Details] OD, Orders O, Customers C
WHERE C.CustomerID = @.CustomerID
AND C.CustomerID = O.CustomerID AND O.OrderID = OD.OrderID AND OD.ProductID = P.ProductID
And here is an example of some C# code to return and display the output parameters:
using System;
using System.Data;
using System.Data.SqlClient;
namespace OutPutParms
{
class OutputParams
{
[STAThread]
static void Main(string[] args)
{
using(SqlConnection cn = new SqlConnection("server=(local);Database=Northwind;user id=sa;password=;"))
{
SqlCommand cmd = new SqlCommand("CustOrderOne", cn);
cmd.CommandType=CommandType.StoredProcedure ;
SqlParameter parm=new SqlParameter("@.CustomerID",SqlDbType.NChar) ;
parm.Value="ALFKI";
parm.Direction =ParameterDirection.Input ;
cmd.Parameters.Add(parm);
SqlParameter parm2=new SqlParameter("@.ProductName",SqlDbType.VarChar);
parm2.Size=50;
parm2.Direction=ParameterDirection.Output;
cmd.Parameters.Add(parm2);
SqlParameter parm3=new SqlParameter("@.Quantity",SqlDbType.Int);
parm3.Direction=ParameterDirection.Output;
cmd.Parameters.Add(parm3);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
Console.WriteLine(cmd.Parameters["@.ProductName"].Value);
Console.WriteLine(cmd.Parameters["@.Quantity"].Value.ToString());
Console.ReadLine();
}
}
}
}
The above 2 replies does not actually get the return value, which is a special parameter.
The first reply returns the row affected count and the second reply just gets the value out output parameters.
I am afraid I do not know how to retrieve the return value declaratively using controls like object data sources.
However of you are familiar with using SqlCommands then the following code shows you how to get the return values from stored procedures assuming your stored procedure is returning values which is different to result sets, row counts, and output parameters.
SqlCommand cmd =new SqlCommand("this is the query", connection);//create a parameter for the return valueSqlParameter param =new SqlParameter();param.Direction = ParameterDirection.ReturnValue;param.ParameterName ="returnValue";//add to parameter to collectioncmd.Parameters.Add(param);//execute commandcmd.ExecuteNonQuery();//get the return valueint retVal =int.Parse(cmd.Parameters["returnValue"].Value.ToString);
I have set this parameters direction to output, however, when i run it I get an error saying procedure is expecting this output parameter..
Not sure where I am going wrong...
Can someone please help with retrieving the ID after an insert. What is the correct code in .NET?
Many ThanksShow your SP, and how you call it. You can get info like htis back as an output parameter, or as a return code. Absent knowing what you are doing, it is impossible to help.|||This is just a suggestion.
First define the input parameters and assign its values.
'Define the output parameter
With cmd.Parameters.Add("@.outputField", SqlDbType.Int)
.Direction = ParameterDirection.Output
End With
'Read the result
Dim dr As SqlDataReader = cmd.ExecuteReader
Dim returnId As Integer
If dr.Read Then
returnId = dr("OutputField")
End If
dr.Close()
My cmd is the reference to the SqlCommand.
OutputField – Field which you want to return.
Hope this might give you some help.|||Use Scope_Identity() instead of @.@.Identity.
Also..
cmd.ExecuteNonReader()
returnID = cmd.Parameters("@.outputField").Value
cmd.Dipose()
I am using SQLHelper to run a Stored Procedure.
The Stored Procedure returns 3 variables:
ie:
SELECT @.Hits = COUNT(DISTINCT StatID) FROM Stats
The SP is currently called as below:
SqlParameter[] sqlParams = new SqlParameter[]
{
new SqlParameter("@.FromDate", Request["FromDate"]),
new SqlParameter("@.ToDate", Request["ToDate"]),
};
My question is this: How do I retrieve these variables?
I know I need to declare a new SqlParameter and change it's Direction to Output but I am unsure of the exact syntax required to do this.
Thanks in advance,
PeteHi.
Could u please expain how you are returning the values.. all 3 as output parameter?
as the code will depend on the way the values are returned..|||my full Stored Procedure looks as so:
CREATE PROC GetGeneralStats
@.FromDate smalldatetime,
@.ToDate smalldatetimeAS
Declare @.uniqueHits int
Declare @.noOfSearches intSELECT @.uniqueHits = COUNT(DISTINCT StatID) FROM Stats
WHERE (StatDate >= @.FromDate) AND (StatDate <= @.ToDate)-- No of times Searchresults Shown
SELECT @.NoOfSearches = COUNT(SectionID) FROM Stats
WHERE (StatDate >= @.FromDate) AND (StatDate <= @.ToDate)
AND SectionID = 3Go
Cheers,
Pete|||This procedure is retieving and storing values in 2 variables and returning nothing.
you need to add 2 output parameters to the procedure..
CREATE PROC GetGeneralStats|||Hi,@.FromDate smalldatetime,
@.ToDate smalldatetime,
@.uniqueHits int OUTPUT,
@.noOfSearches int OUTPUT
AS
SELECT @.uniqueHits = COUNT(DISTINCT StatID) FROM Stats
WHERE (StatDate >= @.FromDate) AND (StatDate <= @.ToDate)
-- No of times Searchresults Shown
SELECT @.NoOfSearches = COUNT(SectionID) FROM Stats
WHERE (StatDate >= @.FromDate) AND (StatDate <= @.ToDate)
AND SectionID = 3
Thanks for the reply.
As you said (and now having looked in the documentation) this is how it works but....
Im getting
Procedure 'GetGeneralStats' expects parameter '@.uniqueHits', which was not supplied.
So its thinking that I want them as Input variables? Even though I have them as:
@.uniqueHits int OUTPUT,
@.noOfSearches int OUTPUTAS
pete|||Arrgghhh!!! Everywhere I look Im told to do it this way but Im getting this stupid error message!
Im tearing my hair out over this!!!!!!!