How to get time from Date & time field in SQL? [duplicate] - sql

In my table I have a datetime field that stores dates in this format:
YYYY/mm/dd HH:MM:SS
Now I need to retrieve via query only the time.
I have tried this
SELECT time(start_date)
FROM table
LIMIT 100
but no luck, it gives me "not an error" but no records return
Any idea?
EDIT
I solved it! The problem is that SQLite needs Times to be in a format in which if hours, minutes and seconds are less than 10 they must be represented with the zero.
For example:
H:M:S
WRONG --> 12:1:30
RIGHT --> 12:01:30
Moreover, the correct format for dates is YYYY-mm-dd and not YYYY/mm/dd.

There is a built-in function in SQLite called strftime(format,datetime) which can be used to get whatever piece of information you require from the given datetime. In your case you can use in this way:
SELECT strftime('%H:%M:%S',start_date) FROM table LIMIT 100;

Related

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 :

Is there any way to get data between specific time intervals?

For example, I want to query data for events in the last two days from a table. Currently, using between date_sub(CURRENT_DATE,2) and date_sub(CUREENT_DATE,1). What I want to do actually is get data from 7 AM two days before to 7 AM today. How can I do that in hive?
The function that you use has only date. If you can concatenate time in hh:mm:ss format. You will be able to achieve what you are looking for. If it doesn't work you can try converting to datetime.
between concat( date_sub(CURRENT_DATE,2),' 07:00:000') and concat( date_sub(CURRENT_DATE,2),' 07:00:000')

sqlite converting milliseconds to HH:MM:SS.SSS

I have a column in the table that stores milliseconds as an Integer type in sqlite. The millisecond timestamp is stores the time it took for certain event happen. For example, it took 36 milliseconds for Action 1 occurred, the value 36 will be stored into the table.
I want to construct a SQL select statement that will format the millisecond timestamp to HH:MM:SS.SSS format, and so far I couldn't get it working.
Here's what the SQL query that I constructed.
select strftime('%H:%M:%f', table_foo.time/1000, 'unixepoch') as time from table_foo
The query result returned 00:00:00.000 where I was expecting to see 00:00:00.036 when the timestamp is 36 ms.
It appears that it is not showing the reminder properly.
Can someone points me what I did wrong? or what is the appropriate way to do what I need?
Thanks in advance.
try the following, replace int 1000 with decimal 1000.0.
select
strftime('%H:%M:%f', table_foo.time/1000.0, 'unixepoch') as time
from table_foo

Fetching the records which are having all time stamp columns

I am trying to fetch the records which are having all time stamp columns.
I am using the following query to fetch the products that are created between the final date and (final date - 30) days, i.e products created during the last 30 days that fall in the 'final date' range.
I have products that are created on 30-OCT-2014. For the same products, the initiated date is 12-NOV-2014. However they are not being fetched when I using the below query.
SELECT A.ROW_ID,
A.PROD_NAME
FROM PROD A,
PROD_REL B
WHERE A.ROW_ID = B.PAR_ROW_ID
AND TO_DATE(A.CREATED_DT,'YYYY-MM-DD') BETWEEN (TO_DATE(B.FINAL_DATE,'YYYY-MM-DD') - 30)
AND (TO_DATE(B.FINAL_DATE,'YYYY-MM-DD'));
So, could you please let me know if I am missing something?
Here is a link to a SQLFiddle that demonstrates the problem.
… or just fix your format string TO_DATE(A.CREATED,'DD-MON-YYYY')
SQL Fiddle
Storing dates as DATE, is , of course, always a good starting point.
Since your data types are all dates, there is no need to use to_date. It's harmful, in fact, since to_date doesn't take a date as a parameter. Oracle has to do an implicit conversion from a date to a string, using your session's NLS_DATE_FORMAT which gets passed in to to_date and converted back to a date using the explicit format mask you specified. If the two conversions aren't using the same format mask, bad things happen.
Your WHERE clause just needs to be
AND a.created_dt BETWEEN b.final_date - 30
AND b.final_date
If I make that change, your SQLFiddle returns two rows

MS Access - Select Char as Date and doing a date diff

I have two columns. ColA and ColB contains char(10) with data "20090520" and "20090521".
I want to select and get the date difference in days. I have tried using Format() and CDate()
but MS Access always display as #ERROR.
Access prefers its dates in this format:
#2009-12-01#
You can convert your date to something Access understands with:
CDate(Format([ColA], "0000-00-00"))
Or alternatively:
DateSerial(Left([ColA],4),Mid([ColA],5,2),Right([ColA],2))
And to display the result in your preferred format:
Format(<date here>, "dd-mm-yyyy")
Try using DateSerial() to convert the dates:
DateSerial(Left([FieldName],4),Mid([FieldName],5,2),Right([FieldName],2))
If at all possible, change the datatype to a date datatype. You should not store dates as character data.
I am connecting to another database which I have no control on. That is why this problem occurred. Thanks for the feedback.