Working with dateadd in Sybase Stored Proc - tsql

For my requirement, based on input date, two values are calculated in vb.net code.
DateAdd(DateInterval.Day, -1, inputdate.Value.AddDays((inputdate.Value.Day - 1) * -1).AddMonths(1))
inputdate.Value.AddDays((inputdate.Value.Day - 1) * -1).AddMonths(1)
I want to implement above same logic in Sybase ASE procedure. Could anyone help me on this.
I need to have these 2 as functions , based on input date ,these two dates has to be calculated

Related

Custom date format iseries access odbc for SSRS

How to resolve my issue below?
1. I am pulling data from AS/400 DB2 using (iseries access odbc driver) to SSRS.
2. I want to format the column stores in integer to date format.
Sample = 20180612 below is the SQL Query.
Using below query,
SELECT CHAR(DATE(SUBSTR(DIGITS(20180612),1,4)||'-'||
SUBSTR(DIGITS(20180612),5,2)||'-'||
SUBSTR(DIGITS(20180612),7,2)),ISO) AS RESULTSDATE
Output = 2018-06-02
Question: How can I produce a below custom date format like d/m/yyyy
I have used, ISO, USA, LOCAL, JIS, EUR but no yield.
Example: 2/6/2018
If you want to do it in SQL Server, the closest you can CONVERT it to is the USA 110 (mm-dd-yyyy) in SQL Server.
If the your dataset has a field with the 2018-06-02, you can use the CDATE function is SSRS to convert it to a date and then format the text box the way you want with the FORMAT property or the FORMAT function.
Format Function:
=FORMAT(CDATE("2018-06-02"), "M/d/yyyy")
Format Property of Text Box:
/* this is as close as I can come to the desired results there is a leading zero */
select
varchar_format(
timestamp_format(char(20180612), 'YYYYMMDD')
, 'DD/MM/YYYY') as resultdate
from my8digitdatetable
/* for export IRL send out a date */
select
cast(
timestamp_format(char(20180612), 'YYYYMMDD')
as date) as resultdate
from my8digitdatetable
I got it already. I just format it to the date without formatting.
SELECT DATE(SUBSTR(DIGITS(MIN(DTAHRS.HREMCP.EMREDT)),1,4)||'-'||
SUBSTR(DIGITS(MIN(DTAHRS.HREMCP.EMREDT)),5,2)||'-'||
SUBSTR(DIGITS(MIN(DTAHRS.HREMCP.EMREDT)),7,2)) AS RESULTSDATE
After that, I put this as sub-query so that I can pass this to the parameter.
Below is my whole query, then at SSRS I can use date/time parameter control to compare with the results.
SELECT x.RESULTSDATE
FROM ( SELECT DATE(SUBSTR(DIGITS(MIN(DTAHRS.HRLVTP.LTLDTE)),1,4)||'-'||
SUBSTR(DIGITS(MIN(DTAHRS.HRLVTP.LTLDTE)),5,2)||'-'||
SUBSTR(DIGITS(MIN(DTAHRS.HRLVTP.LTLDTE)),7,2)) AS RESULTSDATE
) as x
WHERE x.RESULTSDATE >= ? AND x.RESULTSDATE <= ?
I hope it will help you too.

To filter sales based on max date value minus 6 months

I'm trying to filter the data based on the date filter. The date column in my table is in date9. format(30JUN2017).
I want to filter the date column by subtracting 6 months from the existing date, which will be 31DEC2017.
e.g: date<31DEC2017
Can anyhow suggest on how to do this, I have tried using intx function and other options as well.
Thanks for your help.
Best Regards,
AJ
When manipulating dates in SAS using their formatted values you should enclose them in ""d, i.e. in your example it should be where date<"31DEC2017"d. If your date is stored in date9 format as a string, you can make it into a date like so: where input(date,date9.)<"31DEC2017"d
Now your question seems to differ slightly from its title. According to the latter, you want to filter on max_date - 6 months, whatever that max date might be in your dataset. This could be done so:
proc sql;
select *
from t
where intnx('month',date,6)<(select max(date) from t)
;
quit;
If however, for some strange reason, by "max date" you mean the current date, the above query simply becomes this:
proc sql;
select *
from t
where intnx('month',date,6)<date()
;
quit;

