You should use the 'datetime' type for your DateColumn. It solves all your problem. It is good to use proper variable for proper field.
SQL Insert and Replace?
Question
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)
Solution
OTHER TIPS
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.
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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow