Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Old Forums
 CLOSED - General SQL Server
 conversion of char data type to smalldatetime data

Author  Topic 

jasraja
Starting Member

22 Posts

Posted - 2006-10-26 : 06:27:07

Hi All,

In MS-SQL server, I am executing a SQL task in a DTS package. This SQL task runs perfectly on one server but the same code fails on the other one prompting with this error message :

"conversion of char data type to smalldatetime data type resulted in an out-of-range smalldatetime value".


The Code in the SQL Task is :

DECLARE @fileDate VARCHAR(10)
DECLARE @timeID INT
DECLARE @msg VARCHAR(100)

--Extract the file date from the file
SELECT
@fileDate = File_Date
FROM
Temp_Sales_Data
WHERE
RTRIM(RR_ID) = 'SALES ANALYSIS FOR'

--format the value
SET @fileDate = SUBSTRING(@fileDate, 5, 4) + '/' + SUBSTRING(@fileDate, 3, 2) + '/' + SUBSTRING(@fileDate, 1,2)

--Select the approp. Time_ID
SELECT @timeID = Time_ID FROM [Time] WHERE [Date] = convert(varchar(10), convert(smalldatetime, @fileDate), 20)

--Insert the Time_ID
UPDATE Temp_Sales_Data
SET Time_ID = @timeID


--Delete the header and footer records from the file
DELETE FROM Temp_Sales_Data
WHERE RTRIM(RR_ID) IN ('SALES ANALYSIS FOR', 'END')

--Check that the Time_ID has been assigned

SELECT * FROM Temp_Sales_Data WHERE Time_ID IS NULL

IF @@ROWCOUNT > 0
BEGIN
SELECT @msg = 'Time_ID not set correctly. Check that there is a suitable entry in the Time table'
RAISERROR(@msg, 16, 1)
END


The statement that creates the problem is :
SELECT @timeID = Time_ID FROM [Time] WHERE [Date] = convert(varchar(10), convert(smalldatetime, @fileDate), 20)


Does any one have a clue why this statment works perfectly on one server while fails on the other one ?

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-10-26 : 06:34:03
Check the regional settings (date/time) on both the server, that may be the culprit.

Harsh Athalye
India.
"Nothing is Impossible"
Go to Top of Page

jasraja
Starting Member

22 Posts

Posted - 2006-10-26 : 06:49:24
Well I have checked the Regional Date/Time settings too and they are same. Even I registered Both the servers on a single comp and executed the package but all in vain.
Plz help!!!
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-10-26 : 07:19:36
Run the following command on both server and check the value for option "dateformat".

EDIT:
dbcc useroptions


Sorry... my mistake !

Harsh Athalye
India.
"Nothing is Impossible"
Go to Top of Page

jasraja
Starting Member

22 Posts

Posted - 2006-10-26 : 07:35:10
I think you forgot to mention the command.
Go to Top of Page

jasraja
Starting Member

22 Posts

Posted - 2006-10-26 : 07:45:51
when I run the command dbcc useroptions
it displays me "mdy" for dateformat on one server and
"dmy" for dateformat on the other server.

Do I need to change the dateformat somewhere ?
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-10-26 : 07:48:01
Try setting same dateformat on both servers like:

set dateformat dmy


or

set dateformat mdy


Harsh Athalye
India.
"Nothing is Impossible"
Go to Top of Page

jasraja
Starting Member

22 Posts

Posted - 2006-10-26 : 08:08:21
When I execute "set dateformat mdy" on query analyzer, it does change the value for dateformat but the effect is temporary i.e. when I close the query analyser and open it again the effect is gone.

Even when I changed it in query analyzer and executed my DTS package the effect was not there, so I had to add the extra line "set dateformat mdy". This eventually solved the problem.
Thanks Dude!!!

Is there any way i can change the dateformat permanently ?
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-10-26 : 08:28:17
what is the date format for @fileDate ?


KH

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-10-26 : 08:30:07
quote:
SELECT @timeID = Time_ID FROM [Time] WHERE [Date] = convert(varchar(10), convert(smalldatetime, @fileDate), 20)


Why are you converting from varchar to smalldatetime and then back to varchar again ? What is the data type for column [Date] ?


KH

Go to Top of Page

jasraja
Starting Member

22 Posts

Posted - 2006-10-26 : 08:35:46
Its yyyy/mm/dd
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-10-26 : 08:40:34
quote:
Originally posted by jasraja

Its yyyy/mm/dd



then use
convert(smalldatetime, @fileDate, 111)



KH

Go to Top of Page

jasraja
Starting Member

22 Posts

Posted - 2006-10-26 : 08:49:47
that doesn't solves my problem. I still get the same error.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-10-26 : 08:57:02
Check your data. Make sure the date you form into @fileDate is of valid format and range


KH

Go to Top of Page

jasraja
Starting Member

22 Posts

Posted - 2006-10-26 : 09:13:27
The date is valid. As I quoted before, my code runs perfectly on one server but fails on other.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-10-26 : 09:19:31
you have not answer my the other quesions


KH

Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-10-26 : 09:22:30
quote:
Is there any way i can change the dateformat permanently ?


The effect of SET DATEFORMAT only applies and last for the connection in which it is fired.

The default dateformat depends on the default language selected for the server. If you permanently want to change the setting to say british date format (dmy), you can change the server's default language using sp_configure or EM.

Harsh Athalye
India.
"Nothing is Impossible"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-10-26 : 09:34:52
Dont worry on how dates are stored in sql server table. It is front end where youu need to show the date with different formats. When sending date to table, use universal format YYYYMMDD. To know how to query on dates, refer this
http://sql-server-performance.com/fk_datetime.asp

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

jasraja
Starting Member

22 Posts

Posted - 2006-10-27 : 01:25:05
Thanks Harsh and Madhivanan !!!
Go to Top of Page
   

- Advertisement -