473,378 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

putting a date string made from string builder in an sql query

Hi,
i'm currently making an app that needs to run a query between two dates. this app will run automatically, so i need to put the dates as today and yesterday, essentially.

Expand|Select|Wrap|Line Numbers
  1.         Dim str As New System.Text.StringBuilder()
  2.         Dim time As String
  3.         str.Append("'")
  4.         str.Append(Date.Now.Year.ToString)
  5.         str.Append("-")
  6.         str.AppendFormat("{0:MM}", Date.Now.Month.ToString)
  7.         str.Append("-")
  8.         str.Append(Date.Now.Day.ToString)
  9.         str.Append(".")
  10.         str.AppendFormat("{0:hh}", Date.Now.Hour.ToString)
  11.         str.Append(".")
  12.         str.AppendFormat("{0:mm}", Date.Now.Minute.ToString)
  13.         str.Append(".")
  14.         str.AppendFormat("{0:ssssss}", Date.Now.Second.ToString)
  15.         str.Append("'")
  16.         time = str.ToString
  17.         Console.WriteLine(time)
That is the string I made, and now I need to put it in the sql string.

Expand|Select|Wrap|Line Numbers
  1. '" & time & "'"
That is how I have it in my sql string, but it just crashes when I run it.

Any suggestions?
Jun 24 '10 #1
1 2913
Joseph Martell
198 Expert 128KB
I think that I see 2 problems with your code. The first is that you have 2 sets of ' to delimit your date string.

You start and end you string builder with the lines:

Expand|Select|Wrap|Line Numbers
  1. str.Append("'")
and then you insert it into you SQL statement as such:

Expand|Select|Wrap|Line Numbers
  1. "'" & time & "'"
so your date/time string looks like (if I follow your string builder logic):

''2010-06-23.09.01.57''

The second is your date/time format. I get an error when attempting to insert a date-time in that format into SQL Server.

Out of curiosity, why are you using a string builder to construct your date time string instead of something like:

Expand|Select|Wrap|Line Numbers
  1.         Dim myDate As Date = Date.Now
  2.         myDate.ToString("G")
  3.  
which gives me a timestamp of:
6/24/2010 9:12:11 AM

If the fractions of a second are necessary you can still use the date datatype's ToString method, but supply the following format:

Expand|Select|Wrap|Line Numbers
  1. myDate.ToString("yyyy-MM-dd hh:mm:ss.ffffff")
which gives me a timestamp of:
2010-06-24 09:15:17.113125
Jun 24 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Agent Mulder | last post by:
My funky new compiler does not understand string, but it does understand String, so that this program gives the expected result: #include<iostream> #include<string> int main() { String...
4
by: Peter Bailey | last post by:
I have a vba string taht dynamically creates the query which has two dates in it that it grabs off an open form as a string from the textbox. What I generate in vba is: SELECT DOSMBK.Date,...
0
by: Hessam | last post by:
Hello, I am designing a .net custom control in VS.net 7.1 and my control exposes an array of strings which are supposed to be the items to show. To do this have declared a private string variable...
4
by: J. Oliver | last post by:
I am attempting to copy a portion of a string into another string. Say I have "abcdef" in string 'a' and want to copy only "bcd" into a new string 'b'. The following function came to mind: ...
5
by: WebBuilder451 | last post by:
I have a string that is in the format of a url query string, but it is not a query string. "RESULT=126&PNREF=V54A0A779FD7&RESPMSG=Under review by Fraud...
0
by: jonefer | last post by:
Basic Syntax: I have a formula that needs to return "N/A" or a date. So the formula returns string where I am stuck is what is the equivalent of (MS Access) Format(, "mm/dd/yyyy") because...
22
by: Terry Olsen | last post by:
I have an app that makes decisions based on string content. I need to make sure that a string does not contain only spaces or newlines. I am using the syntax 'Trim(String)" and it works fine. I...
2
by: rocksoft | last post by:
Hi, I have used stream reader to read text file using Readline method, and i tried to append all string to string builder, but i'm not able to get all string value, i'm only getting last line of...
11
by: Dan Holmes | last post by:
I have a need to reverse a date to show as a "date code". For example today, 090507 would be coded as 905070 (reversing the parts of the date. This is what i have but there has to be a better...
8
by: Proaccesspro | last post by:
I have several reports that are date driven. (There is a Date field called date received in the DB) I need to offer the user a time frame for each report. For example, I have a report named by...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.