Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting INT to DATETIME 3

Status
Not open for further replies.

KizMar

Technical User
Mar 13, 2002
52
US
How would you convert an 8 digit int '20020912' (represents a date) into a datetime data type?
 
declare @v1 int
set @v1 = 20020912

select convert(datetime,CONVERT(VARCHAR(20),@V1))

OR

select convert(datetime,CONVERT(VARCHAR(20),20001201))
 
see the BOL for CONVERT.

Try this:

SELECT CONVERT(DATETIME, '20020912')

-SQLBill
 
Still getting Syntax errors. Here are more specifics on what I'm trying to do:

SQL Statement:
Declare @String2 Table (date2 datetime)

Insert @String2

Select convert(datetime, (convert(char(8), w.ActiveDate)))

From DateTable w


Note: ActiveDate (int, Null)

The error I'm getting:
Server: Msg 241, Level 16, State 1, Line 6
Syntax error converting datetime from character string.
 
Are you absolutely sure that all the values in the field will convert to a valid date? Usually when people don;t use datetime fields for dates, the users enter incorrect information in the database. You might have one or more values that says something like 20020230 which is acceptable integer data but is not a real date.
 
But the syntax error is not the same when user insert 20020230 or 20020350

If the error is because of this kind of junk insert,it should return

Server: Msg 242, Level 16, State 3, Line 1
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
The statement has been terminated.
-------------------------------------------------------
I think the query KizMar wrote is wrong.You should check the correct syntax
 
Very good point... That was the problem, there were invalid fields. THANKS!!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top