0

I have an SQL statement I run for my website that inserts data into an access database. Is there a way during the insert to replace a space " " if one of the date textboxes contains a space? I need to do this because it throws off my date queries if there is a space in one of the columns?

INSERT INTO tblOpen (DateColumn1, DateColumn2) Values (@Value1, @Value2)
3
  • 1
    I need to ask you. Do you store a date value inside a string database field? Commented May 12, 2014 at 14:33
  • Its just a text data type column Commented May 12, 2014 at 14:34
  • 4
    And here are the roots of your problems. A date should be stored as a date, not as a string. Then you will avoid any kind of problems in querying on that dates. Commented May 12, 2014 at 14:40

3 Answers 3

2

You should use the 'datetime' type for your DateColumn. It solves all your problem. It is good to use proper variable for proper field.

Sign up to request clarification or add additional context in comments.

Comments

1

If you mean heading and trailing spaces, then:

myString = myString.Trim()

in your vb.net code will be fine. Even though I would follow Steve's suggestion and converting to date.

Comments

1

As question was about replace, you can either use Replace()

INSERT INTO tblOpen (DateColumn1, DateColumn2) 
    Values (REPLACE(@Value1, ' ', ''), @Value2)

or LTrim() and RTrim()

INSERT INTO tblOpen (DateColumn1, DateColumn2) 
    Values (LTRIM(RTRIM(@Value1)), @Value2)

However if datatype is a datetime then it makes sense to convert to that type using

DateTime d = Convert.ToDateTime(TextBox1.Text);
SqlParameter p = command.Parameters.Add("@Value1", System.Data.SqlDbType.DateTime);
p.Value = d;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.