PC Review


Reply
Thread Tools Rate Thread

CASTing a date

 
 
DavidC
Guest
Posts: n/a
 
      26th Aug 2010
I have the following DECLARE that works fine but I need to handle times when
the day of a date is not = 1 and @Month passed is less than 10 so the date
gets '0x' for month or day. Below is my current CAST.

DECLARE @StartMoth date;
SET @StartMonth = CAST(CAST(@Year as char(4)) + CAST(@Month as char(2)) +
'01');


Thanks.
--
David
 
Reply With Quote
 
 
 
 
Mike
Guest
Posts: n/a
 
      2nd Sep 2010
Hi David,

I think it would be easier if you had one parameter of datetime. A datetime
(or date) could be converted directly to the format that you want with
convert.

With a single date(time) parameter you could do the following
--- this just to make as if I had a parameter of datetime
declare @myParameter datetime
set @myParameter = getdate()
--- end code for make as if I had a parameter
select convert(varchar, @d, 112) MyDate

the response is
MyDate
--------
20100902

In this example I build a date from integers and then cast it with convert
to get the format that you want. Note that I used varchars instead of chars
as you did.

declare @MyDay int = 5
declare @MyMonth int = 9
declare @MyYear int = 10
declare @MyDateStr varchar(10)
declare @MyDate datetime
set @MydateStr = cast(@MyYear as varchar(4)) + '/' + CAST(@MyMonth as
varchar(2)) + '/' + CAST(@MyDay as varchar(2))
set @MyDate = CONVERT(datetime, @MydateStr, 11)
select convert(varchar, @MyDate, 112) MyDate

Mike
http://www.homemadepride.com

"DavidC" <(E-Mail Removed)> a écrit dans le message de
news:3F00F204-619F-42AE-81AD-(E-Mail Removed)...
>I have the following DECLARE that works fine but I need to handle times
>when
> the day of a date is not = 1 and @Month passed is less than 10 so the date
> gets '0x' for month or day. Below is my current CAST.
>
> DECLARE @StartMoth date;
> SET @StartMonth = CAST(CAST(@Year as char(4)) + CAST(@Month as char(2)) +
> '01');
>
>
> Thanks.
> --
> David


 
Reply With Quote
Reply

Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Down-casting of Typed Collection (Casting Generic Types?) conchur Microsoft C# .NET 1 3rd Jul 2006 11:45 AM
Casting to Date type =?Utf-8?B?RGF2aWQgQ2xlYXZl?= Microsoft Access Form Coding 2 18th Apr 2005 06:41 PM
Validate Date before casting Martin Microsoft C# .NET 3 21st Mar 2005 09:56 PM
Casting to date Rob Bradford Microsoft VB .NET 3 5th Oct 2004 02:30 PM
Date Time Casting Problem ed Microsoft VB .NET 2 4th Dec 2003 11:32 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:36 AM.