?
Solved

Day month and year format.

Posted on 2006-11-23
16
Medium Priority
?
185 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month13 days, 8 hours left to enroll

800 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