Link to home
Start Free TrialLog in
Avatar of tingleweb
tinglewebFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Error converting data type varchar to smalldatetime

I have seen a few answers on the web for this error but I can't seem to get anything to work for me.
Can you help me please?

My output is:
__________________________________________________________________________________
execute sp_insertreminderuser 'jo', 'bloggs', '28/10/1965', 'joe@bloggs.com', 'jo', 1
__________________________________________________________________________________

The SP is:
__________________________________________________________________________________
CREATE PROCEDURE sp_insertreminderuser
  @firstname varchar(50),
  @lastname varchar(50),
  @birthdate smalldatetime,
  @email varchar(100),
  @password varchar(25),
  @sees_public char(1)
 
AS
INSERT INTO tblusers (firstname, lastname, birthdate, email, password, sees_public)
  VALUES (@firstname, @lastname, @birthdate, @email, (convert(varbinary, @password)) , @sees_public)
 SELECT ID = @@IDENTITY
__________________________________________________________________________________

The error is:
__________________________________________________________________________________
Microsoft OLE DB Provider for ODBC Drivers error '80040e07'

[Microsoft][ODBC SQL Server Driver][SQL Server]Error converting data type varchar to smalldatetime.
__________________________________________________________________________________

In the database - the birthdate field is stored as datetime, 8 chars
ASKER CERTIFIED SOLUTION
Avatar of _Maddog_
_Maddog_
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tingleweb

ASKER

This works [YYYYMMDD]

execute sp_insertreminderuser 'jo', 'bloggs', '19651028', 'joe@bloggs.com', 'jo', 1

So I guess I'm really asking how I can insert '28/10/1965' as a date?
And how to make sure that all 8 chars are completed as '1965928' doesnt work but 19650928 does?
CAST(@birthdate as smalldatetime) and CAST(@birthdate as datetime) don't work where the date is less than 8 chars but I'm wondering if its because not all 8 chars are completed.  Example: '1965928' instead of '19650928'
Avatar of leechoonhwee
leechoonhwee

28-Sep-1965 should work.
does it have to 19650928 or can it be 1965928 to work?
ohhh.. didn't notice you used smalldatetime in the sp.

you can use the following suggestion:
https://www.experts-exchange.com/questions/21086240/Date-Format-on-Insert-Stored-Procedure.html
or even use CDate in VBScript:

execute sp_insertreminderuser 'jo', 'bloggs', CDate('28/10/1965'), 'joe@bloggs.com', 'jo', 1
Use this at the top of your script:

SET DATEFORMAT dmy

execute sp_insertreminderuser 'jo', 'bloggs', '28/10/1965', 'joe@bloggs.com', 'jo', 1
I'm still having problems where the date is YYYYMDD rather than YYYYMMDD

For instance-  my SP is

CREATE PROCEDURE sp_insertnewreminder
@userid int,
@name varchar (50),
@occasion int,
@firstdate datetime
AS
INSERT INTO tblreminders (userid, name, remindertypeid, firstdate)
  VALUES (@userid, @name, @occasion, CAST(@firstdate as datetime))
 SELECT ID = @@IDENTITY
GO



and the firstdate variable in this instance = 2004112 [2 November 2004], if I select [30 November 2004] 20041130, it works OK but otherwise I get:


Error converting data type varchar to datetime.

Did you try my solution? It's much simpler.
my sql to update the sp is

      sql = "execute sp_insertnewreminder '" & _
            userid & "', '" & _
              name & "', '" & _
              Occasion & "', '" & _              
            firstdate & "'"


where would SET DATEFORMAT dmy go?  and do I still have to use CAST in the sp?
    sql = "SET DATEFORMAT dmy " & vbcrlf & " execute sp_insertnewreminder '" & _
           userid & "', '" & _
            name & "', '" & _
            Occasion & "', '" & _            
           firstdate & "'"


You will not have to use CAST in your SP. Just make sure the SP data type is SMALLDATETIME. The date '28/10/1965' will be converted automatically.
what if the format is datetime?
>>what if the format is datetime?

It will still work for dates such as '28/10/1965' (it just adds a 12:00:00 for the time)

I'm afraid I've not tested passing a time as well.