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'
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));
`
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'
I have a table which is having timestamp column as follows
ID timeStamp var1 var2 var3
1 05-09-2013 18:23:56 0 1 1
2 05-09-2013 18:23:57 0 0 1
3 05-09-2013 18:23:58 1 0 1
4 05-09-2013 18:23:59 1 0 1
I want to retrieve according to timestamp like timestamp>05-09-2013 18:23:56 and timestamp <05-09-2013 18:23:59
I am trying these ways
select * from test where timeStamp<'05-09-2013 18:23:59' and timeStamp>'05-09-2013 18:23:57'
Its giving null rows.I had tried using between keyword still not getting the desired result.
SELECT * FROM test
WHERE (timeStamp BETWEEN '05-09-2013 18:23:57' AND '05-09-2013 18:23:59')
May i know what wrong I am doing here?
here is the screen shot
This screenshot is without where clause
This screen shot shows table DESCRIPTION
Use appropriate datetime literals that don't break your queries. More and detailed explanations at Bad habits to kick : mis-handling date / range queries
SELECT *
FROM test
WHERE ( timeStamp BETWEEN '2013-09-05T18:23:57' AND '2013-09-05T18:23:59' ) ;
Also note that a timestamp with time part above 18:23:59.000, like '18:23:59.100' will not be included in the results because the above condition is equivalent to:
WHERE timeStamp >= '2013-09-05T18:23:57.000'
AND timeStamp <= '2013-09-05T18:23:59.000'
So, you may want this instead:
SELECT *
FROM test
WHERE timeStamp >= '2013-09-05T18:23:57.000'
AND timeStamp < '2013-09-05T18:24:00.000'
The use of BETWEEN in date and datetime comparisons is not advised, for this reasons. See another blog post by #Aaron Bertrand: What do BETWEEN and the devil have in common?
I'm trying to query a table that has a varchar(100) "VALUE" column. This column can hold anything from a letter, a number or, in this case, a date.
The date will always be entered in the table as 'YYYY-mm-dd'. However, when I run the following query:
select * from myTable
where VALUE = '2009-12-11' (Date, Format 'yyyy-mm-dd')
I receive the following error:
Invalid date supplied for myTable.VALUE.
Example of the value table:
(1,'122')
(2,'red')
(3,'2009-12-11')
Any ideas as to what might be causing this?
Thanks!
if the data type is declared as varchar, it should just treat it like a string.
try not specifying anything about the date format, like
select * from myTable
where VALUE = '2009-12-11'
If you run an explain on the query, you can see that it's casting value to date before comparing against your supplied value. If you have another column that accurately records the type of what's in VALUE, you can add that to the where clause and you will no longer get the error (see below). Otherwise, go with Beth's recommendation.
select * from myTable
where VALUE = '2009-12-11' (Date, Format 'yyyy-mm-dd')
and VALUE_TYPE = 'DATE';
Teradata internal date calculation is (year - 1900) * 10000 + (month * 100) + day.
So if date is 02/11/2009 (2nd November 2010) then
=(2009-1900) * 10000 + (11 * 100) + 2
=109 * 10000 + 1100 + 2
=1090000 + 1100 + 2
=1090000
1100
2
----------
1091102
----------
So 2nd november 2009 is stored in Teradata as 1091102.
You can extract it in required format by casting (as u have it in varchar). Hope this helps.
Is it possible that VALUE is a reserved word in Teradata?
If so, you need to put that into double quotes:
select *
from myTable
where "VALUE" = '2009-12-11'