Getting error The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. The statement has been terminated [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
this is my code
sqlcommand=new sqlcommand("inset into table values('"+home.name'"+,'"+home.DOJ'")",con);

You're using the local default string conversion from DateTime (I assume) to string, and then hoping that's valid for SQL.
Additionally, your code has a SQL injection attack vulnerability.
Both of these can be fixed - and your code readability - can be improved using parameterized SQL:
using (var command = new SqlCommand(
"INSERT INTO Foo (Name, DOJ) Values (#Name, #DOJ)", connection))
{
command.Parameters.Add("#Name", SqlDbType.VarChar).Value = home.name;
command.Parameters.Add("#DOJ", SqlDbType.DateTime).Value = home.DOJ;
...
}
(Adjust the types accordingly, e.g. using SqlDbType.NVarChar and SqlDbType.DateTime2.)
Never put values directly into SQL like you were doing. Using parameterized SQL is a win all round.

Related

SQL Insert Query With Condition Exception [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
I have this query:
String requete = "INSERT INTO reduction(newPrix) VALUES (?) "
+ "WHERE id_prom=? AND id_prod=?";
I am getting an exception:
You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use
near 'WHERE id_prom=2 AND id_prod=6' at line 1
I'm wondering why the INSERT query was failing. Any advise appreciated.
You insert a row into your table.
This means that you need to specify all mandatory values (i.e. values for the columns that are not nullable and don't have a default value).
You can't use a where clause in this kind of insert statement. What would it mean? And in your program you can verify the validity of the data you'd like to add to your table and decide to insert or not.

Fetching VB Variables to insert with SQL Query [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Hello everybody hope you are well.
I have a place on a project im working on that im struggling to get past. I'm basically gathering meta data of a song and assigning them to variables in VB.
I'm next trying to save this data to a SQL database with tables i have created in visual studio and im having trouble doing this. The problem lies within trying to link the VB variables into a SQL query. Any Ideas?
To link VB variables to an SQL query, you need to add parameters that store the variables and then use those parameters in your SQL command:
Using parameters
MyCommand = New SqlCommand("SELECT Height, Weight, DoB, Gender, FROM `User` WHERE Username = #Username", DatabaseConnection)
MyCommand.Parameters.AddWithValue("#Username", Username)
UPDATE: How to connect to database
'Leads to the database
Public Filename As String = $"{AppDomain.CurrentDomain.BaseDirectory}YourDatabase.accdb"
'The connection string is set
Public ConStr As String = $"PROVIDER=Microsoft.ACE.OLEDB.12.0;DATA Source = {Filename}"
'The connection to the database is defined
Public DatabaseConnection As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConStr)

JET ODBC SQL in excel, Case statements vs iif [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am getting unrecognized keyword when when I am running a query to join two of my sheets in excel.
Error - 2147467259 - Unrecognized keyword WHEN
This error is due to case statements, they are not accepted in JET ODBC.
I am using JET ODBC and querying within excel sheets.
Can anyone help write a proper case statement using JET ODBC syntax, i can't get this to work.
iif ([stack$].[business_name] = 'GELP'
AND [overflow$].[level] = 'Package' THEN [overflow$].[identifier1]) END AS standardized_identifier,
CASE statements are not supported in SQL when hitting Excel sheets. You are using the JET ODBC/OLEDB provider in Windows which has limited syntax.
Instead use the IIF() function which is similar syntax to Excel's If().
As an example, your first CASE would look like:
iif([stack$].[managementl6description] = 'GLOBAL EQUITY-LINKED PRODUCTS', 'GELP' , iif([stack$].[managementl6description] = 'EQUITY MARKETS', 'EQUITY MARKETS', iif([stack$].[managementl6description] = 'FOREIGN EXCHANGE', 'FIC Foreign Exchange', NULL))) AS business_name

How to use sp_depends for other database [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Using SQL Server 2010
For updating the tables i am using following line
update database1_ab..table1
For sp_depends i am getting error as "Incorrect syntax near '.'"
sp_depends database1_ab..table1
How to use sp_depends for referring other database tables in current database
regardless, you need to put it in single quotes... ie
sp_depends 'database1_ab..table1'
or
sp_depends '[database1_ab]..[table1]'
You can call all the sp_ -procedures also in different database like this:
database1_ab..sp_depends table1

Alter script not working on server [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an alter script to change datatype from nvarchar to float which works fine in my local machine but fails to work on 2012 server.
Can anybody tell me what's wrong with my script or any changes need to be done on server?
My alter script is as follows:
ALTER TABLE mytable
ALTER COLUMN mycolumn FLOAT
and I get this error:
Msg 8114, level 16,state 5, line 6
Error converting datatype nvarchar to float
If it is throwing an error 'Error converting data type varchar to float.' then it is obvious that there are some values in the varchar field which cannot be converted to float. If you can tell us more about the error being thrown, it will be easier to understand the issue.