Search data between two timestamp in oracle

I am trying to search data between two dates, but it retrieving old data where not in the range i gave? kindly advice me how to re-write the below query logic
select *
from Users
where TO_CHAR(Users.lastloggedindate,'dd-MON-yy HH.MI.SS') between '17-JAN-17 07.16.55' and
'18-JAN-17 09.20.22';
Try using to_date() instead of to_char(), to convert the two points of the range into dates against which your logged in date can be compared:
select *
from Users
where Users.lastloggedindate between to_date('2017-17-01 07:16:55', 'YYYY-DD-MM HH24:MI:SS') and
to_date('2017-18-01 09:20:22', 'YYYY-DD-MM HH24:MI:SS');

Formatting using FormatDateTime for running SQL Query in MS Access

I have dates in TakeDate Field (Data type : Date ) in a table of MS Access.
I have a text Box txtGdateTo in a search form with Format set to Short Date
When I run SQL queries like as below, I don't get desired data filtered.
Select * from tblWork WHERE TakeDate <= Me.txtGdateTo
Hence I tried the below SQL, but it does not work too.
Select * from tblWork WHERE TakeDate <= Format(Me.txtGdateTo, "\#mm\/dd\/yyyy\#")
I added another filed to the table TakeDate0 with function
TakeDate0: FormatDateTime([TakeDate],"\#mm/dd/yyyy\#")
This gives me value as #error.
Q : Is there any best way to manage these errors using date field?
Q : What formatting needs to be done and is there a need for the additional column?
In my case, the users will enter data in the table in the format dd/mm/yyyy
Me can't be used in a query. So try:
Select * From tblWork Where TakeDate <= Format(Forms!YourForm!txtGdateTo, "\#mm\/dd\/yyyy\#")
In VBA you must format the string expression of the date before concatenating, like:
SQL = "Select * From tblWork Where TakeDate <= #" & Format(Me!txtGdateTo.Value, "yyyy\/mm\/dd") & "#"
After researching for about 2 hours and some hints from #Gustav answers, I was able to figure out the solution to the problem.
1st - Use function FormatDateTime and not Format
Select * from tblWork WHERE TakeDate <= #" & FormatDateTime(Me.txtTdateTo, 2) & "#"
2nd - Make sure the format is correct
txtGdateTo in a search form, Format set to Short Date
3rd - No need for another column for searching functionality.

TSQL format the date difference to be "YY years MM months and DD days"

Ok this is a little bit of a strange request. We are trying to get a formatted "age" statement to come out in a report in PeopleSoft, it provides a "TSQL" based builder which is fairly unhelpful. We cannot use stored functions and we cannot edit the entire SQL statement as one thing. All we can do is say field by field what the formula is, then the tool will join it all the elements together to produce the query.
So, given that restriction how can we get the difference between two dates to be formatted as a nicely human readable sentence
eg. "14 years, 3 months and 10 days"
Any ideas would be greatly appreciated.
If you can use string concatenation, this will give you the number of years:
DATEDIFF(yy, t.startdate, t.enddate)
This will give you the months:
DATEDIFF(mm,
DATEADD(yy,
DATEDIFF(yy,
t.startdate,
t.enddate),
t.startdate),
t.enddate)
And this will give you the days value:
DATEDIFF(dd,
DATEADD(mm,
DATEDIFF(mm,
DATEADD(yy,
DATEDIFF(yy,
t.startdate,
t.enddate),
t.startdate),
t.enddate),
DATEADD(yy,
DATEDIFF(yy,
t.startdate,
t.enddate),
t.startdate)),
t.enddate)
You can see & test using this saved query I posted on the StackExchange Data Explorer. It uses Azure, with uses TSQL...
You can use whatever formula you like. You simply put the return type, and the SQL function in the expression tab, then use the expression as a field.