How to convert a 5 digit INT to date - tsql

I apologise if this question has been asked but I cannot find an answer that quite fits.
I have a database with dates stored as 5 digit integers. I can covert these to datetime, however the dates are showing in the future.
For example,
select convert(datetime,StartDate,103)
from dpm.Schedule
where ScheduleID like 50003;
Gives the results of
2107-05-31 00:00:00:000 but this date should actually be 26/05/2008.
I am pretty new to T-SQL and have looked for sometime to find the answer to this but I am reaching the end of my sanity.

We cannot answer because you are doing a lot of confusion. To start 5003 is the Id of the record and [StartDate] is the value you are trying to convert.
Also drop using operator LIKE in the Id
Example:
select convert(datetime,50003,103)
returns:
2036-11-26 00:00:00.000
but you are trying to convert the value returned by
select StartDate
from dpm.Schedule
where ScheduleID = 50003;
Please edit your question and show us a nice example using SQL Fiddle

Related

How Can I compare table column by convert it first in postgresql + typeorm

I am beginner in Backend technology and I am developing one query in Typeorm QueryBuilder + PostgreSQL.
My query is look like this :
But I can't convert my timestamp into following format. Can anyone who has expertise in it, please help me to select all records with timestamp in specific format.
I am suffering this things from last two days but still not find any solution. Actually I want to compare this date format in having clause. so I want to first select that date format then I can use that same method to compare column with current date and previous dates.

PostgreSQL TO_DATE date/time field value out of range

I am using TO_DATE in one of my PostgreSQL functions and it is throwing errors like date/time field value out of range: "2021901". This is happening for the months of January to September as I need to add zeros in front of them. So I tried to execute a simple select query there as follows as I am using the same syntax in function.
SELECT TO_DATE(2021::varchar||09::varchar||'01','YYYYMMDD')
This is also giving me the error
ERROR: date/time field value out of range: "2021901"
SQL state: 22008
Now if I change the month to October, November, or December it works fine, but for all the other months, it is showing this error. I am actually new to Postgres and not sure how to fix this. It would be very much helpful if someone can point me in the right direction. Thanks
If your input values are numbers (integer), another alternative is to use make_date()
make_date(2021,9,1)
A better and easier way would be to just provide the date correctly and use TO_DATE, so as example do this:
SELECT TO_DATE('2021-09-01', 'YYYY-MM-DD')
If you really want to go your way, you can force a leading zero by using TO_CHAR, like this:
SELECT TO_DATE(2021::varchar||TO_CHAR(09, 'fm00')||'01','YYYYMMDD')
But I recommend to take the first propose.

Cast varchar as date select distinct top 100

