Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 741587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:38:59+00:00 2026-05-14T08:38:59+00:00

I have a date column in a DB tabel that I want to query

  • 0

I have a date column in a DB tabel that I want to query using a date taken from textbox.text. the user selects a date from the calendar in the format dd/MM/yyyy. I want to use that date to put into a query. How do i format the date to be able to query the database?

  Dim datefrom As String =txtDateFrom.Text
  Dim dateto As String =txtDateTo.Text

The query will look like this:

  WHERE (tblClient.ClientID = " & ClientID & ") AND (tblBackupArchive.BackupDate BETWEEN '" + datefrom + "' AND '" + dateto + "')"

I’m using MS SQL Server btw. Any help most appreciated.

Jonesy

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T08:38:59+00:00Added an answer on May 14, 2026 at 8:38 am

    NEVER USE STRING CONCATENATION LIKE THAT TO BUILD YOUR QUERIES!!!

    And yes, I did mean to yell, because date formatting is the least of your problems. Imagine what would happen in your current code if something entered the following into one of your date textboxes:

    ‘;DROP TABLE tblClient;–

    Instead, use a parameterized query. That will fix your date issues and protect against sql injection attacks. Here’s an example:

    Dim sql As String = " .... WHERE tblClient.ClientID= @ClientID AND tblBackupArchive.BackupDate >= @DateFrom AND tblBackupArchive.Backupdate < @DateTo"
    
    Using cn As New SqlConnection("your connection string here"), _
          cmd As New SqlCommand(sql, cn)
    
        cmd.Parameters.Add("@ClientID", SqlDbType.Int).Value = ClientID
        cmd.Parameters.Add("@DateFrom", SqlDbType.DateTime).Value = DateTime.Parse(txtDateFrom.Text)
        cmd.Parameters.Add("@DateTo", SqlDbType.DateTime).Value = DateTime.Parse(txtDateTo.Text).AddDays(1)
    
        cn.Open()
    
        cmd.Execute___()
    
    End Using
    

    You can think of it now as if you had run an sql statement more like this:

    DECLARE @ClientID Int
    DECLARE @DateFrom DateTime
    DECLARE @DateTo DateTime
    
    Set @ClientID = ImaginaryFunctionToGetQueryData('ClientID')
    Set @DateFrom = ImaginaryFunctionToGetQueryData('DateFrom')
    Set @DateTo   = ImaginaryFunctionToGetQueryData('DateTo')
    
    SELECT ... 
    FROM ... 
    WHERE tblClient.ClientID= @ClientID 
        AND tblBackupArchive.BackupDate >= @DateFrom 
        AND tblBackupArchive.Backupdate < @DateTo
    

    The “ImaginaryFunction” in that code is accomplished using the sp_executesql stored procedure, but the point is that the query string as seen by sql server will never substitute data directly into the query string. Code is code, data is data, and never the ‘twain shall meet.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that contains records with a column indicates the Date. Given
I have a DATE datatype mysql column in a table. I want to set
I have a table of user-generated content that includes an ENUM format column for
I have a table that has a column with type of dateTime(I'm using SQL
I have a class hierarchy that I want to map across several tables using
I have a table that has three different date columns, so I set each
I have table with date/time column and person column. Each person has multiple records
If I have table with a Date column called myDate , with values like
I have this table: ID(INT) DATE(DATETIME) Under the DATE column there are a lot
I want to select all distinct order_ids from my table, and order that list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.