c# Sql between statement - sql

I'm trying to sort customers billings and i need to sort them by different time periods.
What I've been trying is:
(select billing_date from [transaktions]
between '" + start + "' and '" +stop+"' where konto_nr = #konto_nr")
also
(select billing_date from [transaktions] where konto_nr = #konto_nr" between '" + start + "' and '" +stop+"')
start = the starting period of the date
stop = the ending of the period
The error message I'm getting is
Incorrect syntax near the keyword
'between'.

First of all : you should never concatenate together your SQL statement! That's a big big open door for SQL injection attacks....
Second: you need to put your BETWEEN clause into a WHERE clause:
SELECT billing_date
FROM dbo.[transaktions]
WHERE Billing_Date BETWEEN #Start AND #EndDate
AND konto_nr = #konto_nr

Your syntax should be something like
where Transaktions.Billing_Date between StartDate and EndDate
of the obvious respective columns and variable names you are working with. Yes, you referred to the "billing_date" as a selected column, but the WHERE can be testing OTHER columns of criteria so you have to explicitly identify it there too.

Related

Use a parameter in SSRS for table_name

I have scoured the internet for options and the only one I have found that can do it is by using a $Proc however I am trying to avoid that.
I would think it would be pretty simple to use a parameter to select a different table depending on what the user chooses from a drop down.
Here it is:
- There are two tables the report needs to use,
* some_table_CY (current year table)
* some_table_STLY (same time last year table)
So I created a parameter that gives the user the option to select "Current_Year" or "Last_Year", depending on which one the user chooses the parameter would then be used in the select statement, something like this: "SELECT * FROM :pReportVersion"
However, it is not working. I need it to do this, not using a union since unioning these two tables causes HUGE performance issues and the query takes more than 4 hours to run which is not acceptable for a report that users need on request.
(This is querying oracle)
Use the Dataset expression and set it to:
="SELECT * FROM " & Parameters!ReportVersion.Value
For longer queries you may need to wrap each line with quotes, append with an ampersand and add a line feed:
="SELECT * " & VBCLRLF &
"FROM " & Parameters!ReportVersion.Value & VBCRLF &
"WHERE FIELD1 > 10 " & VBCRLF &
"AND FIELD2 = 'YES' "
you can still use the union..
Say you have a parameter called #year
set the available values to the following (specify values)
current year for label and 1 for value
last year for label and 2 for value
Then your dataset can be something like this:
select * from some_table_CY
where #year = 1
union all
select * from some_table_LY
where #year = 2

How to define variables in select statement vb.net?

I have a query like this
Dim view_src_14 As String = GetParameterValue("ViewSrc14")
Dim calendar_date_14 As String = GetParameterValue("CalendarDate14")
select calendar_date,view_src,sum(effective) effective_total, sum(ineffective) ineffective_total
from wrk_alert_effectiveness
where calendar_date='" + calendar_date_14 + "' and '" + view_src_14 + "'
group by 1,2
order by 1 desc;
calender_date_14 and view_src_14 are variables... when I run the query it bring this error:
invalid input syntax for type date: ""
Where do i make the changes??
I don't think this is specific enough for an answer, but it is too long for a comment.
You are trying to execute a SQL statement where you pass in values for constants in the statement. This is allowed and a part of SQL -- using parameters. There are two types of parameters, named parameters and positional parameters.
select calendar_date, view_src,
sum(effective) as effective_total, sum(ineffective) as ineffective_total
from wrk_alert_effectiveness
where calendar_date = #date1 and #date2
group by 1, 2
order by 1 desc;
Often, these are represented by ? for anonymous parameters. Sometimes named ones are introduced with colons.
The exact syntax depends on your database and the application interface you are using. My point is that you should learn about parameters and how to use them.
"select calendar_date, view_src, sum(effective) effective_total,
sum(ineffective) ineffective_total
from wrk_alert_effectiveness
where calendar_date= '" + #CalendarDate + "' AND " + #ViewSrc + "
group by 1,2
order by 1 desc;"

How do I Select Rows in DataTable based on Name and Date in VB.net?

Trying to create a VB.net expression to select rows from a datatable that only have a certain vendor's name (Vendor column), and the event is after a certain date (PurchaseDate column).
My current approach is to use:
datatableVar.Select("Vendor = '" + vendorName.ToString + "' And PurchaseDate < Convert('" + eventDate.ToString + "', DateTime)")
Currently it is saying that DateTime is an invalid Type name, pretty sure this is the syntax for convert though and it takes DateTime as a thing to format to.
The best option depends on exactly what you want to do with the data afterwards but, assuming that you want to stick with that Select method, there's no need to call Convert because you can just use a DateTime literal:
datatableVar.Select($"Vendor = '{vendorName}' AND PurchaseDate < #{eventDate:M/dd/yyyy}#")
Note that I have also used string interpolation rather than concatenation, in order to aid readability. If you're using an earlier version than VB 2015, use String.Format instead:
datatableVar.Select(String.Format("Vendor = '{0}' AND PurchaseDate < #{1:M/dd/yyyy}#",
vendorName,
eventDate)
The reason that your original code didn't work is that you didn't do what the documentation tells you to do when calling Convert. The example in the documentation is this:
myDataColumn.Expression = "Convert(total, 'System.Int32')"
So you can see there that the type is qualified with the namespace and it is wrapped in single quotes. That means that:
"', DateTime)")
should have been:
"', 'System.DateTime')")
ALWAYS read the relevant documentation first.

how to write int value inside the query passing through asp.net

I am having the following exception when passing the query through executereader:
incorrect syntax near )"...
How do I write the 0 here?
Here's the whole query:
string query = "select distinct BillNumber,PatientName,MobileNo,DueAmount from PaymentView where RequestDate between '" + fromDate.ToString("yyyy-MM-dd") + "' and '" + toDate.ToString("yyyy-MM-dd") + "' and DueAmount>'"+value+"')";
Extra Closing bracket at end of query. Also DueAmount should not be wrap into single quotes remove it.
and DueAmount>'"+value+"')";
------------^
Note : This may lead to SQL Injection attack, My suggestion is use Sql Parameter.

