PostgreSQL returns wrong values for varchar-timestamp conversion - sql

I have been encountering a very strange error while working on a postgre server. I have a column which contains times as varchar in the format HH24MMSS and I have to convert it to a timestamp using to_timestamp(varchar, 'HH24MMSS'). However, it returns me wrong values and I cannot understand why. Here are a few examples:
Select to_timestamp('111111', 'HH24MMSS')
-> 11:00:11
Select to_timestamp('000000', 'HH24MMSS')
-> 00:00:00
Select to_timestamp('122317', 'HH24MMSS')
-> 12:06:49
Here A Short Overview:
Unfortunately, I cannot offer more detailed information about the server since I am not the admin nor the one maintaining it. I only know that it is Postgre 9.2. I really do not understand, what is wrong here. I would appreciate any hint or help.

Thanks for the comments. The "MM" is part of the problem and explains why the minutes have not been translated correctly. However, using the correct function with 'MI' instead of 'MM' still delivers weird values. For some reasons, postgre automatically adds 6:32 to add time:
enter image description here

Related

Where in my query to place the CONVERT to convert DateTime to Date

Just learning SQL and I've searched many options about converting a DateTime into a Date, and I do not want current date. It's a super simple query from this website: https://sqlzoo.net/wiki/Guest_House_Assessment_Easy
SELECT booking_date, nights
FROM booking
WHERE guest_id=1183
But the output is with the timestamp and I just want the date. I've searched so many forums and tried all their suggestions, including this:
SELECT CONVERT(varchar(10), <col>, 101)
So I've done:
SELECT CONVERT(varchar(10), booking_date,101), nights
FROM booking
WHERE guest_id=1183
But I'm getting syntax errors. This is probably so simple and you'll all think me an idiot, but I'd greatly appreciate help. It's driving me nuts.
When I fiddled about at your sqlzoo link I got the error
execute command denied to user 'scott'#'localhost' for routine 'gisq.to_date'`.
When I googled gisq.to_date I got this link https://sqlzoo.net/wiki/Format_a_date_and_time
Which has examples of how this dialect represents dates. See if you can work it out. Something like this:
SELECT date_format(booking_date,'%d/%m/%Y')
FROM booking
You didn't post the error in your question which is a big mistake. When you get an error message, you actually have something to work from.
It is also very important to note that the query above returns a string, not a date. It's only good for display, not for date arithmetic
TBH that seems like a terrible site to learn on as it gives no clues about the dialect. it looks like Oracle but to_date and trunc don't work.
The use of convert() suggests that you think you are uinsg SQL Server. If you only want the date component of a date/time data type, then you can use:
SELECT CONVERT(DATE, booking_date), nights
FROM booking
WHERE guest_id = 1183;
The syntax error suggests that you are not using SQL Server.
CONVERT() is bespoke syntax for SQL Server. Examples of similar functionality in other databases are:
DATE(booking_date)
TRUNC(booking_date)
DATE_TRUNC('day', booking_date)
In addition, what you see also depends on the user-interface.
In your case, the data is being stored as a date with no time component, but the UI is showing the time. For that, you want to convert to a string. That site uses MariaDB -- which is really a flavor of MySQL-- and you would use:
DATE_FORMAT(booking_date, '%Y-%m-%d')

Invalid procedure call when running SQL statement (Max function with parameter)

So this is my third question in so many hours. Thanks again to everyone who has taken the time to help me through my SQL ordeal. I think this might be my last tango for the night, so here goes:
After taking some very good advice from #Vojtěch Dohnal, I converted one of my queries from a concatenated string to a parameterized SQL query here:
PARAMETERS NewPrefix TEXT; SELECT MAX([Suffix]) FROM [SalesTable] WHERE [Prefix] = [NewPrefix];
From what I can tell, this should be the right syntax for creating a parameterized query; the user will define what should go into the NewPrefix field and it will find the appropriate max function based on that. However, whenever I go to execute this query it hits me with the same 'Run-time error '5'; Invalid procedure call' error I've been wrestling with for about 9 hours now haha
I went ahead and tried to test the same query in the Access SQL query window, and I receive an error message there claiming: "This expression is typed incorrectly, or it is too complex to be evaluated. For example, a numeric expression may contain too many complicated elements. Try simplifying the expression by assigning parts of the expression to variables".
I'm not sure how to get around this. I don't think the syntax is wrong, but I can't find anything to compare it to on the Internet. I've used the debugger to step through and it looks like all of the values and variables and fields are populated correctly, but when it gets to the execute command it crashes with the same singularly unhelpful error message.
Thanks again for anyone who can help.
So it looks like the main problem that I was running into was that the "Prefix" field was actually a calculated field in my underlying Access table. For whatever reason, Access doesn't want to work with calculated fields with SQL; when I took out the calculation and just made the Prefix column a regular field, everything seems to work perfectly. I'm not really happy with this but it seems to work and that's what matters. Thanks to everyone who took the time to try and help me with this stuff. Cheers!

