CASTing a date

Discussion in 'ASP .Net' started by DavidC, Aug 26, 2010.

  1. DavidC

    DavidC Guest

    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
    DavidC, Aug 26, 2010
    #1
    1. Advertising

  2. DavidC

    Mike Guest

    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" <> a écrit dans le message de
    news:...
    >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, Sep 2, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    1
    Views:
    597
    Matthew Speed
    Nov 8, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,250
    Michael Borgwardt
    May 30, 2004
  3. kevin
    Replies:
    11
    Views:
    5,799
    Andrew McDonagh
    Jan 8, 2005
  4. Wally Barnes
    Replies:
    3
    Views:
    521
    Wally Barnes
    Nov 20, 2008
  5. Sosuke

    Up casting and down casting

    Sosuke, Dec 20, 2009, in forum: C++
    Replies:
    2
    Views:
    560
    James Kanze
    Dec 20, 2009
Loading...

Share This Page