Results 1 to 4 of 4

Thread: sql statement select range 2 date field in sql server

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2004
    Location
    kl
    Posts
    1

    sql statement select range 2 date field in sql server

    1. how do i write a sql statement to select 2 range date fields in sql server
    i tried in usual vb6 code but its not working

    vb.net & sql server
    =============
    field name = datein, dateout
    so it will be datein => txtdatein.text and dateout =< txtdateout.text

    my date format is "dd/MM/yyyy"


    2. if i use this below method, the sql server reads the date format as "MM/dd/yyyy" but in my database date stored as "dd/MM/yyyy". how do i get the format to read field in db as "dd/MM/yyyy".

    Where DateIn >= '" & txtDateIn.Text & "' and DateOut <= '" & txtdateOut.Text & "' "

    thanks guys.
    rizal

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    you can use the between method for 2 dates like that. From an SQL statment, I think you're forced to use the really wierd SQL Server format, however if you can use stored procedures, you can do that on by calling the DATEFORMAT method...
    Code:
    CREATE  PROCEDURE spMyStoredProcName
    @DateIn (fieldtype),
    @DateOut (fieldtype)
    AS
    SET DATEFORMAT dmy 
    
    SELECT * FROM mytable
    WHERE datein BETWEEN @DateIn AND @DateOut

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  3. #3
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    This is how I do mine
    VB Code:
    1. CREATE PROCEDURE stp_GetMailingByDateRange
    2.     @StartDate varchar(10),
    3.     @EndDate varchar(10)
    4. AS
    5. SELECT A.ESPEmail_MailingID,
    6.     A.SubjectLine,
    7.     A.MailingDesc,
    8.     A.EmailCreator
    9. FROM dbo.ESPEmail_Mailing As A
    10. [color=red]WHERE A.MailingDate >= @StartDate AND A.MailingDate <= @EndDate[/color]
    11. AND A.InActive = 0
    12. AND A.EmailSent = 0
    13. ORDER BY A.SubjectLine, A.MailingDesc
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  4. #4
    Lively Member nokia8210's Avatar
    Join Date
    Dec 2002
    Location
    Coventry
    Posts
    92
    The Date format needs to be : 'DD/MMM/yy'

    this then tells the sql server which part is the month
    then just convert it back when you get the info


    by Format(Date,"dd/MM/yy")

    you also need to dim dates as show below

    so it should look like this:

    Dim DateIn as Date
    Dim DateOut as Date

    DateIn = Format(txtBox.Text,"dd/MMM/yy")
    DateOut = Format(txtBox2.Text,"dd/MMM/yy")

    then put the DateIn and DateOut into the query this should slove it.
    Last edited by nokia8210; Mar 19th, 2004 at 11:49 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width