How to have a good format of date? - sql

I have this kind of date in my sql table : 1/24/2018 10:34:23 PM
And I want to extract the date with this format : YYYYMMDD that is to say I would like to have this 20180124
How could I do this ?
I tried this :
try_parse([date] as date using 'en-US')
but the format is wrong ...
I precise the name of my column is date

use the below to format dates in the YYYYMMDD format;
SELECT
CONVERT(VARCHAR(8), [DateField],112) AS DateField

Added as answer as requested...
Dates don't have formats in SQL Server, how you display them is the format. Strings have formats.
You can use FORMAT or CONVERT depending on your version.
convert(varchar(8),yourColumn,112)
format(yourCOlumn,'yyyyMMdd')
DEMO
declare #table table (yourColumn datetime)
insert into #table
values
(getdate())
select
yourColumn
,format(yourColumn,'yyyyMMdd')
,convert(varchar(8),yourColumn,112)
from #table
If your column is a string, then you need to convert that to a date first or just parse it a different way.
select
convert(varchar(8),cast('1/24/2018 10:34:23 PM' as datetime),112)
,format(cast('1/24/2018 10:34:23 PM' as datetime),'yyyyMMdd')

I usually refer to a question already answered on Stack Overflow in the below link.
How to convert DateTime to VarChar
The answer by Colin really works for me and a source of reference eveytime I have confusions around datetimes and formats in SQL Server.

Related

Convert decimal to date format SQL

