473,399 Members | 4,254 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,399 software developers and data experts.

Proper syntax for a sql query

347 100+
I have the following code:


Expand|Select|Wrap|Line Numbers
  1.  
  2. Imports System.Data.SqlClient
  3. Public Class Form1
  4. Dim ReturnValue As Object = Nothing
  5. Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  6. Dim connectionString As String
  7. Dim cnn As SqlConnection
  8. Dim myCommand As SqlCommand
  9. Dim dteReturnValue As DateTime = Nothing
  10. 'the connection string to the SQL server'
  11.   connectionString = "Data Source=10.2.1.41;Initial Catalog=MDR;uid=xxxxx;password=xxxxxx"
  12. cnn = New SqlConnection(connectionString)
  13. cnn.Open()
  14. 'the SQL query'
  15. Try
  16. myCommand = New SqlCommand("select payrolldate from payroll where(payrolldate <= getdate())and payrollran <> 'yes'")
  17. myCommand.Connection = cnn
  18. ReturnValue = myCommand.ExecuteScalar()
  19. If ReturnValue IsNot Nothing Then
  20. dteReturnValue = Convert.ToDateTime(ReturnValue)
  21. dteReturnValue = dteReturnValue.AddDays(1)
  22. End If
  23. Catch exp As SqlException
  24. End Try
  25. cnn.Close()
  26. Dim ButtonDialogResult As DialogResult
  27. ButtonDialogResult = MessageBox.Show(String.Format("The next pay period available to you is {0} through {1}", dteReturnValue.ToShortDateString(), dteReturnValue.AddDays(7).ToShortDateString), "Payroll", MessageBoxButtons.OKCancel)
  28. If ButtonDialogResult = Windows.Forms.DialogResult.OK Then
  29. 'pass 'Return Value' to SQL query that will be here'
  30. Button2.Enabled = True
  31. Button1.Enabled = False
  32. End If
  33. End Sub
  34.  
  35.  

and what it will do currently is query a field in my sql database that is a date and return the following date +7 as the Messagebox.show results. I've discovered that this won't do exactly what I need it to do so I have to use this sql query:


select
a.MyDate,
Sunday = dateadd(dd,((datediff(dd,'17530107',a.MyDate)/7)*7)+7,'17530107')
from
( -- Test Data
select MyDate = convert(datetime,'20100930')
) a


which should return back the next available sunday to me from the database. What I'm getting back is 01/01/0001 - 01/08/0001.


I've tried a couple of versions of this but they all produce the same date. Can someone please assist.

Thank you


Doug
Oct 1 '10 #1
0 922

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

Similar topics

2
by: Shea Martin | last post by:
I am trying to use a system call which takes a function ptr. My compiler won't compile the code if I give the system_call a ptr to a class member function, A::func(). To combat this, I created an...
1
by: ivan | last post by:
The OpenReport syntax listed in Access97 help for "view", is "acViewPreview"or "acViewNormal". I have been using "acPreview " and "acNormal" . Is there any difference in the results or usage of...
2
by: Kevin | last post by:
I am currently importing data into Access 2002 from 3 Sybase ASA 7.0 databases over a network. At this time I am using a ODBC System DSN connection using the proper ASA 7 driver. I would like to...
2
by: Rick Brown | last post by:
I'm trying to run code behind a command button from a command button on another form but get an error on this line. I figure I've got the syntax wrong for calling this command button? Whats the...
1
by: Dalan | last post by:
I can't seem to find a workaround of Query Syntax Error. Actually, the query performs just fine, except when the last record on a related subform is deleted, then it generates a Runtime Error 3075...
2
by: ROBERT MELLOTT | last post by:
I am currently using VB 6.0 so I hope someone out there can help me. All I need is the proper syntax to reference the paramter in a SQL query. Here is my problem. I created a dataenvironment...
1
by: RamanS | last post by:
I have these variables: Dim str1Year As String Dim str5Years As String str1Year = "1 Year" str5Years = "5 Years" and this query statement If Me.frmEvergreen.Value = 1 Then
7
by: Neil | last post by:
What I am doing wrong This works batPointer = adaptors.adaptor->batData; adaptors.batteries = batPointer->battery; where: batData is a pointer to a struct batPointer is a pointer to a...
9
by: svdoerga | last post by:
I am wondering how you can change the fontcolor in the header or footer in VBA when exporting to excel. I need some text in red. I found the ms page with the codes here. It's saying the formatting is...
3
by: kstevens | last post by:
Please help. I know the sysntax is wrong. Here are some details. I am looking for the sum of past shipped items. I originally wrote this Dsum with only one criteria, but because of multiple...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.