How do you convert varchar string in ISO 8601 format 'ID:HH24:MI' into time? - sql

I have a column in postgresql of type varchar that stores time in the format "ID:HH24:MI" and I would like to add/subtract time to it. For example, a value of "4:21:35" would translate to Thursday 9:35PM. If I wanted to add thirty minutes to it, I expect the string "4:22:05".
I tried the query below but it does not work because it returns the wrong day. I'm expecting 4:22:05 and it returns 6:22:05.
SELECT to_char(to_timestamp('4:21:35', 'ID:HH24:MI' ) + INTERVAL '30 minute', 'ID:HH24:MI');
6:22:05
How could I modify the query or change my approach to achieve the right output? I want to compare time strings in the format "ID:HH24:MI" and would like to be able to add/subtract time to those time strings. Any help would be greatly appreciated.

Related

Upper case day of week in Teradata SQL

I can get the day of the week from a data but I can't seem to convert to uppercase
This is the statement I'm using:
CAST(CURRENT_DATE AS DATE FORMAT 'e4') (CHAR(9))
How can I convert this to uppercase?
I've tried wrapping UPPER around each of the elements of the statement but I get errors each time
UPPER(CAST(CURRENT_DATE AS DATE FORMAT 'e4') (CHAR(9)))
UPPER(CAST(CURRENT_DATE AS DATE FORMAT 'e4')) (CHAR(9))
Any help would be greatly appreciated
While it would be possible to fix your current code
Upper(Cast(Cast(Current_Date AS FORMAT 'E4') AS CHAR(9)))
it's better to switch to
To_Char(Current_Date,'DAY')

Issues while converting timestamp to specific timezone and then converting it to date in bigquery

I am doing just a simple conversion of timestamp column value to specific timezone and then getting the date out of it to create analytical charts based on the output of the query.
I am having the column of type timestamp in the bigquery and value for that column is in UTC. Now I need to convert that to PST (which is -8:00 GMT) and was looking straight forward to convert but I am seeing some dates up and down based on the output I get.
From the output that I was getting I took one abnormal output and wrote a query out of it as below:
select "2021-05-27 18:10:10" as timestampvalue ,
Date(Timestamp("2021-05-27 18:10:10" ,"-8:00")) as completed_date1,
Date(Timestamp("2021-05-27 18:10:10","America/Los_Angeles")) as completed_date2,
Date(TIMESTAMP_SUB("2021-05-27 18:10:10", INTERVAL 8 hour)) as completed_date3,
Date(Timestamp("2021-05-27 18:10:10","America/Tijuana")) as completed_date4
The output that I get is as below:
Based on my understanding I need to subtract 8 hours from the time in order to get the timestamp value for the timezone that I wanted and according to that completed_date3 column seems to show the correct value that should be there but if I use other timezone conversions as suggested in google documentation, the output gets changed to 2021-05-28 and I am not able to understand how that can happen.
Can anyone let me know what is the thing that I am doing wrong?
I was actually using it in a wrong way. I need to use it as below :
select "2021-05-27 18:10:10" as timestampvalue ,
Date(Timestamp("2021-05-27 18:10:10") ,"-8:00") as completed_date1,
Date(Timestamp("2021-05-27 18:10:10"),"America/Los_Angeles") as completed_date2,
Date(TIMESTAMP_SUB("2021-05-27 18:10:10", INTERVAL 8 hour)) as completed_date3,
Date(Timestamp("2021-05-27 18:10:10"),"America/Tijuana") as completed_date4
Initially I was converting that string timestamp to a specific timestamp based on the timezone and that is what I did not want.
Now if a convert a string to timestamp first without using time zone parameter and then apply timezone parameter when getting the date value out of it then it would return me correct date.
Please see the snapshot below :

Format date to MM/DD/YYYY, remove seconds & miliseconds, and convert from military to standard time SQL