I am trying to convert a column FC_FROM_DATE containing decimal values (ex. 20,200,721) into a date format 2020/07/21.
I have tried this code
SELECT TO_DATE(CHAR(CAST(FC_FROM_DATE AS DECIMAL(8,0))), 'YYYY/MM/DD')
FROM MARKETS.FORECAST
I get an error
Argument for chr should be between 0 and 127
Would much appreciate your help!
If you are using Oracle then you can use the following query:
SELECT TO_DATE(CAST(FC_FROM_DATE AS VARCHAR(8), 'YYYY/MM/DD') FROM Table
Seeing as the persisted format is YYYYMMDD you could convert the value to a varchar(8) and then use CONVERT to get a Date instance.
SELECT CONVERT(DATE, CAST(FC_FROM_DATE AS VARCHAR(8))) FROM MARKETS.FORECAST
Ideally Dates are stored as Date and a Date with a time component is stored as DATETIME2 (or equivalent if not Sql Server).
Test of the code above
DECLARE #FC_FROM_DATE decimal(8,0) = 20200721
SELECT CONVERT(DATE, CAST(#FC_FROM_DATE AS VARCHAR(8)))
2020-07-21
Disclaimer: This works in MS Sql Server.

How to convert column type from str to date when the str is of format dd/mm/yyyy?

I have a large table in sql I imported from a large csv file.
A column is recognized as a str when it contains date information of format dd/mm/yyyy.
I tried select TO_DATE('12/31/2015') as date but that does not work because TO_DATE function needs yyyy-mm-dd format.
How can I rearrange the '12/31/2015' string to '2015-12-31' format inside sql so that I can convert the column type to date?
I am doing this on a sparkSQL (on databricks environment ) due to the very large size of the data where the update keyword of sql does not seem to be supported.
Just re-read your question;
I would suggest this:
UPDATE table
SET column = Convert(varchar(10), Convert(smalldatetime, column, 103), 120)
This converts the column value to smalldatetime, using the british format (dd/mm/yyyy), then converts it back to varchar, using the 120 format (yyyy-mm-dd); The 120 format contains time info, but this will be truncated because it's being cast back as varchar(10);
Test it:
SELECT Convert(varchar(10), Convert(smalldatetime, column, 103), 120)
FROM table
The below link answer works
https://forums.databricks.com/answers/12121/view.html
df.withColumn("tx_date", to_date(unix_timestamp($"date", "M/dd/yyyy").cast("timestamp")))

How to convert the date format in SQL Server?

I have a table that contains date and the format is :'01-16-1989' which is mm-dd-yyyy but I want to insert to another table that has format like this: '1989-01-16' which is yyyy-mm-dd. What function can I use in the insert statement to do this?
insert into des_table
select date from source_table
How to update the second line in order to finish the date format conversion?
You can convert a string from mm-dd-yyyy format to a date using conversion type 110:
select convert(date, [date], 110)
from source_table
You can then convert this back to a string in the yyyy-mm-dd format using code 120:
select convert(varchar(10), convert(date, [date], 110), 121)
You can format date with DATE_FORMAT()
This is one way
SELECT DATE_FORMAT(date, '%Y-%m-%d') FROM source_table;
Source: http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_date-format
Unless they are not actually datetime data types, your insert should be golden as it is.
Also, to answer this you should tell us what RDBMS you are using.
If you are using MySQL this should get you covered:
select DATE_FORMAT(current_date,'%y/%m/%d');
On the dates note you should keep all your dates
ISO formated
Since your column is already a DATE you don't need to do any conversions on the insert. If you want it to look a certain way in a specific query result, you can use CONVERT. There are many format options, but again, you don't need to bother with changing how it looks on the insert.
Here are some resources
https://msdn.microsoft.com/en-us/library/ms187928.aspx
https://technet.microsoft.com/en-us/library/ms187928(v=sql.105).aspx
http://www.w3schools.com/sql/func_convert.asp

how to insert a string as date with 106 format in mssql database?

I need to insert a string as date into my MSSQL database.
I am trying this query but it is not inserting as per my expectation.
Insert into date_table (CONFIG_DATE)
Values (CONVERT(VARCHAR(11),CONVERT(DATETIME,'18-SEP-13',103),106));
Inserted as : 2013-09-18
but expected output is 18-SEP-2013
Any help would be appreciated on this..
I am not wure what you expecting by inserting date value in this format since I can't see any advantage or posibility of that.
I think the best way is to store the value in date or datatime format in database and apply the formatting when you do the select.
I am not sure why you converting converted date back to a string before inserting. Date itself does not have a format. Formatting is used for presentation purposes.
Best thing is to store date as a Date not a string. First conversion should be fine with 106 style.
Insert into date_table (CONFIG_DATE)
Values (CONVERT(DATETIME,'18-SEP-13',106));
When converting strings to date type, safest practice is to use culture independent ISO formats. For example yyyymmdd (ISO format) and yyyy-mm-ddThh:mi:ss.mmm (no spaces) (ISO8601).
can you please provide your table schema ??
i think your column datatype is datetime, date or datetime2 and you cant insert 18-SEP-2013 in datetime ,date or datetime2 columns .. you need to insert it 2013-09-18 but when you selecting it at that time you can write
select CONVERT(varchar(30), CONFIG_DATE, 106)
try this
Insert into date_table (CONFIG_DATE)
Values (replace(CONVERT(VARCHAR(11),CONVERT(DATETIME,'18-SEP-13',103),106),' ','-'))

Formatting Dates In SqlServer To Use As Comparison

This should be easy
I have a date column on table A
SELECT * FROM TABLEA
WHERE DTE = '01/02/2010'
The problem is that this is deployed onto US servers and I have no control over the date as its an arg.
How can I tell SqlServer to treat this date as being in that format??
I gave tried this:
SELECT *
FROM TABLEA
WHERE DTE = CONVERT(VARCHAR(10), '01/01/2010' , 101) AS [DD/MM/YYYY]
Your last try was almost correct, but it should have been
SELECT * FROM TABLEA WHERE AS_OF_DATE = CONVERT(DATETIME, '01/01/2010', 101)
Use a safe format. For dates (without a time component), the safe format is YYYYMMDD, e.g. today is '20100209'.
For datetimes, the safe format is YYYY-MM-DD'T'HH:mm:SS, where 'T' is the literal T, so right now is '2010-02-09T11:10:30'.
(When I'm saying safe, I mean that SQL Server always, unambiguously, knows how to convert these strings into datetime values)
Check out this reference article: The ultimate guide to the datetime datatypes
EDIT: Specifically what Tibor says about SET DATEFORMAT & SET LANGUAGE, since you mention that you have no control over the input format.
Another option is a double conversion (check performance when used as criteria):
select strTempNo, dtmTempDateStart,
convert(varchar(10), Convert(datetime, dtmTempDateStart, 103), 126) As UTCDate
I use 103 here as the data is already in UTC format but this works as well (UTC ISO8601 is 126).
If your dates are known to be always in American format you have to use 101.
Alternatively use 112 (ISO "safe" format) and cut the first 8 characters out of the string.
Data sample: (Sorry, don't have an American date table available)
521002 2008-09-1500:00:00.000 2008-09-15
580195 2008-04-1500:00:00.000 2008-04-15
530058 2008-09-2200:00:00.000 2008-09-22
580194 2008-04-0200:00:00.000 2008-04-02
500897 2008-07-0100:00:00.000 2008-07-01
500966 2008-09-2300:00:00.000 2008-09-23