Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   CASTing a date (http://www.velocityreviews.com/forums/t731880-casting-a-date.html)

DavidC 08-26-2010 10:09 PM

CASTing a date
 
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

Mike 09-02-2010 08:15 PM

Re: CASTing a date
 
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" <dlchase@lifetimeinc.com> a écrit dans le message de
news:3F00F204-619F-42AE-81AD-4EDBF4A50244@microsoft.com...
>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




All times are GMT. The time now is 09:14 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57