I have a field Run_time. I would like to Format date to MM/DD/YYYY, remove seconds & miliseconds, and convert from military to standard time SQL.
For example:
Select Run_time from Table
The current output is such:
2018-06-04 15:18:56.333
I would like the output to look like the following:
06/04/2018 3:18pm
Any Help would be greatly appreciated!
thank you!
~Adam
Adam,
The correct place to format dates would be in the presentation layer, namely the front end application. However, since the question wasn't where to do it, rather how to do it in t-sql, we'll answer that question as-is.
There are many formats in which you can convert a date in SQL Server (reference below), though for this exact result, you'll need to concatenate 2 of them, as the one which provides dates in a mm/dd/yyyy format doesn't have the hh:mm AM/PM format to go with it. Here's the t-sql:
SELECT
CONVERT(CHAR(10), Run_time ,101) + ' '+ LTRIM(RIGHT(CONVERT(CHAR(19), Run_time ,0),7))
FROM Table
Here's a link to the page which shows you the different conversion formats in t-sql:
https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-2017

Subtract Two Date Time Values in SQL Server

I want to subtract two DateTime values:
I have Start DateTime #2015-06-14 23:00:00# End Date Time #2015-06-15 01:01:00#
I want to get the duration by time format like HH:MM:SS and update it in the duration column. I tried the following code but, it doesn't work.
update [ZainJTA].[dbo].[TBL_Justification]
set [Justification_Event_Duration]=CONVERT(Datetime,(DateDiff("n",[Justification_From],[Justification_TO])/60/24),108)
DATEDIFF returns an INT, so dividing it by two other INTs is going to give you - another INT, most likely zero.
Try:
CONVERT(Datetime,(DateDiff(s,[Justification_From],[Justification_TO])/60.0/60/24),108)
(the 60.0 will trigger conversion to a floating point format.)
However, it'd probably make more sense to store it as a number of seconds (integer), and only do the CONVERT when you output it to display.
To display as just the time, with no day/year part, you'll also need to do a second conversion:
CONVERT(VARCHAR, CONVERT(Datetime,
(DateDiff(s,[Justification_From],[Justification_TO])/60.0/60/24),108),108)

Date not displaying correctly in Oracle

I have a character field that stamps in the order of MMDDYYHHMMSS (note: not a date but character field). I am wanting to kick this out to a date field in my SQL into this format dd.mm.yyyy. hh24:mi.
My problem is that the sql kicks it out to YYYY-MM-DD field without the time. This section of the sql looks like this:
TO_DATE(SUBSTR(MOPACTIVITY.MOPID,3,2)||'.'||SUBSTR(MOPACTIVITY.MOPID,1,2)
||'.'||'20'||SUBSTR(MOPACTIVITY.MOPID,5,2)||'.'||SUBSTR(MOPACTIVITY.MOPID,7,2)
||':'||SUBSTR(MOPACTIVITY.MOPID,9,2)||':'||SUBSTR(MOPACTIVITY.MOPID,11,2)
, 'dd.mm.yyyy. hh24:mi:ss') "XXX",
Any thoughs on how to get the time to convert too?
No need for such a complicated expression:
to_date(MOPID, 'MMDDYYHH24MISS')
will convert the column to a real DATE column assuming the time part is in 24 hour format (00-23, not 00-12). And this will also fail if you don't really have valid dates in the varchar column.
this out to a date field in my SQL into this format
A DATE column does not have "a format"!
The format is only applied when you display it.
In case you mean you want to convert the varchar stored in your column into another varchar that has a different date formatting, the easiest is probably to simply convert the above expression back to a varchar:
to_char(to_date(MOPID, 'MMDDYYHH24MISS'), 'dd.mm.yyyy. hh24:mi')
Before applying something like that, allow me one comment:
Store dates in DATE columns, never ever store them in a VARCHAR column.
If you had done that from the beginning, all you would have to do know is to simply apply a single to_char() to your DATE column to get the display format you want.