The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Databases
> MS SQL Development
|
Numbers to date time...
Discuss Numbers to date time... in the MS SQL Development forum on Dev Shed. Numbers to date time... MS SQL Development forum discussing administration, MS SQL queries, and other MS SQL-related topics. SQL Server is Microsoft's enterprise database engine.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 13th, 2012, 08:58 PM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 31
Time spent in forums: 4 h 42 sec
Reputation Power: 1
|
|
|
Numbers to date time...
Is there any way to have numbers become a datetime? here is my code
declare @month as integer
declare @day as integer --1 or 16
declare @year as integer
declare @datefrom as varchar
set @month = 10
set @day = 1
set @year = 2012
set @datefrom = @month+'/'+@day+'/'+@year
select @datefrom
Ouput should be : 10/01/2012
but i get this error,
Server: Msg 245, Level 16, State 1, Line 10
Syntax error converting the varchar value '/' to a column of data type int.
could somebody help with me please 
|

November 14th, 2012, 03:53 AM
|
|
Contributing User
|
|
Join Date: Jan 2003
Location: Paris Uppland
|
|
Code:
declare @datefrom as varchar(10)
set @month = 10
set @day = 1
set @year = 2012
set @datefrom = right('0' + cast(@month as varchar(2)),2) + '/' +
right('0' + cast(@day as varchar(2)),2) + '/' +
right('000' + cast(@year as varchar(4)),4)
Note that you should specify a maximum length for the @datefrom variable.
You can also do
Code:
set @datefrom = convert(varchar(10),datefromparts(@year,@month,@day),101)
|

November 16th, 2012, 01:32 AM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 31
Time spent in forums: 4 h 42 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by swampBoogie
Code:
declare @datefrom as varchar(10)
set @month = 10
set @day = 1
set @year = 2012
set @datefrom = right('0' + cast(@month as varchar(2)),2) + '/' +
right('0' + cast(@day as varchar(2)),2) + '/' +
right('000' + cast(@year as varchar(4)),4)
Note that you should specify a maximum length for the @datefrom variable.
You can also do
Code:
set @datefrom = convert(varchar(10),datefromparts(@year,@month,@day),101)
|
could i know the complete code? it seems like i'm missing the function datefromparts? have you declared it as a seperate variable? or is it a UDF(User Defined Function)?

|

November 16th, 2012, 03:29 AM
|
|
Contributing User
|
|
Join Date: Jan 2003
Location: Paris Uppland
|
|
|
Datefromparts is a built in function in SQL server. At least it was included in the 2008 release
|

December 2nd, 2012, 07:51 PM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 31
Time spent in forums: 4 h 42 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by swampBoogie Datefromparts is a built in function in SQL server. At least it was included in the 2008 release |
That sir, is what i'm lacking, i'm currently using SQL Server 2000.. is there any way to do this at my current instance?
|

December 3rd, 2012, 03:00 AM
|
|
Contributing User
|
|
Join Date: Jan 2003
Location: Paris Uppland
|
|
|
The first example I posted shall work in SQL Server 2000
|

December 3rd, 2012, 10:51 AM
|
|
|
|
SELECT CONVERT(VARCHAR(10), GETDATE(), 101)
should give you date in mm/dd/yyyy format
|

December 7th, 2012, 12:20 AM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 31
Time spent in forums: 4 h 42 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by swampBoogie The first example I posted shall work in SQL Server 2000 |
That sir, is what i need  thanks sir! 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|