I have the code below
technician_sqlsource.SelectCommand = "Select analyst as Analyst, sample_description, RFA_number, convert(varchar(10), updated_date, 103)as updated_date, customer, po_number, total_charged from New_Analysis_Data where analyst = '" & FullName & "' and updated_date > '" & CDate(startdate.Text) & "'" & " and updated_date < '" & CDate(enddate.Text) & "'"
This basically passed through the sql command to the sql server and retrieves data between 2 dates. These dates come from 2 text boxes (start and end date). When I run this I get the error - The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
If I remove & "'" & " and updated_date < '" & CDate(enddate.Text) & "'" then it will work, seems to be an issue with the enddate.
Use query parameters!
technician_sqlsource.SelectCommand = _
"SELECT analyst as Analyst, sample_description, RFA_number, " & _
" convert(varchar(10), updated_date, 103)as updated_date, " & _
" customer, po_number, total_charged " & _
" FROM New_Analysis_Data " & _
" WHERE analyst = #FullName " & _
" AND updated_date >= #StartDate AND updated_date < #EndDate"
With technician_sqlsource.SelectCommand.Parameters
.Add("#FullName", SqlDbType.VarChar,50).Value = FullName
.Add("#StartDate", SqlDbType.DateTime).Value = startdate.Text
.Add("#EndDate", SqlDbType.DateTime).Value = enddate.Text
End With
This also may or may not fix your problem with the end date. Even if it doesn't fix the problem, you should get a clearer error message... but most likely, you're not entering a valid date format in that textbox. Have you considered using a DatePicker control?
1st off you should use SqlParameters!!!
It is much safer and you will also get rid of the date conversion issue.
SqlCommand cmd=new SqlCommand();
cmd.CommandText = #"
Select analyst as Analyst,
sample_description,
RFA_number,
convert(varchar(10), updated_date, 103) as updated_date,
customer,
po_number,
total_charged
from New_Analysis_Data
where analyst = #analyst
and updated_date between #startdate and #enddate";
technician_sqlsource.SelectCommand = cmd;
technician_sqlsource.SelectCommand.Parameters.Add(new SqlParameter("#analyst", FullName));
technician_sqlsource.SelectCommand.Parameters.Add(new SqlParameter("#startdate",CDate(startdate.Text)));
technician_sqlsource.SelectCommand.Parameters.Add(new SqlParameter("#enddate", CDate(enddate.Text)));
If you want still in your old (unsafe) style .. you can try this ..
technician_sqlsource.SelectCommand = "Select analyst as Analyst, sample_description, RFA_number, convert(varchar(10), updated_date, 103)as updated_date, customer, po_number, total_charged from New_Analysis_Data where analyst = '" & FullName & "' and updated_date > #" & startdate.Text & "# and updated_date < #" & enddate.Text & "#"
But later you must use query parameters! .. to prevent SQL Injection !
try this:
technician_sqlsource.SelectCommand = "Select analyst as Analyst, sample_description, RFA_number, convert(varchar(10), updated_date, 103)as updated_date, customer, po_number, total_charged from New_Analysis_Data where analyst = '" & FullName & "' and updated_date > '" & CDate(startdate.Text) & "' and updated_date < '" & CDate(enddate.Text) & "'"
Related
I am just curious, how do I concat join table so the result doesn't return many rows?
What I want is the firstname d return only one row like 001;004;005;003;007 in column interest, so in my grid doesn't display many rows.
Here's my code
.CommandText = String.Format("select aa.code, aa.firstname, cc.interest as interest from db.name aa ") _
'& ("left join db.interest cc on cc.lead = aa.code ") _
'& ("where convert(date,time) between '" & date1& "' and '" & date2& "' order by aa.code ")
I am asking here because I don't know the keyword that relates to this
You need to add GROUP BY Steatment to your query:
.CommandText = String.Format("select aa.code, aa.firstname,COUNT(aa.firstname) as 'FnameCount', cc.interest as interest from db.name aa ") _
'& ("left join db.interest cc on cc.lead = aa.code ") _
'& ("where convert(date,time) between '" & date1& "' and '" & date2& "' GROUP BY aa.code, aa.firstname, cc.interest order by aa.code ")
I am having following VBA Code that has been giving a syntax error. Can someone please help me figure out what is causing the error?
Private Sub Command11_Click()
Dim EndingDate As Date
'Getting ending date from Label named endDate
EndingDate = endDate
StartingDateTxt = DateSerial(Year(EndingDate), Month(EndingDate) - 15, Day(EndingDate))
Dim customerRecords As New ADODB.Recordset
customerRecords.Open "SELECT COUNT(*) AS N FROM (SELECT DISTINCT E.Date,"&_
"E.[Inv Num], E.CusName, E.[Name Street1], E.[Name Street2], "&_
"E.[Name City], E.[Name State], E.[Name Zip], E.[Account #], E.Amount FROM TempFromExcel "&_
"AS E INNER JOIN TempFromExcel AS X ON E.CusName = X.CusName "&_
"WHERE (((DateDiff("d",X.Date,E.Date))>=30)) AND E.Date >= '" & StartingDateTxt & "' and"&_
"E.Date <= '" & endDate & "') AS T ;", _
CodeProject.Connection , _
adOpenStatic, _
adLockOptimistic, _
adCmdText
MsgBox customerRecords("N")
End Sub
My Query is taking both dates and finding the results that are between the two dates.
I think I may be missing at that part only. The rest seems fine as I had explicitly check the query and it runs fine. So is this right ?
E.Date >= '" & StartingDateTxt & "' and E.Date <= '" & endDate & "'
This has been corrected, in the answer but still am getting syntax error in Select statement first line. Am missing something?
In Microsoft Access SQL query you have to encapsulate Date value into ##, like for example, #06/01/2015#. Pertinent to your case it should look like:
E.Date >= #" & StartingDateTxt & "# AND E.Date <=#" & endDate & "#"
Hope this may help.
Try changing these lines:
"WHERE (DateDiff('d', X.Date, E.Date) >= 30 AND E.Date >= #" & Format(StartingDateTxt, "yyyy\/mm\/dd") & "# and " & _
"E.Date <= #" & Format(endDate, "yyyy\/mm\/dd") & "#) AS T ;", _
Can any body help me with the following SQL statement?
What am I doing wrong here?
It is giving me no result however there should be some...
The desired result should be a list or errorsdetails with the total number of times it is in the database (mdb) based on the selected date-period.
PS sorry for my badly written English..
Many thanks
Koen
sSQL = "SELECT ErrorDetail, count(*) AS totalcount FROM tblErrors WHERE DisplayName = " & cbox_modules.Text & " AND ErrorDate BETWEEN #" & startdate & "# AND #" & enddate & "# GROUP BY ErrorDetail"
Assuming the "DisplayName" column is a string, ensure you surround it with apostrophes:
SELECT
ErrorDetail,
COUNT(*) AS totalcount
FROM tblErrors
WHERE DisplayName = '" & cbox_modules.Text & "' AND ErrorDate BETWEEN #" & startdate & "# AND #" & enddate & "#
GROUP BY ErrorDetail
new to SQL and having trouble with this code. It seems that the "SUM" function is just really slow. It times out if the date range is more than 5 days. I have to loop thru this 115 times and cover a date range of 180 days. Any ideas on what I can do to speed it up? The string times out in the SQL Enterprise Manager as well.
I've heard mention of using a "pivot" command, but don't know if that is the answer or how to implement it. TIA
strSQLRead = "SELECT SUM(BatchRecordDetail.BRD_Actual) AS LbsBatched, BatchRun.BAT_CoatingLBS AS CoatingLbs, " _
& "BatchRun.BAT_ID AS RunID " _
& "FROM BatchRun " _
& "INNER JOIN BatchRecordMaster ON BatchRun.BAT_HmiRunID = BatchRecordMaster.BRM_BatchRunID " _
& "AND BatchRun.BAT_Area = BatchRecordMaster.BRM_Area " _
& "INNER JOIN BatchRecordDetail ON BatchRecordMaster.BRM_Area = BatchRecordDetail.BRD_Area " _
& "AND BatchRecordMaster.BRM_HmiBatchID = BatchRecordDetail.BRD_BatchRecordID " _
& "WHERE (BatchRun.BAT_ExtruderEnd BETWEEN CONVERT(DATETIME, '" & Format(ToDate, "yyyy-MM-dd") & " 00:00:00', 102) " _
& "AND CONVERT(DATETIME, '" & Format(FromDate, "yyyy-MM-dd") & " 00:00:00', 102)) AND (BatchRun.BAT_RunComplete = 1) " _
& "AND BatchRun.BAT_FormulaCode = '" & sFormula(i) & "'" _
& "GROUP BY BatchRun.BAT_FormulaCode,BatchRun.BAT_ID, BatchRun.BAT_CoatingLBS " _
& "HAVING (BatchRun.BAT_CoatingLBS Is Not Null)"
Here is the string as trapped in code. Just wondering if anyone had any ideas. thx
SELECT Sum(batchrecorddetail.brd_actual) AS LbsBatched,
batchrun.bat_coatinglbs AS CoatingLbs,
batchrun.bat_id AS RunID
FROM batchrun
INNER JOIN batchrecordmaster
ON batchrun.bat_hmirunid = batchrecordmaster.brm_batchrunid
AND batchrun.bat_area = batchrecordmaster.brm_area
INNER JOIN batchrecorddetail
ON batchrecordmaster.brm_area = batchrecorddetail.brd_area
AND batchrecordmaster.brm_hmibatchid =
batchrecorddetail.brd_batchrecordid
WHERE ( batchrun.bat_extruderend BETWEEN
CONVERT(DATETIME, '2014-06-26 00:00:00', 102)
AND
CONVERT(DATETIME, '2014-06-26 00:00:00'
, 102) )
AND ( batchrun.bat_runcomplete = 1 )
AND batchrun.bat_formulacode = '3100007'
GROUP BY batchrun.bat_formulacode,
batchrun.bat_id,
batchrun.bat_coatinglbs
HAVING ( batchrun.bat_coatinglbs IS NOT NULL )
I can't post pics cause I don't have enough reputation points.
How would I display what else is needed to help answer the question?
This is my query which runs perfectly
"SELECT RemBal FROM Sales WHERE CustomerName='" & CustomerName.Text & "'"
now i am trying to get the balance on two basis customer name & current date
"SELECT RemBal FROM Sales WHERE CustomerName='" & CustomerName.Text & "' AND SaleDate=#" & SaleDate.Value & "#"
now this query not giving me any error but not returning any value too
please help
Try this
SaleDate.Format = DateTimePickerFormat.Custom
SaleDate.CustomFormat = "yyyy-mm-dd"
"SELECT RemBal FROM Sales WHERE CustomerName='" & CustomerName.Text & "' AND SaleDate=#" & SaleDate.Text() &"# "
SELECT rembal
FROM sales
WHERE customername = '" & CustomerName.Text & "'
AND Format(saledate, 'dd/MM/yyyy') = '" & SaleDate.Value & "'