SQL datetime into date and time - sql

Not sure the way to break the datetime field become 2 columns weekday and time
ContactDate
2019-07-09 09:15:12.000
My query:
SELECT FORMAT(contactdate, 'ddd') AS Result FROM contact
And the outcome only week date but no time. How do we get the time? Thank you.
Result
Tue

Just use DATENAME and CONVERT:
SELECT CONVERT(varchar(3),DATENAME(WEEKDAY,ContactDate)),
CONVERT(time(3),ContactDate)
FROM dbo.Contact;
I recommend against FORMAT. Although it's a useful function, it is significantly slower than other functions; especially when dealing with large sets of data.

SELECT cast(contactdate as time) [time]

Related

format datetime in big query select FORMAT_DATETIME('%Y-%m-%d %H:%M:%E*S','0001-01-01')

I need to make the date in the below particular format in big query, but somehow it is truncating leading zero's automatically. How to get the output as is.
Query used:
select FORMAT_DATETIME('%Y-%m-%d %H:%M:%E*S','0001-01-01')
output: 1-01-01 00:00:00
Desired output: 0001-01-01 00:00:00
Please help.
Use $E4Y for the year:
select FORMAT_DATETIME('%E4Y-%m-%d %H:%M:%E*S', '0001-01-01')
'%Y' uses only as many characters as needed for the year. '%E4Y' always uses 4 characters.

Cannot pull correct time range

After executing this query
AND TRUNC(ITD.TRAN_DATE)>= '02-APR-18'
AND TO_CHAR(ITD.TRAN_DATE,'HH24MI')>='0600'
AND TRUNC(ITD.TRAN_DATE)<= '03-APR-18'
AND TO_CHAR(TRUNC(ITD.TRAN_DATE+1),'HH24MI')<='0030'
Everything after 6AM for April 2nd will show, but also times after 12:30AM the next day shows. What am I doing wrong here?
When we truncate a date we remove the time element. Consequently, a mask of 'HH24:MI' will return '00:00' which means that this will always be true regardless of the actual time component of ITD.TRAN_DATE:
AND TO_CHAR(TRUNC(ITD.TRAN_DATE+1),'HH24MI')<='0030'
Probably what you should do is something more straightforward, such as
where itd.tran_date >= date '2018-04-02' + (6/24)
and itd.tran_date <= date '2018-04-03' + (1/48)
Here's a SQL Fiddle demo comparing your WHERE clause with my suggestion.
Not sure what you want. Maybe this:
and to_char(itd.tran_date, 'YYYYMMDD-HH24MISS')
between '20180402-0600'
and '20180403-0030'
If so however, and if the table have many rows and tran_date is an indexed column, this might be a lot faster:
and itd.tran_date
between to_date('20180402-0600','YYYYMMDD-HH24MI')
and to_date('20180403-0030','YYYYMMDD-HH24MI')
In your question you assume the date_format to be DD-MON-YY. This might be the case now, for the environment you have today, but might not be so always. So it's good practice to use to_char or to_date on DATE values or strings.

check for dates syntax - teradata SQL

I am trying to check for dates but after running the query below, it displays no result. Could someone recommend me the correct syntax?
SELECT TOP 10 * FROM MY_DATABASE.AGREEMENT
WHERE end_dt=12/31/9999
12/31/9999 might look like a date for you but for the database it's a calculation:
12 divided by 31 divided by 9999 and because this involves INTEGER division this results in an INTEGER 0
So finally you compare a DATE to an INT and this results in typecasting the DATE to a INT.
The only reliable way to write a date literal in Teradata is DATE followed by a string with a YYYY-MM-DD format:
DATE '9999-12-31'
Similar for TIME '12:34:56.1' and TIMESTAMP '2014-08-20 12:34:56.1'
Is it a date column? Then try where end_dt = '9999-12-31'.
The question you ask is not very clear. The date you specify is language dependent.
Try
SELECT TOP 10 * FROM MY_DATABASE.AGREEMENT WHERE end_dt='99991231'

finding records between two dates and without entries elsewhere

I am having a problem setting up my date boundaries for the query.
I want records between 10/1/2010 and 12/31/2010, but without a record (activity) in calendar year 2011 to date.
where INV.Date_Imported BETWEEN '10/1/2010' AND '12/31/2010'
AND INV.RecID NOT IN (
SELECT RecID
FROM [VW_Invoice_All]
WHERE Date_Imported > '1/1/2011'
)
The only glaring issues I see is your Date_Imported line. If you want 1/1/2011 to be included in the NOT IN query, you need to change the query to
WHERE Date_Imported >= '1/1/2011'
BETWEEN is already inclusive, which is what you appear to be going for.
You don't have any times on your dates, but if they are DATETIME columns then that could be important. I would probably use:
WHERE
INV.Date_Imported >= '10/1/2010' AND
INV.Date_Imported < '1/1/2011' AND
NOT EXISTS (
SELECT *
FROM [VW_Invoice_All] I2
WHERE
I2.RecID = INV.RecID AND
I2.Date_Imported >= '1/1/2011')
(The EXISTS might give you better performance than the IN query, but test both.)
As gtcompscientist says:
BETWEEN is already inclusive...
so you only need:
WHERE INV.Date_Imported BETWEEN '2010-10-01 00:00:00' AND '2010-12-31 23:59:59'
To avoid any doubts, using the YYYY-MM-DD HH:mm:ss format means you don't need to worry about regional settings (UK dates are DD-MM-YYYY whilst US is MM-DD-YYYY but YYYY-MM-DD format is interpreted the same in both regions).
The addition of time (HH:mm:ss) ensures that you include all of 2010-12-31 i.e. from 00:00:00 to 23:59:59.
From my experience, the safest date format is 'yyyymmdd'. In the bank where I work at the moment it's the only format that works on both the production server and the test server.

MySql difference between two timestamps in Seconds?

Is it possible to calculate difference between two timestamps in Mysql and get output result in seconds? like 2010-11-29 13:16:55 - 2010-11-29 13:13:55 should give 180 seconds.
Thank you
I do not think the accepted answer is a good universal solution!
This is because the UNIX_TIMESTAMP() function fails for DATEs before 1970-01-01 (and for dates in the far future using 32 bit integers). This may happen easily for the day of birth of many living people.
A better solution is:
SELECT TIMESTAMPDIFF(SECOND, '2010-11-29 13:13:55', '2010-11-29 13:16:55')
Which can be modified to return DAY YEAR MONTH HOUR and MINUTE too!
Use the UNIX_TIMESTAMP function to convert the DATETIME into the value in seconds, starting from Jan 1st, 1970:
SELECT UNIX_TIMESTAMP('2010-11-29 13:16:55') - UNIX_TIMESTAMP('2010-11-29 13:13:55') as output
Result:
output
-------
180
An easy way to deal with if you're not sure which value is bigger than the other -- use the ABS function:
SELECT ABS(UNIX_TIMESTAMP(t.datetime_col1) - UNIX_TIMESTAMP(t.datetime_col2)) as output
TIMESTAMPDIFF method only works with datetime format. If you want the difference between just two times like '11:10:00' minus '10:20:00' then use
select TIME_TO_SEC('11:10:00')-TIME_TO_SEC('10:20:00')