formatted date and time from postgresql - database

I am using server time for one of my process. For that I am taking date and time using postgresql. The time format I want is 2 digit day,month,hour,minute,second and 4 digit year (eg: 05/01/2015 16:05:30). I am using SELECT to_char(now()::timestamp,'DD/MM/YYYY HH24:MI:SS') I want to make sure that the number of digits for each will be as like I want. Because its very important for my processing. I have refered the following link Link. There it is saying, day of month (01-31) for DD's decription. Is there any possibility to get day as 1 instide of 01

Feel safe.
The table ad your link (http://www.postgresql.org/docs/9.4/static/functions-formatting.html#FUNCTIONS-FORMATTING-DATETIME-TABLE)
is right.
Numbers in date converted with to_char, are 0 padded.

Is there any possibility to get day as 1 instide of 01
You can use regular expression to remove leading 0:
select regexp_replace(to_char(now()::timestamp,'DD/MM/YYYY HH24:MI:SS'), '^0(\d)', '\1', 'g');

Related

How to tell SQL Server to not include results that end with specific letters/numbers

For an assignment I got, I have to create a request that list the flight numbers (varchar), the date of the flight and if there's a lay over (varchar, return NULL if not). However, the conditions to be put are that the date of the flight must be between September 1st 2005 and December 30th 2006 and the flight number must not end with 8 and/or 9.
I came with the following statement:
SELECT ID_VOL, DATE_DEP, ESCALE
FROM VOL
WHERE ID_VOL NOT LIKE ('%8','%9')
AND DATE_DEP BETWEEN '2005-09-01' AND '2006-12-30'
The query doesn't work. I've tried with only one of the ID_VOL condition and it works fine but it doesn't work when I put the second. It works if I put the same condition twice one for each number, but the assignment specify I can only do it with one condition/operator. So I'm kinda stuck on that one.
Your LIKE Expression is wrong.
SELECT ID_VOL,DATE_DEP,ESCALE
FROM VOL
WHERE ID_VOL NOT LIKE '%[89]' AND
DATE_DEP BETWEEN '20050901' AND '20061230';

Average for loop in loop PL / SQL

I am trying to calculate the avarage of durations from the last 40 days for diffrent IDs.
Example: I have 40 days and for each day IDs from 1-20 and each ID has a start date and end date in HH:MI:SS.
My code is a cursor which fetches the last 40 days, then I made a second for loop. In this one I select all the ids from this day. Then I go through every ID for this day and select start and end dat calculating the duration. So far so good. But how do I calculate the avarage of the duration for the IDs in the last 40 days.
The idea is simple. To take the durations for one id (in the last 40 days) add them together and divide them by 40. And then do the same for all IDs. My plan was to make a 2d Array and in the first array putting all IDs, then in the second array to put the duration and add the values for one id together. Then I would have added all the durations for one ID together and get the value from the array. But I am kinda stuck in that idea.
I also wonder if there is a better solution.
Thanks for any help!
From my point of view, you don't need loops nor PL/SQL - just calculate the average:
select id,
avg(end_date - start_date)
from your_table
where start_date >= trunc(sysdate) - 40
group by id
Drawback might be what you said - that you stored dates as hh:mi:ss. What does it mean? That you stored them as strings? If so, most probably bad idea; dates (as Oracle doesn't have a separate datatype for time) should be stored into DATE datatype columns.
If you really have to work with strings, then convert them to dates:
avg(to_date(end_date, 'hh:mi:ss') - to_date(start_date, 'hh:mi:ss'))
Also, you'll then have to have another DATE datatyp column which is capable of saying what "last 40 days" actually means.
Result (the average) will be number of days between these values. Then you can format it prettier, if you want.

How can i get the hour between two datetime in sql?

Below sql showing 57 hours, But,it's 44 hrs. How can i solve.
SELECT DATEDIFF(Hour,'2018-11-20 2:26:38.000','2018-11-22 11:00:29.367')
Use 24 hr format in both dates
SELECT DATEDIFF(Hour,'2018-11-20 14:26:38.000','2018-11-22 11:00:29.367')
Not prefixing the hours to be two digits looks like it could be the issue, making it unambiguously in the afternoon gives your result.
/*------------------------
SELECT DATEDIFF(Hour,'2018-11-20 14:26:38.000','2018-11-22 11:00:29.367')
------------------------*/
45
You're default Date Time format is probably 12 hour, and "2:…" is being treated as pm.
Using two digits for the hour should help.
/*------------------------
SELECT DATEDIFF(Hour,'2018-11-20 02:26:38.000','2018-11-22 11:00:29.367')
------------------------*/
57
(SQL Server has a lot of backward compatibility which may be triggered is the input is not precisely formatted, ISO Date/Time formats always use two digits for hours, minutes, and seconds.)
If you are using a client (rather than fixed code in a Stored Proc/Function/Trigger/…) parameterising your queries avoids this issue: pass data as Date-Time type directly without any need to convert into a string.

Date Difference in MS SQL

DATEDIFF(datepart,FromDate , Todate)
SELECT DATEDIFF(dayofyear,'2008-08-07','2008-08-09') AS DiffDate
Result = 2 days
which date sql sever is exclude while calculating difference FromDate or Today ?
Why it not be 3 days for 7,8 and 9 ?
For simplicity, for yourself, you could remember that DATEDIFF views the range as including the "from" date and excluding the "to" date. So, in your case, only the 7th and the 8th are counted.
Formally, however, the logic is described to be this:
Returns the count (signed integer) of the specified datepart boundaries crossed between the specified startdate and enddate.
How many DAYOFYEAR boundaries are there between 2008-08-07 and 2008-08-09?
2008-08-07 -> 2008-08-08
2008-08-08 -> 2008-08-09
Two, as it happens. Hence the result you get.
Because 9-7 is 2 in most decimal maths systems?
Your are on the 7th. How many days to you have to wait to be on the 9th?
2

SQL date values converted to integers

Ok, I can't understand this thing.
A customer of mine has a legacy Windows application (to produce invoices) which stores date values as integers.
The problem is that what is represented like '01.01.2002' (value type: date) is indeed stored in SQL Server 2000 as 731217 (column type: integer).
Is it an already known methodology to convert date values into integers (for example - I don't know - in order to make date difference calculations easier?)
By the way, I have to migrate those data into a new application, but for as much I googled about it I can't figure out the algorithm used to apply such conversion.
Can anybody bring some light?
It looks like the number of days since Jan 1st 0000 (although that year doesn't really exists).
Anyway, take a date as a reference like Jan 1st 2000 and look what integer you have for that date (something like 730121).
You then take the difference between the integer you have for a particular date and the one for your reference date and you that number of days to your reference date with the DATEADD function.
DATEADD(day, *difference (eg 731217 - 730121)*, *reference date in proper SQLServer format*)
You can adjust if you're off by a day a two.

Resources