Solved

Day month and year format.

Posted on 2006-11-23
16
175 Views
Last Modified: 2010-04-23
i have the following SQL string as part of my VB2005 code.

Select flight, min(DepDT) as DepDate,  convert(varchar(5), DateAdd(s,Sum(datediff(s,depdt,arrdt)),0),114) As FlyingTime, Substring(convert(varchar,dateadd(s,datediff(s,min(depdt),Max(arrdt)),0),120),9,20) As ElapsedTime From (Select flight,depdate + deptime As DepDT, arrdate+arrtime As ArrDT from legplanning Where Aircraft = '" + lblaircraft(k).Text + "' AND DepDate + deptime >='" + System.DateTime.Now.AddDays(-1) + "' AND DepDate + deptime <= '" + System.DateTime.Now.AddDays(14) + "') legplanning Group By flight

The problem i have is the System.DateTime.Now.AddDays(14) and the System.DateTime.Now.AddDays(-1) needs to be formatted as dd,mmmm,yyyy

Could somebody please show me the correct code for this i.e edit the sql string for me?
0
Comment
Question by:dr_dudd
  • 7
  • 5
  • 3
16 Comments
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 18002339
Hi dr_dudd,

You better format in ISO format. You can use:
System.DateTime.Now.AddDays(-1).TosTring("yyyy-MM-dd")


Cheers!
0
 
LVL 2

Author Comment

by:dr_dudd
ID: 18002361
hmmm.... tried replacing '" + System.DateTime.Now.AddDays(-1) + "' in the sql string with '"+System.DateTime.Now.AddDays(-1).TosTring("yyyy-MM-dd")+"'

and nothing was returned from the sql server. :-(
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 18002384
dr_dudd,

what if you hardcode a date into your string to test if your string is working correctly?
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 18002390
dr_dudd,

Wait a minute. What is this?
> DepDate + deptime

What are the datatype of these 2 fields? Can you do that?
0
 
LVL 2

Author Comment

by:dr_dudd
ID: 18002426
If i hardcode a date/time into my code then it works fine.... eg. 15 - november-2006
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 18002443
dr_dudd,
 what if you put 2006-11-15 ?
0
 
LVL 2

Author Comment

by:dr_dudd
ID: 18002478
Comment from emoreau
Date: 11/23/2006 12:24PM GMT
      Comment       Accept

dr_dudd,
 what if you put 2006-11-15 ?



Returns rows correctly...
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 34

Expert Comment

by:Sancler
ID: 18002645
Looks like a typo in this

System.DateTime.Now.AddDays(-1).TosTring("yyyy-MM-dd")

Shouldn't TosString be ToString?  Or is it something I haven't come across?

Roger
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 18002717
dr_dudd,
> Returns rows correctly...

so System.DateTime.Now.AddDays(-1).TosTring("yyyy-MM-dd")

will work correctly. Can you whow your code?

or maybe you should try: System.DateTime.Date.AddDays(-1).TosTring("yyyy-MM-dd")

0
 
LVL 2

Author Comment

by:dr_dudd
ID: 18002744
Heres a copy of the code for getting the data from my sql server and drawing appropriate boxes on screen.


For k = 1 To 5
            Dim flightlength As String = "Select flight, min(DepDT) as DepDate,  convert(varchar(5), DateAdd(s,Sum(datediff(s,depdt,arrdt)),0),114) As FlyingTime, Substring(convert(varchar,dateadd(s,datediff(s,min(depdt),Max(arrdt)),0),120),9,20) As ElapsedTime From (Select flight,depdate + deptime As DepDT, arrdate+arrtime As ArrDT from legplanning Where Aircraft = '" + lblaircraft(k).Text + "' AND DepDate + deptime >='" + System.DateTime.Now.AddDays(-1) + "' AND DepDate + deptime <= '" + System.DateTime.Now.AddDays(14) + "') legplanning Group By flight"
            Dim getflight1 As New SqlCommand(flightlength, cnafm)
            Try
                cnafm.Open()
                Dim flight1 As SqlDataReader = getflight1.ExecuteReader
                With flight1
                    If .HasRows Then
                        While .Read
                            days = .GetString(3)
                            daysint = days.Substring(0, 2)
                            daysint = ((daysint * 24) * 6)
                            hours = .GetString(3)
                            hoursint = hours.Substring(3, 2)
                            hoursint = (hoursint * 6)
                            minutes = .GetString(3)
                            minutesint = minutes.Substring(6, 2)
                            minutesint = (minutesint / 10)
                            length = daysint + hoursint + minutesint - 144
                            pos1 = .GetSqlDateTime(1)
                            Convert.ChangeType(pos1, TypeCode.DateTime)
                            pos2 = DateDiff(DateInterval.Minute, now, pos1)
                            position = 190 + (pos2 / 10)
                            formGraphics.FillRectangle(BlackBrush, New Rectangle(position, 131 + (k * 182 - 182), length, 20))
                        End While
                    End If
                End With

            Catch ex As Exception

            Finally
                cnafm.Close()
            End Try
        Next k
0
 
LVL 34

Expert Comment

by:Sancler
ID: 18002760
Doh.  I thought I was just being ignored, so I looked more carefully.  And I see the "typo" was TosTring not TosString.

Sorry for butting in.

Roger
0
 
LVL 2

Author Comment

by:dr_dudd
ID: 18002775
The answer was so simple its annoying...... just add .tolongdatestring to the end of the system.datetime.now.adddays(-1)
0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 18002800
dr_dudd,

change datetime for date and add the ToString:

>             Dim flightlength As String = "Select flight, min(DepDT) as
> DepDate,  convert(varchar(5), DateAdd(s,Sum(datediff(s,depdt,arrdt)),0),114)
> As FlyingTime, Substring(convert(varchar,dateadd(s,datediff(s,min(depdt),Max(arrdt)),0),120),9,20)
> As ElapsedTime From (Select flight,depdate + deptime As DepDT,
> arrdate+arrtime As ArrDT from legplanning Where Aircraft = '" +
> lblaircraft(k).Text + "' AND DepDate + deptime >='" +
> System.Date.Now.AddDays(-1).tostring("yyyy-MM-dd") + "' AND DepDate + deptime <= '" +
> System.Date.Now.AddDays(14).tostring("yyyy-MM-dd") + "') legplanning Group By flight"
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 18208658
I think I deserve the points.
0
 
LVL 34

Expert Comment

by:Sancler
ID: 18208811
I agree that emoreau should get the points

Roger
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now