Solved

Day month and year format.

Posted on 2006-11-23
16
182 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…

724 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