I have a view xyz_view which has around 16,000 records.
There are four date columns which have data in this format '2015-04-30 00:00:00.000'
When I use query
select *
from xyz_view
I get all the records without any problem but when I use
select top 1000 *
from xyz_view
I get an error:
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value '2013-08-05 00:00:00.0' to data type int.
Even select top 100 * from xyz_view works. Anything over 300 is throwing above error.
Please help me understand on this.
I think something in data type of four columns are not datetime type. Because your value is: '2013-08-05 00:00:00.0'
If is datetime type, it is: '2013-08-05 00:00:00.000' (3 numbers for milliseconds)
In view, you can convert or cast columns to datetime type, like this:
CAST(columns AS DATETIME)
CONVERT(DATETIME, columns)
Your top < 1000 doesn't show an error because it doesn't fetch the row with erroneous data.
The error check is only running when you fetch the row where one of your column with data type int got a date value in it - 2013-08-05 00:00:00.0
Check this record by running a Select statement.
SELECT * FROM myTable
WHERE myColumn = '2013-08-05 00:00:00.0'
Base on the record you see, decide then if you would change the erroneous 2013-08-05 00:00:00.0 to any acceptable int value
UPDATE myTable
SET myColumn = 123
WHERE myColumn = '2013-08-05 00:00:00.0'
Related
I have a table with a varchar(25) column that holds a date value. A typical value is '11/04/2017'.
This query returns 0 rows
select *
from myTable
where isdate(inputDate) = 0
I am trying to find a max on this, using a date sort.
This query returns the expected result
;with gooddates as
(
select
medcomfolder, PatientId, PatientBirthday, InputDate
from
myTable
where
isdate(inputDate) = 1
)
select max(convert(datetime, inputDate))
from gooddates
This query returns an error.
;with gooddates as
(
select
medcomfolder, PatientId, PatientBirthday, InputDate
from
dwhFuData
where
isdate(inputdate) = 1
)
select max(convert(date, inputdate))
from gooddates
This is the returned error
Msg 241, Level 16, State 1, Line 274
Conversion failed when converting date and/or time from character string
The difference between the 2 queries is that the first is converting to a dateTime while the latter is converting to a date.
At this point, I can move forward w/ the dateTime option, but I am left wondering what I am missing.
I have checked that there are no embedded spaces, and all the columns have a len(InputDate) = 10 (there is NO time data included)
I selected distinct values,put them in excel, and did a date function on each row. I was hoping to get a #VALUE on 1 row. All the rows worked.
So there is nothing silly like '02/31/2019' going on.
How can a dateTime conversion pass when a simple date conversion does not?
My guess is that you have values that include a time stamp following the date (based on the fact that isdate() is always zero).
If so, one simple solution would be to use convert(date, left(inputdate, 10)). Another solution uses try_convert():
try_convert(date, inputdate)
To find the offending values:
select inputdate
from dwhFuData
where try_convert(date, inputdate) is null and inputdate is not null;
I have tried using:
SELECT * FROM dbo.admin_IVM_ITEMMAST WHERE LSTSAL_DATE <= '2016-09-31 00:00:00.000'
But when I execute the query i get this error.
Error:
Msg 242, Level 16, State 3, Line 1 The conversion of a char data type
to a datetime data type resulted in an out-of-range datetime value.
The column name is: LSTSAL_DATE and a typical string inside the column is: 2013-05-02 00:00:00.000
There are only 30 days in september!
SELECT * FROM dbo.admin_IVM_ITEMMAST WHERE LSTSAL_DATE <= '2016-09-30 00:00:00.000'
you can select the date you or from access form's textbox and 20 can be written any number you want if you put 50 instead of 20 you will get people who are older than 50 years.
20 in the code could be a variable to select specific age by number
`SELECT *
FROM Table Name
WHERE ((Year(Now())-Year(date)>20));
`
How to retrieve specific month using date-time field let's say that i have following table
ID - DATETIME - AMOUNT
01 - 27/05/2017 - 1
02- 02/06/2017 - 2
03- 03/07/2017 - 1
04- 05/05/2017 - 2
05- 13/06/2017 - 3
What is the proper way to write a query to retrieve the records of May only
I have used the following query but it gave me error.
SELECT * FROM TABLE WHERE TABLE.DATETIME = '05/2017'
Msg 241, Level 16, State 1, Line 3
Conversion failed when converting date and/or time from character string.
You can use this (but if you have an index on the TABLE.DATETIME column, it will not be used):
SELECT *
FROM TABLE
WHERE MONTH(TABLE.DATETIME) = 5
AND YEAR(TABLE.DATETIME) = 2017
It may be more efficient (if you have or have plans to add an index) to do:
SELECT *
FROM TABLE
WHERE TABLE.DATETIME >= '20170501'
AND TABLE.DATETIME < '20170601'
Check against the start and end of month.
where DateTime >= '20170501' and
DateTime < '20170601'
Or you can use between clause:
SELECT *
FROM TABLE
WHERE TABLE.DATETIME BETWEEN '01/05/2017' AND '31/05/2017'
I want to convert a column of UTC time to local time.
My data looks like this:
time_utc TZID timezone
------------------------------------------------
2014-02-27 12:00:39.0 America/Toronto -5
2013-05-21 09:35:30.0 America/Goose_Bay -4
2015-01-08 06:58:58.0 America/Creston -7
I know that using
select *, DATEADD(hour, 5,time_utc)
from mytable
will add 5 hours to column time_utc.
However, as you can see, I have a variable time zone column.
How can I pass this variable to the dateadd function?
I tried the following 2 commands but they don't work:
Attempt #1:
select *, DATEADD(hour, timezone, time_utc)
from mytable
Attempt #2:
select *, DATEADD(hour, (select timezone from mytable), time_utc)
from mytable
Both throws this error:
Argument data type varchar is invalid for argument 2 of dateadd function. [SQL State=S0001, DB Errorcode=8116]
For decimal values of timezone, for instance -3.5, how would this work?
Thanks
How can I pass this variable to datetime function?
Just reference the column in the function call:
select *, DATEADD(hour, timezone, time_utc)
from mytable
For decimal values of timezone, for instance -3.5, how would this work?
The "number" parameter of DATEADD takes an integer, so you'd have to change to minutes and scale the hour offset. Since your timezone colume is apparently a varchar column, convert it to a decimal value as well:
select *, DATEADD(minute, cast(timezone as decimal(4,2)) * 60 , time_utc)
from mytable
There is a character value (most probably blank) in your dataset. Sql does implicity conversion but for non numeric value it will fail. Check your table to see if you have blanks or non numeric values for timezone
I using a query where I am selecting some attributes from the table based on a where condition. My where condition is-
date>GetDate();
I have tried this-
SELECT TOP 2 img,name,substring(description,1,80) as
description,Convert(nvarchar,date,106) as date
FROM tbl_test
where date>=Convert(nvarchar,GetDate(),106)
order by date Asc;
This query is running fine but showing different result as compared to a different query of similar kind in which I am not converting the date format.
SELECT TOP 2 img,name,substring(description,1,80) as description,date
FROM tbl_test
where date>=GetDate()
order by date Asc;
Please guide me where I am doing wrong?
Your first query will convert getdate() into nvarchar data type and it will compare date with string while 2nd query will compare 2 dates. So 2nd option is better. Still if you want to convert date into string then check then use 102 format like
WHERE CONVERT(varchar(20),date,102) >= CONVERT(varchar(20), getdate(),102)
For select column you can use format which you want like
SELECT CONVERT(varchar(20),date,106)
Final Query is :
SELECT TOP 2
img,
name,
SUBSTRING(description,1,80) as description,
CONVERT(varchar(20),date,106) as [DisplayDate]
FROM tbl_test
WHERE CONVERT(varchar(20),date,102) >= CONVERT(varchar(20), getdate(),102)
ORDER BY date ASC;
Without convert to varchar, you can cast getdate() to date to remove time part :
SELECT TOP 2
img,
name,
SUBSTRING(description,1,80) as description,
CONVERT(varchar(20),date,106) as [DisplayDate]
FROM tbl_test
WHERE date >= CAST(getdate() as date)
ORDER BY date ASC;
SQL Fiddle Demo
DECLARE #Date Datetime;
SET #Date = GETDATE();
SELECT CONVERT(VARCHAR(12), #Date, 113) AS Date
RESULT
╔══════════════╗
║ Date ║
╠══════════════╣
║ 01 Jan 2014 ║
╚══════════════╝
Edit
as Upendra Chaudhari has explained that when you do comparing column Date with a string =Convert(varchar(20),GetDate(),102),
what is actually happening behind the scenes is Convert(varchar(20),GetDate(),102) returns a string 2014.01.01 but to compare this string with a Datetime column SQL Server does an implicit conversion to compare both values. Sql Server have to have both values in the same datatype to compare them.
Now datatype Datetime has Precedence over nvarchar/varchar datatype so sql server converts the string into datetime datatype which returns something like
SELECT CAST('2014.01.01' AS DATETIME)
Result : 2014-01-01 00:00:00.000
Now in this process of converting your values to string and then back to datetime you have actually lost all the time values in your comparing values. and this is the reason why you are getting unexpected results back.
so make sure whenever you are comparing to have exactly the same datatype on both sides and take control of any data conversions in your code rather then sql server doing datatype conversions for you.
I hope this will explain you why you are getting different results .
You may try:
where date>=CONVERT(VARCHAR(11), GETDATE(), 113)