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)
有帮助吗?

解决方案

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

其他提示

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;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top