Converting date time value to 10 character date on sql - sql

I'm trying to use the below code to convert the 'date_time' values in my table to 10 characters. Currently, the date_time values are in the format '2020:09:08 10:00:00' but I want to get rid of the time portion of the values so that I'm only left with the date. I'm using DB Browser for SQLite and I thought I would have to use the varchar data type to complete this task but its not working. The output of the code is also shown below.
SELECT CAST(date_time AS VARCHAR(10)) FROM stocks;

I would just use string functions:
substr(date_time, 1, 10)

Your datetime values are not valid datetimes for SQLite which recognizes only the format 'YYYY-MM-DD hh:mm:ss'.
If you used the above format then all you need would be the function DATE():
SELECT DATE(date_time)
FROM stocks;
For your current format you must use string functions to extract the first 10 chars and then replace all : with -:
SELECT REPLACE(SUBSTR(date_time, 1, 10), ':', '-')
FROM stocks;

Related

Convert a Custom (String) Date Format to datetime in SQL

I need to get all rows from a table that have a date of the last 7 days or greater. My issue is that when the DB was originally setup, someone set it up as VARCHAR. So now I need to CONVERT the String to a DateTime.
The issue is, the format of the Date/Time isn't recognized by SQL. The format is:
2023-01-01T00:00:00.000+0000
If I can trim off the last 8 characters from the string, SQL will recognize it. But I've had no luck thus far. The statement I was attempting was:
SELECT CONVERT(datetime, TRIM('.000+0000' FROM date_col), 127) FROM table_name;
But that resulted in the error:
Conversion failed when converting date and/or time from character string.
Try this
SELECT CAST(LEFT(REPLACE('2023-01-01T00:00:00.000+0000', 'T', ' '), 19) AS DATETIME)
No need for the replace with datetime2(n)
Select WithMS = try_convert(datetime2(3),left('2023-01-01T00:00:00.100+0000',23))
,SansMS = try_convert(datetime2(0),left('2023-01-01T00:00:00.100+0000',23))
Results
WithMS SansMS
2023-01-01 00:00:00.100 2023-01-01 00:00:00

How to convert dd-mmm-yy hh.mm nvarchar to any date format

I have a very specific format for date and time stored in my table as nvarchar.
The format is 'DD-MMM-YY hh.mm' ex. '30-NOV-20 19.25' , '22-JAN-52 13.34' etc.
Is there any way to convert the above nvarchar to standard datetime formats within particular table?
Select Convert(DateTime, Replace(YourDateColumn, '.', ':'))
From YourTable
Your format would be fine if you had a colon between the hours and minutes. Use the replace function to accomodate that.
When you select a datetime value in SQL Server Management Studio, you should understand that it is determining how to display the value. If you want it to display in another way, you need to convert it back to a string while applying a format. For example:
Select PERSON_BDAY,
Convert(VarChar(10), Convert(DateTime, Left(PERSON_BDAY, 9)), 1)
From YourTable

Convert string of YYYYMMDD to YYYY-MM-DD Date format in Snowflake

Based on the example mentioned here in the Snowflake documentation, why are the date and timestamp values returning different values just by changing the ORDER BY clause? Also, I am trying to convert a string to a date format which is not returning correct results in Snowflake while this works fine in other SQL based Engines. Need help from experts on this.
This query
SELECT '20200710', TO_DATE('20200710');
is returning the following output
20200710 | 1970-08-22
Also tried:
SELECT TO_DATE('20200710', 'YYYY-MM-DD');
and got the error:
Can't parse '20200710' as date with format 'YYYY-MM-DD'
To convert to a date data type, you would use:
SELECT TO_DATE('20200710', 'YYYYMMDD')
I would recommend just keeping the date data type. But if you want a string in the format YYYY-MM-DD:
SELECT TO_CHAR(TO_DATE('20200710', 'YYYYMMDD'), 'YYYY-MM-DD')

Presto/SQL - Converting string timestamp to date throws error

NOTE: I am running my query in Qubole's presto and sql command engine.
I am trying to convert my string timestamp to just date but none of the options are working out.
My string timestamp looks like 2017-03-29 10:32:28.0
and I want to have it like 2017-03-29
I have tried following queries to convert this string timestamp to retrieve date
1. select cast(created as date) from table1
Value cannot be cast to date: 2017-05-26 17:23:58.0
2. select cast(from_iso8601_timestamp(created) as date) from table1
Invalid format: "2014-12-19 06:06:36.0" is malformed at " 06:06:36.0"
3. select date(created) from table1
Value cannot be cast to date: 2012-10-24 13:50:00.0
How I can convert this timestamp to date in presto/sql?
As far as explained in the documentation, prestoDB seems to expect timestamps in a format '2001-08-22 03:04:05.321', and dates in a '2001-08-22'.
One solution would be to use a string function to extract the relevant part of the string before converting it. We know that the date part is located before the first space in the string, so.
If you need the date part as a string datatype:
split_part(created, ' ', 1)
If you need the date part as a date datatype:
cast(split_part(created, ' ', 1) as date)
You can try to use one of the following solutions:
SELECT
'2017-03-29 10:32:28.0' AS input_string,
DATE(date_parse('2017-03-29 10:32:28.0', '%Y-%m%-%d %H:%i:%s.%f')) AS solution_1,
DATE(try_cast('2017-03-29 10:32:28.0' as timestamp)) AS solution_2

Converting string to date in sql

I need to convert 2014-11-18T14:08:43+00:00 which is in varchar in my sql developer to date format in order to filter a few entries.
I tried
to_date(LAST_UPDATE_DATE,'YYYY-MM-DD')
but it gives an error
ORA-01830: date format picture ends before converting entire input
string.
Kindly help..
Just in case you didn't mean to put up sql server but instead you need to use oracle (seeing as you are using to_date and you are getting an ora exception)
I added a quick datetime conversion for date and timestamp (no milliseconds) for your date format:
SELECT to_Date(concat
(substr
(myvar,0,10),
concat(' ',
substr(myvar,12,8)
)
),'YYYY-MM-DD HH24:mi:ss') AS mydate
FROM mytable
Fiddle
declare #varDate as nvarchar(50) = '2014-11-18T14:08:43+00:00'
select CAST(substring(#varDate,0,CHARINDEX('T',#varDate)) as date)
Either you can use it like this
declare #Date as nvarchar(100) = '2014-11-18T14:08:43+00:00'
SELECT CONVERT(DATE,#Date) AS Date
OR go for this answer which is accepted in this question
ORA-01830: date format picture ends before converting entire input string / Select sum where date query
ORA-01830: date format picture ends before converting entire input string.
to_date(LAST_UPDATE_DATE,'YYYY-MM-DD')
2014-11-18T14:08:43+00:00 is TIMESTAMP and not DATE.
First of all, you should never ever store DATE/TIMSTAMP as string. It is a database design flaw.
Anyway, you could convert it to TIMESTAMP WITH TIMEZONE.
For example,
SQL> SELECT to_timestamp_tz('2014-11-18T14:08:43+00:00',
2 'YYYY-MM-DD"T"HH24:MI:SS.FFTZH:TZM')
3 AS tm_stamp
4 FROM dual;
TM_STAMP
----------------------------------------------------------------
18-NOV-14 02.08.43.000000000 PM +00:00
SQL>
You could try this;
Select CAST ('2014-11-18T14:08:43+00:00' as date)
The assumption is you are in SQL Server 2012