Conversion failed when converting the varchar value '08/22/1954' to data type int

I am trying to write stored procedure within my sql command as shown:
Dim tdate As String = Me.PresentDate.Value.ToString("MM-dd-yyyy")
myCommand As New SqlCommand("select c.description as 'provider',b.lastname,
b.firstname, b.middleinitial,convert(varchar(10),b.dob,101) as DOB,
b.chartID,b.sex, d.businessname,d.businessfax from patientappointmentbase as a,
patientlistbase as b,resourcebase as c, locationbase as d where convert(varchar(10),
a.starttime,101) = " & tdate & "
and a.patientid = b.patientid and a.resourceid = c.resourceid and
a.locationid = d.locationid order by provider, lastname, firstname", myConnection)
and when I run this code I'm getting the error as
Conversion failed when converting the varchar value '08/22/1954' to data type int.
You have two primary problems.
The first problem is that the date is being inlined to SQL so that the SQL you are executing looks like:
select ... where convert(varchar(10), a.starttime,101) = 07-07-2013
This is not valid SQL, so you need to wrap the date parameter in single quotes, i.e.
... where convert(varchar(10), a.starttime,101) = '" & tdate & "' and ...
If you wrap it in double-quotes and you have QUOTED_IDENTIFIER set to on, SQL Server will attempt to interpret it as a column name.
The second problem is that you are using two different date formats to compare. Convert using a style of 101 yields a date in the format mm/dd/yyyy. However, you are using a format of mm-dd-yyyy.
This means you are asking SQL server to compare "07/07/2013" with "07-07-2013" which will never be the same. The easiest fix is to change the date format of tdate to match SQL Server's:
Dim tdate As String = Me.PresentDate.Value.ToString("MM/dd/yyyy")
Your concatenated SQL string looks like
convert(varchar(10), a.starttime,101) = 08/22/1954
That's a sequence of division operations that results in a number, not a date.
You want to create a date literal by wrapping the value in quotes.
(or, better yet, use parameters)