SQL Server 2008 Stored Procedure - sql-server

I cannot store the date data type variables using stored procedure. My code is:
ALTER PROCEDURE [dbo].[Access1Register]
-- Add the parameters for the stored procedure here
#MobileNumber int,
#CitizenName varchar(50),
#Dob char(8),
#VerificationCode int
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
select CAST(#dob As DATE)
Insert Into Access1 (MobileNo,CitizenName,Dob,VerificationCode)
values(#MobileNumber,#CitizenName,#Dob,#VerificationCode)
go
If I exec this procedure it is executing, but there is an error occured in the date type variable. It's raising the error as invalid item '-'.

It depends on how you pass in the #Dob values.
Your best bet is to use the ISO8601 format - 'YYYYMMDD' - since that will always convert to DATE properly - regardless of your language and regional settings on your SQL Server machine.

It depends on the date format that you pass.
If it is 'mm-dd-yy' you can use CONVERT(DATE, #Dob, 110), if it is 'dd-mm-yy' then CONVERT(DATE, #Dob, 105).

In which format you are passing the #Dob values? And what error you are getting exactly?
If you will pass the #Dob values in the format of mm-dd-yy, it should work correctly and no errors will be there.

Related

Error converting data type varchar to datetime. Need to return records based on the date or all records if no date is specified

I have implemented a storedprocedure and basically want the storedprocedure to return all records if no parameter is sent or records based on parameter sent. I am using date as the parameter. I am getting an error when executing exec [dbo].[getLog] '27/07/2017' . The error is
Error converting data type varchar to datetime.
Could somebody tell me what is incorrect in the logic
Create PROCEDURE [dbo].[getLog]
#dateFrom datetime
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT * from [CoreAnalytics].[dbo].[Logs]
where [TimeStamp] > ISNULL(#dateFrom, 0) ;
END
GO
If I understand you correct you want to return all records if #dateFrom is null, or filter on that variable.
I think you need something like this then
Create PROCEDURE [dbo].[getLog] #dateFrom datetime
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT * from [CoreAnalytics].[dbo].[Logs]
where (#dateFrom is null or [TimeStamp] > #dateFrom)
END
Make sure you are passing a valid datetime when calling the procedure.
It is best to use an universal format that will work with any database,
yyyyMMdd is a format that will always work.
http://karaszi.com/the-ultimate-guide-to-the-datetime-datatypes
exec [dbo].[getLog] '20170727'
should always work

Stored procedure time(7) format on insert

i have the ff stored procedure
create procedure InsertDetails(
#name varchar(50),
#lastname varchar(50),
#starttime time(7),
#endtime time(7)
)
AS
BEGIN
insert into table (
name,
lastname,
starttime,
endtime
)
values(
#name,
#lastname ,
#starttime,
#endtime
)END
when the stored procedure is executed i enter the following parameters, however i am not sure for the starttime and end time as to what is the format to enter the time in. i tried
08:00:00 and 08-00-00
but i get an error for both Incorrect syntax near ':'.
can you tell me the format its expecting
There's no syntax for specifying time literals1 - you'll need to give a character literal and have the system convert them to time for you.2
So, it would be '08:00:00', for example.
Of course, if you're executing this stored procedure from some other language, then you should see if there are appropriate bindings to allow you to pass the data across as a time parameter (or local language equivalent) rather than passing it as a string at all.
1Bizarrely, T-SQL calls these Constants, but since just about every other language uses the term literals, I choose to use that term instead.
2You don't have to have the system perform the conversion. You may perform an explicit conversion if you want to, but I usually find that this doesn't add anything to the query, except noise.
I got Why you get this error because you did;t mention it into single quote ' '
you are Doing this exec Like exec InsertDetails 's','s',08:00:00,08:00:00
Try to exec Like exec InsertDetails 's','s','08:00:00','08:00:00'
Error Demo
Working Demo

convert datetime error in SQL Server (2005)?

I have created two functions to work with ISO 8601 dates:
CREATE FUNCTION IPUTILS_STR_TO_ISODATE (
#isostr VARCHAR(30))
RETURNS DATETIME
AS
BEGIN
RETURN CONVERT(DATETIME, #isostr, 126);
END;
GO
CREATE FUNCTION IPUTILS_ISODATE_TO_STR (
#date VARCHAR(30))
RETURNS VARCHAR(30)
AS
BEGIN
DECLARE #result VARCHAR(30);
SET #result = CONVERT(VARCHAR(30), #date, 126);
RETURN #result;
END;
GO
I don't get them working correct for some reason. If I do:
select dbo.IPUTILS_ISODATE_TO_STR(dbo.IPUTILS_STR_TO_ISODATE('1965-04-28T12:47:43'));
I get:
apr 28 1965 12:47PM
instead of:
1965-04-28T12:47:43
if I do:
select convert(VARCHAR(30), dbo.IPUTILS_STR_TO_ISODATE('1965-04-28T12:47:43'), 126);
I get:
1965-04-28T12:47:43
Is this a bug or am I doing something wrong?
Why are you not testing these functions individually first and then in combination? If you do test them individually you will likely see the problem ;-). Check the datatype of the #date input parameter on the IPUTILS_ISODATE_TO_STR function: it is VARCHAR(30) instead of DATETIME.
Having the incorrect datatype for the input parameter means that an implicit conversion from a real DATETIME into VARCHAR, but without a specified "style", is happening as the value comes into the function. This is the same as doing CONVERT(VARCHAR(30), #date). And the result of this implicit conversion (i.e. the value stored in #date) is being sent to the SET #result = CONVERT(VARCHAR(30), #date, 126); line.
Also, I would suggest not doing this in the first place (i.e. creating either of these functions) if they are going to be used in SELECT statements or WHERE clauses. Using the CONVERT() function in those places is repetitive, but also much faster. T-SQL scalar UDFs and Multiline TVFs do not perform well and you can slow down your queries by using them. In this particular case there is no real computation / formula being done so you aren't really gaining much outside of not needing to remember the "style" number. Also, T-SQL functions invalidate the query from getting a parallel execution plan. But if these are just being used in simple SET statements to manipulate a variable that is being used in a query, then that should be fine.

SQL Server stored procedure coming up empty

I have a SQL Server stored procedure that is not returning any data when I plug in my parameter...
ALTER PROCEDURE dbo.EncumBugSearch
#year datetime
AS
BEGIN
SET NOCOUNT ON;
select *
From dbo.BudgetEncumberedTbl as bet
where year(dateadd(month,-3,bet.be_dateposted)) = #year
order by be_dateposted desc
END
GO
This returns nothing, however, when I plug in a number for the parameter (i.e. 2011) then I get the correct results... any ideas?
Your parameter is a datetime, when it should be an int.

What is wrong with this simple SQL? Fails when put in a stored procedure works otherwise

When I ran as is, it works perfectly fine but putting this exact code into a stored procedure in SQL 2005 fails.
I get this error
Msg 102, Level 15, State 1, Procedure GetCurrentLoadDate, Line 23.
Incorrect syntax near '#vardate'.
What is wrong with this call that it can work as a declaration and return the result set but fail if put in stored procedures?
declare #date datetime
declare #vardate varchar(10)
set #date = getDate()
set #vardate = CONVERT(varchar(10), #date ,101)
select tableloaded, insertdatetime, sourcesystemdatetime, FriendlyDescription
from dbo.tbl_loadSourcedates_dttm
where CONVERT(varchar(10), insertdatetime, 101) = #vardate
Thanks
Dhiren
You are probably missing the END at the end of the stored proc definition that you neglected to show us. I get the same error If I append
create proc foo
as
begin
to the beginning of your posted code.
Do you have exactly this?
CREATE PROCEDURE GetCurrentLoadDate
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
declare #date datetime
declare #vardate varchar(10)
set #date=getDate()
set #vardate=CONVERT(varchar(10), #date ,101)
select tableloaded,insertdatetime,sourcesystemdatetime,FriendlyDescription
from dbo.tbl_loadSourcedates_dttm
where CONVERT(varchar(10), insertdatetime ,101)=#vardate
END
GO
Because it looks to me that you are missing something when you declare your proc since you are getting a syntax exception on the very first line.

Resources