oracle sql developer(4.0.0.12) returns wrong date

It seems that a query of mine returns wrong results, and I'm not sure why. I don't yet rule out the possibility that the SQL is actually doing something else then what I expect/want it to do since I haven't used SQL for a time now.
I post it here because I'm kind a stuck with the why it returns wrong results sometimes.
The error is in the MIN(FIRM.account_recharge.X__INSDATE) (or at least the ones I noticed)
SELECT
FIRM.customer.CUSTOMER_ID,
FIRM.customer.CORPORATION,
FIRM.customer.CUSTOMER_NAME_PREFIX,
FIRM.customer.CUSTOMER_NAME,
FIRM.account.ACCOUNT_TYPE,
FIRM.account.ACCOUNT_TYPE,
FIRM.customer.LANGUAGE,
FIRM.customer.VALIDATED,
FIRM.account.X__INSDATE,
SUM(FIRM.account_recharge.GROSS_VALUE) SUM_FELTOLTESEK,
MIN(FIRM.account_recharge.X__INSDATE),
INNER JOIN FIRM.account
ON FIRM.customer.CUSTOMER_ID = FIRM.account.CUSTOMER
INNER JOIN FIRM.customer_address
ON FIRM.account.CUSTOMER = FIRM.customer_address.CUSTOMER
INNER JOIN FIRM.account_recharge
ON FIRM.account.ACCOUNT_ID = FIRM.account_recharge.ACCOUNT
GROUP BY FIRM.customer.CUSTOMER_ID,
FIRM.account.X_INSDATE,
FIRM.customer.CORPORATION,
etc,etc
HAVING MIN(FIRM.account_recharge.X__INSDATE) BETWEEN to_date('2014-JAN. -01','YYYY-MON-DD') AND to_date('2014-DEC. -31', 'YYYY-MON-DD');
This code should return information abut our customers, their sum account 'recharging'/'replenishing'/'paying in' , sorry not sure of what word to use here. and their first payment/money upload to their account in 2014. Yet sometimes the return values seems to just ignore the actual first time our client paid in money, and shows the second or third date. (my random manual check returned that around 1/10 of the time the returned values are wrong.)
A costumer of ours can have more the one account linked to him. I'm using Oracle SQL developer (4.0.0.12) please ask if you would like to know anything else about this pickle im in.
Otherwise It seems to work nicely, but if you have any other tuning tip, I would be glad to hear them.
Thank you for your help!
HAVING MIN(FIRM.account_recharge.X_INSDATE) BETWEEN '14-JAN. -01' AND '14-DEC. -31'
This is definitely incorrect. You are comparing dates. so, you must convert the string literal explicitly into a date using TO_DATE and proper format mask.
For example,
HAVING MIN(FIRM.account_recharge.X_INSDATE)
BETWEEN to_date('2014-JAN-01','YYYY-MON-DD')
AND to_date('2014-DEC-31', 'YYYY-MON-DD')
Also, do not use YY to denote the year. You don't have to re-invent the Y2K bug again. Always use YYYY format for an year. Else, if you are stuck with YY values for year, then use RR format. But, I would insist, always use YYYY format for year.

DATEDIFF command won't work as 'day' is not a recognised column

I'm relatively new to SQL and have been attempting to run a script wherein I can bring up the number of days that have passed between two points in time. I understand how this should look based on your website, but for some reason when I input the values, my database is returning the following error:
ProgrammingError: ERROR: column "day" does not exist
The code I'm using is:
select datediff(day, '2014-01-01', '2014-02-01')
I assume I'm missing something very simple (this is a hugely basic query I'm sure), but would be appreciative of any assistance. I've variously tried pointing it towards the specific table I want to draw from, but it keeps stumbling on this error.
If you are doing this in postgresql then use
select DATE_PART('day', '2014-01-01'::timestamp - '2014-02-01'::timestamp)

YEARFRAC function on SQL Server not working

I was happy to find you can use an Excel-like YEARFRAC function in MS SQL server (http://technet.microsoft.com/en-us/library/ee634405.aspx), but for some reason I get an error that states:
'yearfrac' is not a recognized built-in function name
when I try to run my query. Here is my code:
SELECT CUSTOMER_ID, PRODUCT_SKU, SUB_START_DATE, SUB_EXP_DATE,
YEARFRAC(sub_start_date, sub_exp_date) AS START_TO_END FROM ...
For the record, I have double-checked the dates are in proper datetime format, and I tried both using no basis (as shown above), and using the available 1-4 bases. I also tried removing the column alias (START_TO_END). None of these worked. Any ideas?
No, that is in Analysis Services (DAX specifically), not in T-SQL. The header on the page does not make it clear which section of the documentation you're in, but look at the table of contents on the left...
Sorry the screen shot is double size, it's because of my Retina screen giving 144dpi instead of 72dpi.
Anyway, you should be able to replicate this functionality with your own UDF or, if you always calculate a specific way, it might even be simple enough to do inline. A calendar table may help.