Getting error ERROR: date/time field value out of range: "31 APR 2001 in Postgres - sql

I have a varchar field where dates are stored in the database where I need data from with values like 31 APR 2001 I run the following query
select date(trim(contact_date)) from clients where date(trim(contact_date)) < '2002-03-12';
Whenever it hits 31 APR 2001 I get the error ERROR: date/time field value out of range: "31 APR 2001
Are there any workaround so that I won't get this error. Since all other dates get return perfectly.
The version of postgres I use is
PostgreSQL 8.1.22

There are 30 days in the month of April.

there is no 31 in April month, prevent it from hitting 31 check it before query

Related

How to create a rolling period-over-period comparison in Redshift SQL

I have the following query that pulls all records from a Redshift table from January 1st of the current year through the final date of the most recent, full quarter.
SELECT *
FROM table
WHERE date_value BETWEEN DATE_TRUNC('year',getdate()) AND DATE_TRUNC('quarter',dateadd(day,-1,getdate()));
I now want to create a period-over-period comparison query that returns all records for the previous n months. Ex. if the first query returns all records for Jan - Jun 2022, this query will return all records for Jul - Dec 2021.
Here is what I have so far, however it currently returns Jan - Jun 2021 instead of the desired date range. I've tried playing around with DATEDIFF() instead of DATEADD() but haven't had any luck with that either. Any help is much appreciated.
SELECT *
FROM TABLE
WHERE date_value BETWEEN DATE_TRUNC('year',dateadd(year,-1,getdate())) AND DATE_TRUNC('quarter',dateadd(year,-1,getdate()));

Convert string to date SSRS

I have issue converting parameter string to date.
I have list of data as:
Jul 28 2017 Call
Jan 8 2018 SMS
Apr 24 2018 Call
Jul 2 2018 E-Mail
Jul 13 2018 Call
Oct 1 2018 Call
Nov 27 2018 E-Mail
Dec 31 2018 Call
Jan 1 2019 SMS
Apr 1 2019 SMS
Jun 4 2019 SMS
I want them to be presented as eg. 06/04/2019 if possible
I tried LSet(Format(Parameters!DateInfo.Label,"MM/dd/yyyy"),12) in expression, but when I run the report it is showing me just like this MM/dd/YYYY.
For your parameter, you probably should be using a dataset for the dates with the Value set to use a date field and the Label using the string representation of the date (CONVERT(CHAR(10), THEDATE, 110) AS DATE_LABEL).
I'm guessing the user isn't typing the parameter values in which means that there's already a dataset for the dates. Add another column to the dataset with the date as a date field to use as the Value while using the text as the Label.
If you cannot fix that and still need to convert the text field into a date, you could use the CDATE function which will convert text into a date field.
=Format(CDATE(LEFT(Parameters!DateInfo.Label, 12)),"MM/dd/yyyy")

Get the latest date in SQL from text format

I'm trying to get the latest date from a csv file , the dates are stored in this form
NOV 14 2010
FEB 1 2012
JUN 18 2014
and my query is like
SELECT Max(date) from table
I'm getting
NOV 14 2010
any idea ?
They are likely being considered strings(varchar) not DateTimes. Try:
SELECT MAX(CAST(TABLE.date as DateTime)) FROM TABLE

Sorting records based on modified timestamp?

I am trying to sort a list of records that have been created using a screen scraping script. The script adds the following style of date and time (timestamp) to each record:
13 Jan 14:49
The script runs every 15 minutes, but if I set the sort order to 'time DESC' it doesn't really make sense because it lists the records as follows:
13 Jan 14:49
13 Jan 12:32
13 Jan 09:45
08 Feb 01:10
07 Feb 23:31
07 Feb 06:53
06 Feb 23:15
As you can see, it's listing the first figure correctly (the day of the month in number form) but it's putting February after January. To add to the confusion it's putting the latest date in February at the top of the February section.
Is there a better way of sorting these so they are in a more understandable order?
If you are storing the values in a database, simply use the column type datetime when creating the field. The database will treat the field as time and will sort the values chronologically.
Otherwise, if you are storing the values elsewhere, for example in a flat file, convert the formatted time to unix time. Unix time is an integer, thus you can sort it easier.
Time.parse("13 Jan 09:45").to_i
# => 1326444300
Time.parse("08 Feb 01:10").to_i
# => 1328659800
You can always convert a unix time to a Time instance.
Time.at(1328659800).to_s
# => "2012-02-08 01:10:00 +0100"

SQL select maximum from two time periods

I have a query, Im trying to select the maximum value from the summer period (nov-april down here) but it only gives me values from nov-dec with this query. Any ideas why?
SELECT TOP 10 Value, DateTime
FROM history
WHERE Tagname = #Tag
AND
((DateTime >= #StartYear AND DateTime < #StartWinter)
OR
(DateTime >= #FinishWinter AND DateTime < #FinishYear))
ORDER BY Value DESC
(DateTime >= startYear AND datetime < startwinter) gives you all results between jan and april 2009.
(Datetime > finishwinter and datetime < finishyear) gives all results in nov dec 09.
So, you're selecting top 10 from Jan Feb March April Nov Dec 2009. If that's what you want to select from, and you're only getting values in Nov Dec 2009, check to see that there should be values in the other months?
If #startwinter isn't year-sensitive you might also get jan-apr 2010.
Shouldn't you use a 'ORDER BY' when using 'TOP 10'?
And what locale do you live in, or, to rephrase it: what are reasonable dates for (#StartYear, #StartWinter, #FinishWinter, #FinishYear)
In Europe I expect:
StartYear = 2010-01-01
FinishYear= 2010-12-31
StartWinter=2010-12-20 (about)
FinishWinter=2010-03-20 (about)
So the first period would go from 2010-01-01 to 2010-12-20 (about) and the second from March 2010 to End of year.
So this would include the whole year, and most of it, from 03.20 to 12.20 double.
Hey thanks for the help everyone, it seems this is a problem with our historian (a linked db from sql server) so Ill take the issue up with them. I tried the query on a regular mssql db and it seeems fine...