SQL statement error in VB.Net - mysql

This is the SQL statement which I taken from debug mode in VB.Net.
SELECT dt_Date as 'Date',
s_Time as 'Time',
s_For as 'For',
s_Categ as 'Category',
s_Count as 'Count',
s_Remarks as 'Remarks'
FROM Entry
WHERE (s_ENo = '22' and dt_date BETWEEN '06-05-16' And '27-05-16')
I am not sure what's wrong with the above statement since everything seems to be fine to my eyes.
Description of the Error Message
Additional information: Conversion failed when converting date and/or
time from character string.
Any suggestions how to solve this riddle?

Instead if string literals, use strongly-typed parameters. In addition to avoiding data type conversion issues on the SQL Server side, parameters:
Avoid the need to format date/time string literals in a particular way
Improve performance by providing execution plan resue
Prevent SQL injection (barring dynamic SQL on the server side)

If you are using MS-SQL server than use following instructions.
The basic solution is that you have to provide date into either mm/DD/YYYY format or in YYYY-MM-DD date format into ms-sql query.
So, before passing date to query convert your date into either mm/DD/YYYY format or in YYYY-MM-DD format.

You have to format your date string to YYYYMMDD or if you know the server date format then format same as server

Related

Parsing Custom Date Format in MySQL

Problem
I need to query out a date value for use in some ETL processing. However, the data is stored in MySQL as a VARCHAR column in values like 1/1/19.
What I've tried
A simple CAST(myDateColumn as DATE) fails as I get values like 0001-01-19 returned back.
Question
Am I able to pass a custom date format string into the CAST call somehow to tell it how to parse out the date parts? If not, could a SUBSTRING type of function help here?
As discussed in the comments above, you can use the STR_TO_DATE() function to parse a string into a proper YYYY-MM-DD date.

Trouble Converting varchar to date

I'm using MAMP's phpMyAdmin as my database.
I'm having troubles using the CONVERT sql function. Is this a common problem?
SELECT CONVERT(varchar(10), Date, 103)
This will give me the following syntax error. Which is one of the usual errors given but I've been trying a lot of different ways.
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near 'varchar(10), Date, 103)' at line 1
If the above can't be solved can I bypass this issue by adding a new column with a specific date format style?
You are trying to use sql-server syntax in mysql database.
mysql does not have 3rd parameter for CONVERT function.
CONVERT(expr,type), CONVERT(expr USING transcoding_name)
The CONVERT() and CAST() functions take an expression of any type and
produce a result value of a specified type.
http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html#function_convert
to do the get same format in mysql you need to use DATE_FORMAT() function
this should give you the same resultDATE_FORMAT(date,'%d/%m/%Y')
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
select DATE_FORMAT(Current_Date(),'%d/%m/%Y')
here is SQL Fiddle
http://sqlfiddle.com/#!2/d41d8/36014
The date is already in the correct format but it is stored as varchar(10) Current looks like: 21/04/2014
Yes, I have a column that has dates but is set to varchar as it wasn't being used now I need to convert these into dates.
If you are trying to get a date form from a string form of date, then
select str_to_date( date_column, '%d/%m/%Y' ) as date from table_name
This will return 21/04/2014 as 2014-04-21, regular default date format in MySQL.
It depends on what you want to do.
If you're trying to convert an integer that represents a Unix timestamp to a date with MySQL, you may want to use FROM_UNIXTIME()
See also: MySQL: Convert INT to DATETIME
If you're trying to convert a string that represents a date into a date, you can use STR_TO_DATE()
After that, if you perform some processing on the date and want to convert it back to a string, you'll need DATE_FORMAT()
See also: mySQL convert varchar to date

SQL Server (T-SQL) datetime conversion

A silly question maybe but I wanted clarification. I've created a script that has a date parameter like so:
DECLARE #dateparam as datetime
SET #dateparam = '01-01-2013 00:00:00'
This looks like it is working when I test it even if the date string is not in "correct" format yyyy-MM-dd hh:mm:ss. I changed my computer regional settings to English and the script still did what it was supposed to do.
Is this because of SQL Server 2008 R2 that I have in my computer that it knows how to convert the date or can I ran into trouble with using a dateformat like I have used?
Converting 01-01-2013 won't expose issues such as which 01 is the month, and which is the day.
It's not a safe format.
The safe formats (for converting to datetime, rather than to datetime2) are:
YYYYMMDD 20121201
YYYY-MM-DD'T'hh:mm:ss 2012-12-01T10:43:29
YYYY-MM-DD'T'hh:mm:ss.mil 2012-12-01T10:43:29.337
Stick to those and only those. (The examples all represent the 1st December 2012)
Or, better yet, don't treat dates as strings at all, if you can avoid it. If you're, for example, calling SQL Server from .NET code, keep that dates as DateTimes in your code, and let ADO.NET and SQL Server deal with any required translations to make them become datetimes - without translating them to and from strings.
You're making an implicit conversion from something that looks like a date, but inf fact is a string ( '01-01-2013 00:00:00'). Rather than trusting on SQL Server to make the correct guess in what format the string is in, you should make the conversion explicit by specifying the format.
This can be done by using CONVERT (not CAST) and specify a 'style'. The different styles are listed here: http://msdn.microsoft.com/en-us/library/ms187928.aspx.

Conversion failed when converting datetime from character string on reporting service

when i convert or cast string to date ran succesfull my query from t-sql on sql server but when i assign dataset query on reporting service,i take this error.
'Conversion failed when converting datetime from character string.'
It will depend on the regional settings on your server. I guess your reporting services server thinks days and months are in the opposite position from your SQL server.
The easiest way to consistently get dates to convert from string properly without having to consider regional settings is to use military date format: YYYY-MM-DD
Military format always converts correctly.
Make sure you are not forgetting to put the braces on the "parameter value" field when editing parameters on the DataSet Properties window:
This gives the conversion error:
Parameter Name Parameter Value
#queryDateParameter #reportDateParameter
Correct is:
Parameter Name Parameter Value
#queryDateParameter [#reportDateParameter]
I've had the same issue when trying to pass parameters through SSRS.
when the parameter types are text, SSRS does a conversion using server region settings, in which case, a MM/DD/YYYY and DD/MM/YYYY difference resulted in this error.
Solution for me was to change the parameter type to datetime. This enforces the conversion to be handled correctly.

SQL Server 2008 VarChar To DateTime

I have a table where unfortunately a number of dates are stored as strings.
I have a number of reports that cast them to datetimes and filter by them. This was working fine until today when all of a sudden i'm getting this error
"The conversion of a varchar data type to a datetime data type resulted in an out-of-range value."
The dates are all stored in the format of "yyyy-mm-dd" and are all valid.
If I run the following SQL statement
SELECT CAST('2010-06-02' AS DateTime)
I would expect to get "2010-06-02" however as of today I'm getting "2010-02-06" something has changed with the way SQL formats dates. I've had a look in regional settings on the server and it all looks to be correct.
What else could be causing this?
Try setting the format explicitly
select convert(datetime, '2010-06-02',101)
An unambiguous way of getting this conversion is to do the following:
SELECT CAST(replace('2010-06-02', '-', '') AS DateTime)
And that will always be interpreted as YYYYMMDD, ignoring the set dateformat ydm declaration or any cultural settings that the database has.
Q1: What else could be causing this?
The local, You probably are under the local101(US) and put data from 103 (British/French)
Like barry sad use convert