I am trying to fix a query that has come to light in SSRS after the new year. We have an input that comes from another application. It grabs a date and stores it as varchar. The SSRS report then fetches the top 100 'dates' but when 2017 dates have come around, this are not in the top 100.
The existing query is as follows
SELECT DISTINCT TOP (100)
FROM DenverTempData
ORDER by BY Date DESC
The date is stored as VARCHAR. So obviously this query doesn't grab a value such as 01012017 as being a top 100 (over values likes 12312016). I thought maybe I can simply change the datatype on this column to datetime. But the information comes from a flat file and is converted, so it's a little more difficult that that. So I'm hoping to do a select of the distinct top 100 while converting the date column to datetime or just date and grabbing the last 100 dates.
Can someone help with the query syntax? I'm thinking a cast to convert varchar to date, but how do I format with distinct top 100? I'm simply looking to retrieve the last 100 dates in chronological order from a column that is stored as varchar but contains a string representing a date.
Hopefully that makes sense
It is always a bad idea to store a date as string. This is highly culture specific!
You can cast your formatted string-date to a real date like this:
DECLARE #DateMMDDYYYY VARCHAR(100)='12312016';
SELECT CONVERT(DATE,STUFF(STUFF(#DateMMDDYYYY,5,0,'-'),3,0,'-'),110)
After the conversion your sorting (and therefore the TOP 100) should work as expected.
My strong advise: Try to store your dates in a column of a real date type to avoid such hassel!
SELECT DISTINCT TOP 100 (CAST(VarcharColumn as Date) as DateColumn)
FROM TABLE
Order by DateColumn desc

IBM i (AS400/ISeries) - Adding days to date field in WRKQRY

I have a decimal date field (TDDATR) that is in the YYYYMMDD format.
I would like to create a field that is TDDATR + 30 days but I am unable to.
Using 'Define Results Field' I have tried a few things;
Simply doing this;
TDDATR + 30 DAYS
But it returned this error: Labeled duration not used correctly.
I tried using the DIGITS and SUBSTR commands to create a field in the DDMMYYYY format and then +30 days but got the same error.
Same as above but in the DD/MM/YYYY format - same error.
Using DATE(TDDATR) but all I see is +'s in the field.
Using DATE( ) on the fields created in step 2 and 3 - still get +'s
I've ran out of ideas - any help would be greatly appreciated.
Query/400 lacks a lot of the features that an SQL based interface has.
I'd urge you to consider switching to Query Manager (STRQM) which is a fully SQL based product. You can even convert Query/400 queries to Query Manager queries with the RTVQMQRY command by having the ALWQRYDFN parm set to *YES.
The other option that IBM is pushing is Web Query. Again, fully SQL based and you can convert Query/400 queries into it.
Having said that, the problem is that FLD + 30 DAYS only works when FLD is a DATE data type. Query/400 includes a DATE() function to convert non-date types into date. But it's very limited in that it only works with character fields formatted according to your job defaults. Assuming you're in the US, it'd only work with a character value of '07/01/15'.
You could do a lot of manipulation in Query/400 and end up with a result field that meets DATE()'s requirements. But a better solution would be to create an SQL view over your table and have your numeric date converted into a date data type in the view.
You can find code examples that show how to convert a numeric YYYYMMDD to a actual date data type in the view. However, I'd recommend create a user defined function (UDF) that will do the conversion for you. That will make it much easier to use in the view and to reuse in other places.
If you'd like, there's an open source package called iDate, that includes all the code required for convert to/from date data types.
Download that, install/compile it and your SQL view becomes
select ... idate(TDDATR,'*CCYMD') as TD_DATE
from myfile
The use of days is as follow
Field Expression
CURDATE_30 days(current(date)) + 30
The solution to your problem is: given the field A dec(8,0)
Field Expression
YYYYMMDD_ date(substr(digits(a),5,2)||'/'||
substr(digits(a),7,2)||'/'||
substr(digits(a),3,2))
NEXT_MONTH DAYS(YYYYMMDD_) + 30
Remember to check the date format in your job description. In the example the format is MDY or MM/DD/YY.
More info here
Based on the information here, I created the below 2 fields;
TDDIGI DIGITS(TDDATR)
TDDAT1 SUBSTR(TDDIGI,7,2)||'/'||
SUBSTR(TDDIGI,5,2)||'/'||
SUBSTR(TDDIGI,3,2)
From here I was able to create a date field;
TDDAT2 DATE(TDDAT1)
Which allowed me to perform the necessary calculations.
The format of TDDAT1 is based on your job description which can be found by;
WRKJOB
Option 2
Page down
Date format..: X
Mine was *DMY, so TDDAT1 was formatted based on this.

Number of days between past date and current date in Google spreadsheet

I want to calculate the number of days passed between past date and a current date. My past date is in the format dd/mm/yyyy format. I have used below mentioned formulas but giving the proper output.
=DAYS360(A2,TODAY())
=MINUS(D2,TODAY())
In the above formula A2 = 4/12/2012 (dd/mm/yyyy) and I am not sure whether TODAY returns in dd/mm/yyyy format or not. I have tried using 123 button on the tool bar, but no luck.
The following seemed to work well for me:
=DATEDIF(B2, Today(), "D")
DAYS360 does not calculate what you want, i.e. the number of days passed between the two dates – see the end of this post for details.
MINUS() should work fine, just not how you tried but the other way round:
=MINUS(TODAY(),D2)
You may also use simple subtraction (-):
=TODAY()-D2
I made an updated copy of #DrCord’s sample spreadsheet to illustrate this.
Are you SURE you want DAYS360? That is a specialized function used in the
financial sector to simplify calculations for bonds. It assumes a 360 day
year, with 12 months of 30 days each. If you really want actual days, you'll
lose 6 days each year.
[source]
Since this is the top Google answer for this, and it was way easier than I expected, here is the simple answer. Just subtract date1 from date2.
If this is your spreadsheet dates
A B
1 10/11/2017 12/1/2017
=(B1)-(A1)
results in 51, which is the number of days between a past date and a current date in Google spreadsheet
As long as it is a date format Google Sheets recognizes, you can directly subtract them and it will be correct.
To do it for a current date, just use the =TODAY() function.
=TODAY()-A1
While today works great, you can't use a date directly in the formula, you should referencing a cell that contains a date.
=(12/1/2017)-(10/1/2017) results in 0.0009915716411, not 61.
I used your idea, and found the difference and then just divided by 365 days. Worked a treat.
=MINUS(F2,TODAY())/365
Then I shifted my cell properties to not display decimals.
If you are using the two formulas at the same time, it will not work...
Here is a simple spreadsheet with it working:
https://docs.google.com/spreadsheet/ccc?key=0AiOy0YDBXjt4dDJSQWg1Qlp6TEw5SzNqZENGOWgwbGc
If you are still getting problems I would need to know what type of erroneous result you are getting.
Today() returns a numeric integer value: Returns the current computer system date. The value is updated when your document recalculates. TODAY is a function without arguments.
The following worked for me. Kindly note that TODAY() must NOT be the first argument in the function otherwise it will not work.
=DATEDIF( W2, TODAY(), "d")
Today() does return value in DATE format.
Select your "Days left field" and paste this formula in the field
=DAYS360(today(),C2)
Go to Format > Number > More formats >Custom number format and select the number with no decimal numbers.
I tested, it works, at least in new version of Sheets, March 2015.