Solved

Day month and year format.

Posted on 2006-11-23
16
176 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 70

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 70

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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 70

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 70

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
 
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 70

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 70

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 70

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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 Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

776 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