I want to add a new dataset into an existing table. My SQL statement is as follows:
INSERT INTO Application ( App, Load_Date, Source)
VALUES ('unknown App', '0001-01-01 00:00:00.000','SYSTEM');
I get this Errormessage: The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
I tried converting with CONVERT(VARCHAR(23), '0001-01-01 00:00:00.000', 120) but it didn't work.
The statement works if I use getdate() oder sysdatetime() though...
What am I missing?
What's the specific data type of your date/time column? If it's datetime, for example, then 0001-01-01 00:00:00.000 is out of range, specifically 1753-01-01 through 9999-12-31.
You could use a datetime2 data type instead because that supports a range of dates between 0001-01-01 00:00:00.0000000 and 9999-12-31 23:59:59.9999999. See this page for more information.
Related
I have a database with a date and a time stored separately in datetime columns (not my idea). I need to combine their values to put into another table with a datetime column. As simple as this seems, I just don't seem to be able to do it.
I can get my date value:
cast(sampledate as date) -- returns '2014-11-01'
And my date value:
cast(CollectionTime as time) -- returns '06:46:00.0000000'
I've tried a few different ways of putting them together that look OK.
For example:
concat(cast(sampledate as date) , ' ' , cast(CollectionTime as time)) -- returns '2014-11-05 08:14:00.0000000'
But when I try to insert this into a datetime column, or even just cast it as a datetime value, it doesn't work:
cast(concat(cast(sampledate as date) , ' ' , cast(CollectionTime as time)) as datetime)
-- I get the error 'Conversion failed when converting date and/or time from character string.'
This link warned me against using the FORMAT function, and I've been to some other sites that tell me what NOT to do, but I just can't seem to do this simple thing. Can anyone help? Thanks.
EDIT: Figured it out. This link solved it for older versions of SQL, but not current versions. However, it works fine if you cast to datetime2(0), not datetime.
As I commented above, here is an example where you can add the two datetimes together.
If either column is NOT datetime, simply convert that column to a datetime
Declare #YourTable table (sampledate datetime,CollectionTime datetime)
Insert Into #YourTable values
('2019-06-25 00:00:00','09:09:31')
Select *
,NewDateTime = sampleDate + CollectionTime
From #YourTable
Results
sampledate CollectionTime NewDateTime
2019-06-25 00:00:00.000 1900-01-01 09:09:31.000 2019-06-25 09:09:31.000
I have a table that has a field Report_Date. This field is a bigint type. I have another table that has ReportDate that is datetime type. I want to combine data from each table, but I want the bigint converted into a datetime.
I tried SELECT DATEADD(DD, convert(bigint, Report_Date), Report_date) however I get the error message:
Arithmetic overflow error converting expression to data type datetime.
I have also tried SELECT DATEADD(DD, convert(bigint, Report_Date), convert(datetime, Report_date)) with the same error message result.
I expect the date time to be 2019-02-28 00:00:00.000.
For your example you would need to do something like this.
select convert(datetime, convert(char(8), 20190108))
I can't for the life of me figure out what you are trying to do with your DATEADD logic there.
To cast bigint/int to datetime you firstly need to cast it to varchar. You can do it i.e. like this:
select cast(cast(Report_Date as varchar(80)) as datetime)
Hi i've a column the_date which is having sample data like
1900-01-01 00:00:00.000
1990-01-01 00:00:00.000
1990-01-02 00:00:00.000
1990-01-03 00:00:00.000
1990-01-04 00:00:00.000
1990-01-05 00:00:00.000
1990-01-06 00:00:00.000
1990-01-07 00:00:00.000
now i just want to select only date only and displaying it into 103 style and convert the column into Date format. i've tried this syntaxconvert(varchar,THE_DATE , 103) but then the column is not in Date format.
any help please.
This Works try it once
SELECT CONVERT(varchar,CAST(DATE_TIME AS DATE),103)AS Date_Time From <yourTable>
It's a little confusing because regardless of the datatype, you will always get the same answer with CONVERT. I'll illustrate this with 2 declared variables, 1 datetime datatype, the other date datatype:
DECLARE #mydatetime datetime = '1990-01-01 00:00:00.000'
DECLARE #mydate date = '1900-01-01'
SELECT convert(varchar,#mydatetime, 103) as mydatetime
SELECT convert(varchar,#mydate, 103) as mydate
Produces:
mydatetime
01/01/1990
mydate
01/01/1900
So you don't need to cast to date, then to 103 format.
If you don't like the time with date in your table (datetime) then you can always ALTER the column in the table to the date datatype. This can be done using SSMS or you can do the SQL:
ALTER TABLE mytable ALTER COLUMN mycolumn DATE
Where DATE is the new datatype. And then only the date is stored in the table.
Converting a string (or an equivalent database type) to another type is called "parsing". Converting another type to string is called "formatting".
The DATE or DATETIME type (or equivalent type in the front-end) does not store dates as string and has therefore no format. A number is used to represent the date internally which counts the days since a reference date (1753-01-01 for SQL-Server). The time is represented as decimal fraction.
Of course you always see the date as formatted when you open the table, because it is converted to a text for display, but it is not stored as formatted.
So, what you have to do if your date is given as text, is to parse it using the 121 format (YYYY-MM-DD HH:MI:SS.MMM (24h)) to get a DATE (or DATETIME).
CONVERT(DATE, the_date, 121) or CONVERT(DATETIME, the_date, 121)
If the_date is already of DATE (or DATETIME) type and you want to display it in the 103 format (DD/MM/YYYY)
CONVERT(VARCHAR(10), the_date, 103)
If the date is given as DATETIME type and you just want to strip off the time part and return the result as DATETIME type again, you can do
DATEADD(dd, DATEDIFF(dd, 0, the_date ), 0)
Note that no date format is involved here, since the date does never appear as text.
thanks for all your answer but for anyone who has my same query i got the solution from here
solution
I'm trying to just do a simple check against a column in the table:
If (endDate is null)
use smalldatetime '12/31/2200'
else
use endDate from column
Here is my stored procedure:
ALTER PROCEDURE [dbo].[getCostByDate] #date smalldatetime, #productID int
AS
SELECT cost
FROM testDB.dbo.product_cost
WHERE #date between startDate and isNull(endDate,cast('12/31/2200' as smalldatetime)) and product_id = #productID
I tried to 'cast' the '12/31/2200' to format it for a smalldatetime, but I'm getting the error:
"The conversion of a varchar data type to a smalldatetime data type resulted in an out-of-range value."
The problem is that the date range for smalldatetime is 1900-01-01 through 2079-06-06 and your value is past the upper bound. The solution is to use a value inside the range, or another date type like datetime or datetime2.
cast('12/31/2078' as smalldatetime) would work for instance.
Here's the key:
and isNull(endDate,cast('2029-04-25T15:50:59.997' as smalldatetime))
You have to give the whole date even though it's a smalldatetime... Also, you can't use 2200, it's too far in the future I guess...
I have a very simple query. (I'm working in SQL Server 2008.) I'm trying to select all records from a view where their ModifiedOn column is greater than a specified date. The ModifiedOn column is a datetime format. So, I have:
DECLARE #date1 AS datetime = '2013-07-31 24.59.59.999'
SELECT
some_column
FROM dbo.some_view
WHERE ModifiedOn > #date1
SQL is throwing the following error, though:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. Why is SQL thinking that one of my dates is a varchar, when I know that both of them are datetime formats? How do I fix it?
Datetime variable expects data to be in format of
YYYY-MM-DD HH:MM:SS.mmm
In your case you are trying to assign a value which is not a valid time. HH.MM.SS.mmm
Secondly a clock never strikes 24:00:00 it goes from 23:59:59.999 to 00:00:00.001.
Also in your case rather than juggling with seconds and milliseconds. just use date value and use the ANSI standard YYYYMMDD which is also sargable.
You could have written your above query something like
SELECT some_column
FROM dbo.some_view
WHERE ModifiedOn >= '20130801'