The SQL:
dim sql
sSql = "SELECT * FROM [dbo].[table] WHERE [sent] = 1 and datesent between '" & dStartDate & "' and '" & dFinishDate & "'"
response.write(sSql)
set oRs = oConn.execute(sSql)
When i execute this sql in sql server 2008 it works fine.
However, when i execute it within my application i get error:
Microsoft OLE DB Provider for SQL Server error '80040e07'
The conversion of a char data type to a datetime data type resulted
in an out-of-range datetime value.
Is there something different i have to do in the application? thanks
Have you tried using ISO format dates?
SELECT * FROM [dbo].[Table]
WHERE [sent] = 1 and datesent between '20120101' and '20120127'
(Strictly speaking an ISO 8601 date for sql server is yyyy-mm-ddThh:mm:ss[.mmm])
Side Note: always use ISO format dates when you output to text (or have literals); that way they can be read unambiguously on all systems).
SQL Server ISO 8601 Format Dates
http://msdn.microsoft.com/en-us/library/ms190977(v=sql.90).aspx
SELECT *
FROM [dbo].[Table]
WHERE
[sent] = 1
and datesent between CONVERT(datetime, '01/01/2012', 101) and CONVERT(datetime, '01/27/2012', 101)
OR
Your column datesent is not DATETIME datatype
Check your datetime setting.
set dateformat mdy
select CAST('01/27/2012' as datetime)
set dateformat dmy
select CAST('01/27/2012' as datetime) --> Exception
You can display the current setting with:
DBCC USEROPTIONS
You should always use the ISO-formats 'YYYYMMDD' for your dates, as this will always be parsed correctly by SQL Server, regardless of localization setting.
Related
I am using SQL Server Compact 3.5 version for a small application. I want to extract the date part from a column (which is of datetime data type) and I used following query to try and do that:
string qry = "SELECT DISTINCT DATE(Finger_Record) as [Working Date] FROM dt_raw_data ";
When I execute it, I am getting a error saying DATE function is not a function in SQL Server Compact. Then how can I do this? Please help me.
Try this code it is work for me
select distinct CONVERT(VARCHAR(10),Finger_Record,110) as [Working Date] from dt_raw_data
Try this code:
string qry = "SELECT DISTINCT CONVERT(nvarchar(10), Finger_Record, 101) as [Working Date] FROM dt_raw_data ";
I have a table with a column that stores the date and time. I need to write a query to get only the date from that column,
SELECT CAST(CONVERT(VARCHAR, LoginTime, 101) AS datetime) FROM AuditTrail
But, when I run the query I am getting this error:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
the data in the column is datetime ex: 2012-06-18 12:08:04.000
so i need to extract the date only and remove the time
note that the [Logintime] column is datatime format
Try ISDATE() function in SQL Server. If 1, select valid date. If 0 selects invalid dates.
SELECT cast(CONVERT(varchar, LoginTime, 101) as datetime)
FROM AuditTrail
WHERE ISDATE(LoginTime) = 1
Click here to view result
EDIT :
As per your update i need to extract the date only and remove the time, then you could simply use the inner CONVERT
SELECT CONVERT(VARCHAR, LoginTime, 101) FROM AuditTrail
or
SELECT LEFT(LoginTime,10) FROM AuditTrail
EDIT 2 :
The major reason for the error will be in your date in WHERE clause.ie,
SELECT cast(CONVERT(varchar, LoginTime, 101) as datetime)
FROM AuditTrail
where CAST(CONVERT(VARCHAR, LoginTime, 101) AS DATE) <=
CAST('06/18/2012' AS DATE)
will be different from
SELECT cast(CONVERT(varchar, LoginTime, 101) as datetime)
FROM AuditTrail
where CAST(CONVERT(VARCHAR, LoginTime, 101) AS DATE) <=
CAST('18/06/2012' AS DATE)
CONCLUSION
In EDIT 2 the first query tries to filter in mm/dd/yyyy format, while the second query tries to filter in dd/mm/yyyy format. Either of them will fail and throws error
The conversion of a varchar data type to a datetime data type resulted
in an out-of-range value.
So please make sure to filter date either with mm/dd/yyyy or with dd/mm/yyyy format, whichever works in your db.
hope this may help you:
SELECT CAST(LoginTime AS DATE)
FROM AuditTrail
If you want to have some filters over this datetime or it's different parts, you can use built-in functions such as Year and Month
as you can see on the answer to this question:
Conversion of a varchar data type to a datetime data type resulted in an out-of-range value
-- set the dateformat for the current session
set dateformat dmy
-- The conversion of a varchar data type
-- to a datetime data type resulted in an out-of-range value.
select cast('2017-08-13 16:31:31' as datetime)
-- get the current session date_format
select date_format
from sys.dm_exec_sessions
where session_id = ##spid
-- set the dateformat for the current session
set dateformat ymd
-- this should work
select cast('2017-08-13 16:31:31' as datetime)
I struggled with the same problem. I have stored dates in SQL Server with format 'YYYY-MM-DD HH:NN:SS' for about 20 years, but today that was not able anymore from a C# solution using OleDbCommand and a UPDATE query.
The solution to my problem was to remove the hyphen - in the format, so the resulting formatting is now 'YYYYMMDD HH:MM:SS'. I have no idea why my previous formatting not works anymore, but I suspect there is something to do with some Windows updates for ADO.
some problem, but I find the solution, this is :
2 February Feb 28 (29 in leap years)
this is my code
public string GetCountArchiveByMonth(int iii)
{
// iii: is number of months, use any number other than (**2**)
con.Open();
SqlCommand cmd10 = con.CreateCommand();
cmd10.CommandType = CommandType.Text;
cmd10.CommandText = "select count(id_post) from posts where dateadded between CONVERT(VARCHAR, #start, 103) and CONVERT(VARCHAR, #end, 103)";
cmd10.Parameters.AddWithValue("#start", "" + iii + "/01/2019");
cmd10.Parameters.AddWithValue("#end", "" + iii + "/30/2019");
string result = cmd10.ExecuteScalar().ToString();
con.Close();
return result;
}
now for test
lbl1.Text = GetCountArchiveByMonth(**7**).ToString(); // here use any number other than (**2**)
**
because of check **February** is maxed 28 days,
**
I hope this is solvable.
Basically, I'm working with an SQL Server database table and in this table there is a field called matchdate. The format of the field is nchar(50).
There are over 2000 records in my table and the dates in the matchdate field are all in UK format, for example, "29/04/2014".
I am trying to query the table so I can pull out records between two UK dates. To do this I am using the following query:
SET DATEFORMAT DMY;
SELECT * from mytable
WHERE CAST(matchdate as datetime) BETWEEN '" & startdate & "' and '" & enddate & "'
order by cast([matchdate] as datetime) asc"
As you can probably tell this type of query is certainly not my strength. If the UK startdate value is "01/03/2014" and the UK enddate value is "23/04/2014" I get the following error:
Microsoft OLE DB Provider for SQL Server error '80040e07'
Conversion failed when converting date and/or time from character string.
I'm guessing this is because I am using two UK dates formats? How do I query the UK formatted "matchdate" field table using UK "start" and UK "end" formatted dates and get around this error?
PS: Unfortunately, I do not have access to the database table.
Try using the CONVERT function for the date so that you can specify the date standard
e.g. UK is 103
-- test
SELECT CONVERT(DATETIME, '10 January 2014', 103)
SELECT * from mytable WHERE CONVERT(DATETIME, matchdate, 103) BETWEEN '" & startdate & "' and '" & enddate & "' order by CONVERT(DATETIME, matchdate, 103) asc"
I have an application that comes with its own database and there is nothing I can change on that configuration. However we do pull data from the database to generate reports on.
For some reason (which I don't quite grasp), the application stores dates as the following number:
numdate = (int) '1' & <last two digits of year> & <zero-leading month> & <zero-leading day>
eg:
08/10/2008 -> 1081008
01/01/2014 -> 1140101
27/02/2014 -> 1140227
For now I just pull in the number and convert it on the go to a real date.
Is it possible to do this conversion via the sql query somehow?
If it's SQL Server, the following should work:
DECLARE #dateInt INT = 1981008
SELECT CONVERT(DATETIME, SUBSTRING(CONVERT(VARCHAR, #dateInt), 2, 6), 12)
To save you reading the comments, Gunther improved the performance by removing the substring operation in favour of subtraction:
SELECT CONVERT(DATETIME, CONVERT(VARCHAR,#dateInt-1000000), 12)
You could use this if you are using C#.
string dateString = "1981008";
DateTime dt;
DateTime.TryParseExact(dateString.Remove(0,1), "yyMMdd", null, System.Globalization.DateTimeStyles.None, out dt);
In SQL (assuming it is MS SQL), try this:
convert(datetime,'981008',112)
EDIT:
The solution is in the reference post's solution .I was careless to overlook DATETIME--> Varchar(10)
`Syntax for CONVERT:
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )`
I am aware of this post
SQL Server (2005, 2000, 7.0) does not have any flexible, or even non-flexible, way of taking an arbitrarily structured datetime in string format and converting it to the datetime data type.
So I am looking for a solution that solves this particular String format only.
Let's say I have a table in sql server with field :inputDate in datetime format
The following code works without convert/cast
SELECT inputDate
FROM some_table
WHERE inputDate > '01/24/2013'
But it won't work for
SELECT inputDate
FROM some_table
WHERE inputDate > '24/01/2013'
Throwing an The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
Then I tried
SELECT inputDate
FROM some_table
WHERE inputDate > CONVERT(VARCHAR(10), '24/01/2013', 103)
Throwing the same error
Is there a way to convert string in dd/mm/yyyy to be recognize as datetime format in SQL SERVER? Or the only way, and the proper way is doing santization elsewhere?
have you tried using DATETIME instead of VARCHAR(10)
WHERE inputDate > CONVERT(DATETIME, '24/01/2013', 103)
Try to use the information in this post : ISO 8601
I succeed to do the following :
select convert(datetime,convert(char(23),'20140125' ,127),103) as MyDate
to get this :
2014-01-25 00:00:00.000
Use ISO 8601 date format YYYY-MM-DDT00:00:00. It will be implicitly converted to datetime in any locale
Try to increase the length of your VARCHAR(10) to VARCHAR(14) like:
select inputDate from Table
where inputDate > convert(varchar(14), '24/01/2013', 103)