I do have this sample value of "12/01/2013" which is a string.
How can I convert it directly to an SQL statement? The format must be in INT, like this: 20131201
So from "12/01/2013" to 20131201.
See Data Type Formatting functions
select cast(to_char(to_timestamp('12/01/2013', 'MM/DD/YYYY'), 'YYYYMMDD') as int)
this works with SQL Server
SELECT CONVERT(INT, SUBSTRING('12/01/2013',7,4) +
SUBSTRING('12/01/2013',0,3) +
SUBSTRING('12/01/2013',4,2)) AS dateInt
Result
20131201
Tested in MySQL:
SELECT CAST(CONCAT(SUBSTR("12/01/2013",7,4), SUBSTR("12/01/2013",4,2),SUBSTR("12/01/2013",1,2)) AS UNSIGNED INTEGER);
In postgreSQL you have the to_date function which accepts a date string and a format string and returns a date.
So you can:
postgres=# select to_date('12/01/2015', 'mm/dd/yyyy');
to_date
------------
2015-12-01
(1 row)
postgres=# select to_date('12012015', 'mmddyyyy');
to_date
------------
2015-12-01
(1 row)
You can do it using CONVERT and REPLACE function like below.
DECLARE #dateValue date
SET #dateValue = CONVERT(DATETIME,'12/01/2013',102)
SELECT REPLACE ( CONVERT(nvarchar,#dateValue) , '-' , '' )
Rsult will be
20131201
Related
I've got a string here which needs to be converted into date but the problem is that it could either be in 'DD/MM/YYYY' or 'YYYY-MM-DD' format.
I've already tried convert which only works for one of the two formats but not both:
declare #string nvarchar(255) = '2019-05-21'
declare #table table (date date)
insert into #table
select convert(date, #string, 111) as date
select * from #table
declare #string nvarchar(255) = '21/05/2019'
declare #table table (date date)
insert into #table
select convert(date, #string, 103) as date
select * from #table
Both of the above solutions result in an error is I use the other format.
Is there a way to get a string converted to date regardless of what format it is in?
Use try_convert():
insert into #table
select coalesce(try_convert(date, #string, 111),
try_convert(date, #string, 103)
) as date
try_convert() returns NULL if the conversion fails. In that case, the conversion will move on to the next pattern. With coalesce(), you can have as many different formats as you like.
You can use TRY_PARSE or PARSE to parse the date literal using a specific culture.
The second format YYYY-MM-DD is an unambiguous date format for the "new" date types like date and datetime2. It's not affected by the DATEFORMAT setting like datetime.
This means you only need to find one culture that can handle the first format. All of the following queries will return the same value :
select parse('21/05/2019' as date using 'en-GB')
-----
2019-05-21
select parse('2019-05-21' as date using 'en-GB')
-----
2019-05-21
select try_parse('21/05/2019' as date using 'en-GB')
-----
2019-05-21
select try_parse('2019-05-21' as date using 'en-GB')
-----
2019-05-21
If you are on SQL 2012 and above, you can use the FORMAT function.
The signature of this function is - FORMAT (value,format[,culture])
Example: SELECT FORMAT (getdate(), 'dd-MM-yyyy') as date and in your case SELECT FORMAT(CAST(<str_value> as DATE), 'yyyy-mm-dd')
I am struggling to find a solution for the below issue.
date1 = 31-08-2017 12:10:00
I want to cast it as string and need to take date (31-08-2017) alone.
This is my SQL statement:
select *
from table_name
where cast(date1 as varchar) = '2017-08-30'
Here '2017-08-30' is string; when I ran the above select command it's showing o records as date1 is varchar but time also is included.
Can anyone tell me how to split date column alone as a string?
this will help you :-
select * from table_name WHERE CONVERT(varchar(23), [YourDateColumn], 121)= '2017-08-30 00:00:00.000'
above will accept both date and time
if you want to use only date then try this :-
select * from table_name WHERE CONVERT(varchar(10), [YourDateColumn], 20)= '2017-08-30 00:00:00.000'
If your date1's type is DateTime, Try this,
select *
from table_name
where CAST(date1 AS DATE) = CAST('2017-08-30' AS DATE)
This will help you.
select *
from table_name
Where CONVERT(VARCHAR(100),date1,102) = '2017.08.30'
date1 is column name value= '30-08-2017 12:10:00'
Output will be 30-08-2017
I have a table where the varchar column CREATED_BY has the data in the format
USER - dd/MM/yyyy hh:mm.
I'm trying to do data migration and need to get records where the created date is greater than a certain date, but the format of the column makes this difficult, so
SELECT * FROM TABLE_NAME WHERE -- last part of CREATED_BY > SOMEDATE
Well, since the dd/MM/yyyy hh:mm format has a fixed length, you can use the RIGHT function to extract the data part from your string:
SELECT *
FROM TABLE_NAME
WHERE CONVERT(datetime, RIGHT(CREATED_BY, 16), 103) > somedate
You need to extract the date from the string:
WHERE cast(reverse ( substring ( reverse ( #string ) , 1 , 16 ) ) as datetime) > somedate
Consider the following query (in MSSQL 2008):
SELECT dateModified FROM SomeTable;
This returns floats in javascript format (milliseconds since 1970):
dateModified
============
1301598290687
1071003581343
1311951478593
How can I convert this to a datetime2 right in the select?
Using the formula from #Mikeal Eriksson's answer here.
I would convert the float to a bigint and then create the datetime:
select
DATEADD(MILLISECOND,
cast(dateModified as bigint) % 1000,
DATEADD(SECOND, cast(dateModified as bigint) / 1000, '19700101'))
from sometable
See SQL Fiddle with Demo
Some Oracle example - replace to_date() with eqivalent:
SELECT (1301598290687/60/60/24/1000) as Days
, to_date('01-01-1970','dd-mm-yyyy') as start_date
, to_date('01-01-1970','dd-mm-yyyy')+(1301598290687/60/60/24/1000) as converted_date
FROM dual
/
DAYS START_DATE CONVERTED_DATE
---------------------------------------------------------
15064.7950310995 1/1/1970 3/31/2011 7:04:51 PM
Create dual table:
CREATE TABLE DUAL
(
DUMMY VARCHAR(1)
)
GO
INSERT INTO DUAL (DUMMY)
VALUES ('X')
GO
I want to extract just the date part from a timestamp in PostgreSQL.
I need it to be a postgresql DATE type so I can insert it into another table that expects a DATE value.
For example, if I have 2011/05/26 09:00:00, I want 2011/05/26
I tried casting, but I only get 2011:
timestamp:date
cast(timestamp as date)
I tried to_char() with to_date():
SELECT to_date(to_char(timestamp, 'YYYY/MM/DD'), 'YYYY/MM/DD')
FROM val3 WHERE id=1;
I tried to make it a function:
CREATE OR REPLACE FUNCTION testing() RETURNS void AS '
DECLARE i_date DATE;
BEGIN
SELECT to_date(to_char(val1, "YYYY/MM/DD"),"YYYY/MM/DD")
INTO i_date FROM exampTable WHERE id=1;
INSERT INTO foo(testd) VALUES (i);
END
What is the best way to extract date (yyyy/mm/dd) from a timestamp in PostgreSQL?
You can cast your timestamp to a date by suffixing it with ::date. Here, in psql, is a timestamp:
# select '2010-01-01 12:00:00'::timestamp;
timestamp
---------------------
2010-01-01 12:00:00
Now we'll cast it to a date:
wconrad=# select '2010-01-01 12:00:00'::timestamp::date;
date
------------
2010-01-01
On the other hand you can use date_trunc function. The difference between them is that the latter returns the same data type like timestamptz keeping your time zone intact (if you need it).
=> select date_trunc('day', now());
date_trunc
------------------------
2015-12-15 00:00:00+02
(1 row)
Use the date function:
select date(timestamp_field) from table
From a character field representation to a date you can use:
select date(substring('2011/05/26 09:00:00' from 1 for 10));
Test code:
create table test_table (timestamp_field timestamp);
insert into test_table (timestamp_field) values(current_timestamp);
select timestamp_field, date(timestamp_field) from test_table;
Test result:
Have you tried to cast it to a date, with <mydatetime>::date ?
In postgres simply :
TO_CHAR(timestamp_column, 'DD/MM/YYYY') as submission_date
This works for me in python 2.7
select some_date::DATE from some_table;
Just do select date(timestamp_column) and you would get the only the date part.
Sometimes doing select timestamp_column::date may return date 00:00:00 where it doesn't remove the 00:00:00 part. But I have seen date(timestamp_column) to work perfectly in all the cases. Hope this helps.
CREATE TABLE sometable (t TIMESTAMP, d DATE);
INSERT INTO sometable SELECT '2011/05/26 09:00:00';
UPDATE sometable SET d = t; -- OK
-- UPDATE sometable SET d = t::date; OK
-- UPDATE sometable SET d = CAST (t AS date); OK
-- UPDATE sometable SET d = date(t); OK
SELECT * FROM sometable ;
t | d
---------------------+------------
2011-05-26 09:00:00 | 2011-05-26
(1 row)
Another test kit:
SELECT pg_catalog.date(t) FROM sometable;
date
------------
2011-05-26
(1 row)
SHOW datestyle ;
DateStyle
-----------
ISO, MDY
(1 row)
You can use date_trunc('day', field).
select date_trunc('day', data_gps) as date_description from some_table;