MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 13th, 2012, 08:58 PM
narrokk narrokk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 31 narrokk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old November 14th, 2012, 03:53 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 2,349 swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 4 Days 7 h 27 m 6 sec
Reputation Power: 390
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)

Reply With Quote
  #3  
Old November 16th, 2012, 01:32 AM
narrokk narrokk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 31 narrokk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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)?


Reply With Quote
  #4  
Old November 16th, 2012, 03:29 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 2,349 swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 4 Days 7 h 27 m 6 sec
Reputation Power: 390
Datefromparts is a built in function in SQL server. At least it was included in the 2008 release

Reply With Quote
  #5  
Old December 2nd, 2012, 07:51 PM
narrokk narrokk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 31 narrokk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #6  
Old December 3rd, 2012, 03:00 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 2,349 swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 4 Days 7 h 27 m 6 sec
Reputation Power: 390
The first example I posted shall work in SQL Server 2000

Reply With Quote
  #7  
Old December 3rd, 2012, 10:51 AM
gk53 gk53 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 71 gk53 User rank is Sergeant (500 - 2000 Reputation Level)gk53 User rank is Sergeant (500 - 2000 Reputation Level)gk53 User rank is Sergeant (500 - 2000 Reputation Level)gk53 User rank is Sergeant (500 - 2000 Reputation Level)gk53 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 8 h 28 m 25 sec
Reputation Power: 8
SELECT CONVERT(VARCHAR(10), GETDATE(), 101)
should give you date in mm/dd/yyyy format

Reply With Quote
  #8  
Old December 7th, 2012, 12:20 AM
narrokk narrokk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 31 narrokk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Numbers to date time...

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap