Cast Datetime offset to Date - sql

I have the date column in datetimeoffset format.
2016-01-09 05:49:06.3744350 +00:00
I want to convert it to only date format, e.g. 2016-01-09.
I was able to convert datetimeoffset to datetime2 with this query.
convert(datetime2, sampleDate, 1) as date
Would be much obliged if I could know how to convert this to the desired format in MS SQL.

Simply:
SELECT CONVERT(DATE, CONVERT(DATETIMEOFFSET, '2016-01-09 05:49:06.3744350 +00:00'))
Returns:
2016-01-09

You can use CAST function
SELECT CAST('2016-01-09 05:49:06.3744350 +00:00' as date)

Related

Convert DATETIME to varchar in SQL

I want to convert DATETIME to VARCHAR with Friday 07-Feb-20 8:30 AM this type of output. I tried to do it like this. But it didn't give the correct format that I want.
DECLARE #STARTDATE DATETIME,#SDATE VARCHAR(250)
SELECT #STARTDATE = SESSION_START FROM SESSION_INFO WHERE SESSION_ID = 2071 //#STARTDATE = 2013-01-28 14:00:00.000
SET #SDATE = CONVERT(VARCHAR,#STARTDATE,100)
PRINT #GOOGLE //Jan 28 2013 2:00PM
I want day-mon-year hh:min AM/PM (Friday 07-Feb-20 8:30 AM) this format. Thank you.
You want format for that e.g.
select format(current_timestamp, 'dddd dd-MMM-yy hh:mm tt');
Note: format doesn't perform as well as convert or cast but it has the added flexibility you need.
You can use the format as follows:
FORMAT (SESSION_START, 'dddd dd-MMM-yy hh:mm tt')
DB<>Fiddle with the example output

How to convert a datetime into date without changing the data type in SQL Server?

How to split this TimeStamp column without changing data format?
I have a column TimeStamp which is in datetime format, and I just want to keep the date part (in datetime format).
I tried to use
CONVERT(DATE, "TimeStamp")
it shows only the date part, but the format is nvarchar(10).
SQL Code:
SELECT
"TimeStamp",
CONVERT(DATE, "TimeStamp") AS Date
My expected result:
TimeStamp (datetime) Date (datatype: datetime)
------------------------------------------------------
2017-03-10 07:30:25 2017-03-10
2017-03-10 07:30:28 2017-03-10
2017-03-10 07:31:30 2017-03-10
2017-03-10 07:31:39 2017-03-10
Tysss I have made a demo for you please try this, Here I have used GETDATE() for return current date with a timestamp where you have to pass your column name as you showed in the question which is TimeStamp.
SOLUTION 1
SELECT CAST(CONVERT(VARCHAR,GETDATE(),110) AS DATE) AS DATE
OUTPUT
2019-05-17
SOLUTION 2
SELECT CAST(CONVERT(VARCHAR,GETDATE(),110) AS DATETIME) AS DATE
OUTPUT
2019-05-17 00:00:00.000
I think you need the only date from TimeStamp so, you need to change the datatype DATE else if you will keep your datatype DATETIME then it will return date something like 2019-05-17 00:00:00.000
select GETDATE() as TimeStamp,
convert(date, getdate()) as date
SELECT CONVERT(date, getdate());

How to convert timestamp to date in Presto?

I like to convert my timestamp columns to date and time format. How should I write the query from presto? my timestamp is UTC time. Thank you very much
Timestamp format"1506929478589"
After query convert it looks like "2016-10-25 21:04:08.436"
You can convert timestamp to date with cast(col as date) or date(col).
You can use the date_format function (docs here: https://prestodb.io/docs/current/functions/datetime.html)
Here's an example:
date_format(charges.created, '%Y-%m') as rev_month
If for some reason you're comparing dates you don't need to do the conversion like that, you can do the following
where customers.created BETWEEN timestamp '2018-04-01 00:00:00.000' AND timestamp '2018-05-01 00:00:00.000'

How to cast varchar to datetime in sql

I need to cast a string to a datetime so i can compare it later.
The varchar that i have is like this format:
29/11/2013 12:00:00 a.m.
And i need it to cast to:
2013-11-29 00:00:00.000
Im using MSSQL Server 2012
Thx for all
Please, have a look a CAST and CONVERT for more information.
Here are some examples:
-- converting from DD-MM-YYYY
select CONVERT(datetime, '29/11/2013 12:00:00 AM', 103)
-- converting from MM-DD-YYYY
select CONVERT(datetime, '11/29/2013 12:00:00 AM', 101)
IF Your date is of type varchar in the database then if you need to retrieve
then these cases arises::
case1:if your data is in the format of "dd-MM-yyyy"
then you need to use query as follows
Query::
select * from [yourTableName] where convert(datetime,YourDateColumn,103) between '2016-02-12'(yyyy-MM-dd) AND '2016-03-12'
case2:if your data is in the format of "dd-MM-yyyy HH:mm:ss"
then you need to use query as follows
Query::
select * from [yourTableName] where convert(datetime,YourDateColumn,105) between '2016-02-12'(yyyy-MM-dd) AND '2016-03-12'

How to assign current date with specific time to column?

How do i assign current date with a specific time?
let's say 8:00:00 AM to Column EXIT_DT of datatype datetime??
I have tried GETDATE() AS EXIT_DT but it gives me current datetime. I am using Sql server 2005. Any help?
Lets say Today is 1/3/2013 and i want my result to return as a datetime datatype with value 1/3/2013 8:00:00 AM. If i run the statement ytd, the result will be 1/2/2013 8:00:00 AM
This formula will always produce 08:00 for the day it is called, and avoids string manipulation:
select DATEADD(day,DATEDIFF(day,'20010101',GETDATE()),'2001-01-01T08:00:00')
Try to avoid solutions that convert to and from strings - treating datetime values as strings is one of the largest sources of bugs.
It works by computing the number of days (as an integer) that have elapsed since 1st January 2001. It then adds that same number of days to 08:00 on 1st January 2001.
You can try this :
DECLARE #dt datetime;
SET #dt=CONVERT(DateTime, CONVERT(VARCHAR,GETDATE(),101)+' 8:00:00')
SELECT CONVERT(VARCHAR, #dt, 101)+' '+ LTRIM(RIGHT(CONVERT(VARCHAR(20),#dt, 100), 7))
Visit http://www.sql-server-helper.com/tips/date-formats.aspx for datetime formats.
Use Convert along with getdate() to get specific formats.
ex:
SELECT CONVERT(VARCHAR(30),GETDATE(),113)
This is a bit stupid, but it works
select cast(cast(getdate() as date) as datetime) + '08:00:00'
it casts the getdate() to date thus losing the hours, than it casts it to datetime and adds 8 hours.
If you want to avoid implicit conversion of varchar to datetime, you could use this version:
select cast(cast(getdate() as date) as datetime)
+ convert(datetime,'08:00:00',114)
This is also working. (1). convert today's date to ISO format (yyyymmdd) (2). add the time, (3). convert back to datetime
Select convert(datetime, convert(varchar, getdate(),112) + ' ' + '8:00:00AM')
--Results
2013-01-03 08:00:00.000
If you need in specific format you need to convert back to varchar again.
-- AM/PM --
SELECT TO_CHAR(sysdate, 'MM/DD/YYYY HH:MI:SS AM') FROM dual
/
-- 24 hrs format --
SELECT TO_CHAR(sysdate, 'MM/DD/YYYY HH24:MI:SS') FROM dual
/