adDBTimeStamp - Access VBA to SQL Server Stored Procedure - sql

I am looking to pass a date variable from Access to a SQL Server stored procedure. Please see my code:
VBA:
searchDate = 03/07/2014
cmd.Parameters.Append _
cmd.CreateParameter("#searchDate", adDBTimeStamp, adParamInput, , searchDate)
searchDate is a date formatted as MM/DD/YYYY
SQL:
ALTER PROCEDURE [dbo].[spAppendActivity]
#searchDate as datetime,
AS
SET NOCOUNT ON;
delete * from tbl_activity_losses;
select [Date]
into tbl_activity_losses
from tbl_master_rec
where [Date] = #searchDate
I get an ODBC error stating
[Microsoft][ODBC SQL Server Driver] Conversion failed when converting date and/or time from character string.
Any help on how to pass this date field to my stored procedure would be greatly appreciated.

The problem is adDBTimeStamp. It is converting your simple date string into a timestamp. (NOT a date)
When that timestamp is passed to your stored procedure, SQL server has no idea how to turn that long string into a date.
Changing the type from adDBTimeStamp to adVarChar will keep it as a string and SQL Server will know how to parse it properly.
Additionally, you can change your stored procedure type from DateTime to varchar as well. SQL Server will know how to parse that into a date.

Related

GetUtcDate in insert statement in SQL Server

I need to have the current date and time inserted into SQL Server.
I tried the following method but it didn't work.
insertstatement = "insert into tablename(sqldate) values (?)"
values = ('getutcdate()')
cursor.execute(insertstatement , values)
cnxn.commit()
I am getting the following error:
[Microsoft][SQL Server Native Client 11.0][SQL Server]Conversion failed when converting date and/or time from character string. (241) (SQLExecDirectW)')
I need to use the SQL Server time only because the code is getting executed at different servers and the timezones and need to use a standard time accurate to milliseconds.
As you are trying to insert SQL datetime, then the time should be got at SQL Server, so your code should looks like this
insertstatement = "insert into tablename(sqldate) values (getutcdate())"
cur.execute(insertstatement)
cnxn.commit()
Try to use GETUTCDATE() function. Let me show an example:
insert into tablename(sqldate) values (GETUTCDATE())
In addition, your type of table column should be DateTime. For example:
DECLARE #FooTable TABLE
(
StartDate DateTime
)
INSERT INTO #FooTable
(
StartDate,
)
VALUES
(
GETUTCDATE(),
)
SELECT StartDate FROM #FooTable
MSDN has a great description about GETUTCDATE() and possible use cases.

Implicit conversion from data type varbinary to date SQL server

I have a procedure which takes a date value as parameter and then inserts the date value in a table:
CREATE PROCEDURE dbo.procInsert
#employed_on DATE
AS
BEGIN
INSERT INTO dbo.TBL(EMPLOYED_ON)
VALUES(#employed_on)
END
however i am getting this error:
Implicit conversion from data type varbinary to date is not allowed.
Use the CONVERT function to run this query.
I tried to use convert but its not working.
UPDATE
i found my mistake. i swapped the variables for insert.
i found my mistake. i swapped the variables for insert.

Error converting data type nvarchar to datetime with stored procedure

[Using SQL Server 2008 R2 Enterprise x64 SP1]
I am trying to use some form of GETDATE() to pass today's date to a stored procedure inside OPENQUERY(), but I keep getting the error
Msg 8114, Level 16, State 1, Procedure spCalcProjection, Line 0
Error converting data type nvarchar to datetime
Here is the code (spCalcProjection takes a datetime):
SELECT top 1 multi FROM OPENQUERY([production], 'exec proddb.dbo.spCalcProjection "GETDATE()"')
If I use 2014-05-22 or any literal in place of GETDATE() then I have no problem and get the correct, expected result. If I use some other functionality like CAST(GETDATE() AS DATE) or CONVERT(varchar, GETDATE(), 112) then I get the above error again.
Conrad, posting original syntax error for GETDATE() without double quotes could help more than you think. I also don't see why would you need to escape the function here. (Sorry, can't add to your thread with Lamak, not enough reputation for comments). Also, why do you need an open query to call your sp? When you say SQL Server 2008 R2, is it both on the calling side and on your [production] server? If the other end is not SQL Server it might not have GETDATE() function. If the other end is SQL Server you don't need OpenQuery.
[UPDATE]
I think I have your answer. You cannot use a function as a parameter for stored procedure. Has nothing to do with open query. What you can do, you can replace that stored procedure with table-valued function. I just tried it and it worked.
Definition:
CREATE FUNCTION TestFun
(
#TestDateParam datetime
)
RETURNS
#RetTable TABLE
(
Line nvarchar(20)
)
AS
BEGIN
INSERT INTO #RetTable
SELECT aString
FROM sometable
WHERE aDate = #TestDateParam
RETURN
END
Call:
SELECT *
FROM dbname.dbo.TestFun(GETDATE())
Got an answer from elsewhere that I will use:
SELECT top 1 multi FROM OPENQUERY([production], 'DECLARE #dt datetime SELECT #dt = GETDATE() exec proddb.dbo.spCalcProjection #dt')
This avoids having to create any additional objects in the db.

how to store date and pass values(date) in sql server

i storing date in db as below code
convert(varchar(10),GETDATE(),105)
now i need to search values
ALTER PROCEDURE spr_tb_sales
#to_date date,
#from_date date
AS
BEGIN
SELECT *
FROM tb_sales_
WHERE [Sales Date] BETWEEN #from_date AND #to_date
END
If I pass in the values 01-01-2014 and 10-01-2014 it's showing error
Incorrect syntax near '-'.
where i made error.help me
You are storing date as a varchar field. This doesn't seem to be a good idea and I believe between won't work on that. You should store date using the datetime type. Let SQL decide how to store it and you can format it as you wish while displaying.
I think from what you are saying it seems like you are passing the dates in incorrectly. when you call your stored procedure, it should look like this:
exec spr_tb_sales '01-01-2014', '10-01-2014'
I think you might be missing the quotes around your dates which would give you the error that '-' is not correct syntax.

Problem In get dates between 2 date (vb.net | OLE)

I have an data base (.mdb) and it has a column with dates (dd/mm/yy) , some one give me a code to get all the dates in database between 2 dates , the code was :
Select * from table where date between 'StartDate' and 'EndDate'
but after I use the code , an error occurs told me that the types of data is not the same
System.Data.OleDb.OleDbException was
unhandled ErrorCode=-2147217913
Message="عدم تطابق نوع البيانات في
تعبير المعايير." Source="Microsoft
JET Database Engine"
although I convert the data type in the column of dates in database to (Date \ time) , and use OLE object to connect to data base
what is wrong , and what I have to do ?
You usually need to surround date/time types with # when working with Access, like so
#22/01/2009#
Instead of using dynamic SQL, instead use the Access Database Engine's CREATE PROCEDURE SQL DDL syntax to create a persisted object whose parameters have parameters strongly-typed as DATETIME with the NULL value as default. Handle the NULL value to use the DATETIME column's value instead e.g.
CREATE PROCEDURE GetStuff
(
arg_start_date DATETIME = NULL,
arg_end_date DATETIME = NULL
)
AS
SELECT lastvstart
FROM tb
WHERE lastvstart
BETWEEN IIF(arg_start_date IS NULL, lastvstart, arg_start_date)
AND IIF(arg_end_date IS NULL, lastvstart, arg_end_date);