How can I select rows that are older than a specific date? - sql

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));
`

Related

SQl Server Converting to Date fails , DateTime works

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;

Convert from string to Datetime T-SQL

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'm having date conversion problems with the 3rd column in the script below

Select c1.CDS_Date, c2.CDS_Date, Dateadd(day, -1 , c2.CDS_Date)
from cte as c1 left join
cte as c2
on c1.[Local Patient Identifier] = c2.[Local Patient Identifier] and
c1.JoinKey = c2.JoinKey - 1
Can I get come help please?
I got no issues with c1.CDS_Date and c2.CDS_Date.
I receive the following error message as soon as I include the 3rd column:
(24 rows affected)
Msg 242, Level 16, State 3, Line 50
The conversion of a varchar data type to a datetime data type resulted in an --out-of-range value.
That is because your CDS_Date is not of date/datetime type but is a string.
Here is an example:
declare #t table (CDS_Date varchar(100));
insert into #t values ('28/10/2017');
set language British;
select CDS_Date, dateadd(day, -1, CDS_Date) as dt
from #t;
-------
--CDS_Date dt
--28/10/2017 2017-10-27 00:00:00.000
set language us_english;
select CDS_Date, dateadd(day, -1, CDS_Date) as dt
from #t;
--Msg 242, Level 16, State 3, Line 40
--The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
This example shows you that not every string can be converted to date EVEN IF it seems to you that it represents a valid data. This is because server expects a date that you pass as a string to be in the certain format that depends on session language.
To be independent of session language you shoud pass your date string using convert function and specifying the format of your date.
In my example I can fix the issue converting to date with style 103:
select CDS_Date, dateadd(day, -1, convert(date, CDS_Date, 103)) as dt
from #t;
----
--CDS_Date dt
--28/10/2017 2017-10-27
Now it gives me the correct result in both languages just because it knows that my input strings are of 103 format, i.e. dd/mm/yyyy

SQL Server : Histogram of the starts and stops over time

I am trying to get a histogram of the of the starts and stops over time from a subscriber DB, I know what the result should be but I can't make it work.
The code I am trying is:
Unfortunately I get the message
Msg 241, Level 16, State 1, Line 2
Conversion failed when converting date and/or time from character string.
If I try
It doesn`t work either:
Msg 242, Level 16, State 3, Line 2
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
How can I get over this and get a histogram table?
Thanks in advance
As per your comments, the start_date and stop_date columns having null values. So filter those values and instead of CONVERT please use CAST, because the convert you used cause an error for certain values
Sample:
DECLARE #End_date AS VARCHAR(50) = '2016-05-15';
SELECT YEAR (CAST(#End_date as DATETIME));
SELECT YEAR (CONVERT(DATETIME, #End_date, 104 ));
Note: I manually type the code by seeing your image. Please correct if any typo.
So the below code will work for your expectation:
SELECT theyear, SUM(isstart) as starts, SUM(isstop) as stops
FROM (
SELECT YEAR(CAST([start_date] AS DATETIME)) as theyear, 1 as isstart, 0 as isstop
FROM Subscribers
WHERE [start_date] IS NOT NULL
UNION ALL
SELECT YEAR(CAST(stop_date AS DATETIME)) as theyear, 0 as isstart, 1 as isstop
FROM Subscribers
WHERE stop_date IS NOT NULL
)s
GROUP BY theyear
ORDER BY theyear

Getting error only when using select top 1000 query in sql

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'