How to convert the date format in SQL Server? - sql

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

Related

HOW TO INSERT DATE WITH DOTS FORMAT IN SQL

I want to insert a date like 31.12.2021 in SQL, and I get something wrong
what should I write to make it works?
In sql-server you can use convert(varchar, [date you want to convert], 104) if your column is a date datatype as shown in microsoft sql-server cast and convert docs.

Parse values of different type to date

I'm trying to parse certain columns into a date format and have had success doing so, but have run into an error due to inconsistency that lies in the data.
My date columns are currently integers in the form of YYYYMMDD, so I've used the following in the select statement to parse into dates:
CONVERT(datetime, CAST(date_column AS CHAR(8)), 112)
This works as expected, transforming my data into a YYYY-MM-DD format.
I run into the following error though:
Conversion failed when converting date and/or time from character
string.
After looking through the data a bit, it turns out I have some cases of inconsistent data values, such as -1 and 10630 instead of the expected YYYYMMDD value.
Do I just need to add a WHERE statement to only apply CONVERT and CAST to YYYYMMDD fields while filtering out fields with bad data? If so, how would I do this, or is there a better way?
Thanks in advance
Let me assume you are using SQL Server, based on the syntax of your SQL. You shouldn't actually need the 112. 'YYYYMMDD' is the default date format for SQL Server.
In any case, you can use TRY_CONVERT():
TRY_CONVERT(datetime, CAST(date_column AS CHAR(8)), 112)
This returns NULL if the value cannot be converted. You can find the offending values using:
select date_column
from t
where try_convert(datetime, cast(date_column as char(8)) is null and
date_column is not null;
Use Parse() or Try_parse()
select parse('22-JAN-1989' as date)
select parse('04-March-1992' as date)
select parse('03/10/2020' as date)
select parse('07-05-1958' as date)
select parse('Aug 25,2016' as date)
Every example above returns a valid date.
You can also add USING 'en-US' or such if you want a different culture.
If you are getting numeric values, and you know the starting date, use
select IsNull(try_parse('16500' as date),dateadd(d,cast('16500' as int),'1/1/1980'))

SQL Server clean up inconsistent dates

I have dates currently stored as varchar(1000) that are in inconsistent formats. I.e. yyyy-mm-dd and dd-mm-yyyy etc.
What query can I run to clean these up so they are in a consistent format, so I can convert the column datatype from varchar(1000) to date?
Thanks
This could get ugly if you have many different date formats, possibly including invalid data. One option would be to assign a new date column using a CASE expression to choose the correct format mask:
UPDATE yourTable
SET date_col = CASE WHEN date LIKE '[0-9]{2}-[0-9]{2}-[0-9]{4}'
THEN CONVERT(datetime, date, 105)
WHEN date LIKE '[0-9]{4}-[0-9]{2}-[0-9]{2}'
THEN CONVERT(datetime, date) END;
You may add conditions to the above CASE expression for other date formats.

I want to convert a whole column into the date format of yyyymmdd, I do not want the current date

I want to convert a whole column into the date format of yyyymmdd, I do not want the current date, thus I cannot use getdate() command, there is already data in the column, I just need the right command to convert the whole column into yyyymmdd format.
The column I am using is FIELD_034 and the table is Sur_CompassAuto1_1_7_fetch.
I am using SQL Server.
Thank you
I fully agree with the paqogomez's response, but instead of date style of 111, better use 112.
The output for the statement
SELECT CONVERT(VARCHAR(10), FIELD_034, 111)
will result in yyyy/MM/dd. Where as,
SELECT CONVERT(VARCHAR(10), FIELD_034, 112)
will return yyyyMMdd, which is what he needed.
A common misconception is that a datetime has a format. If you are storing your dates as a datetime, then you can output it in any format.
It sounds as though you might be storing your values as a Varchar. You would be better off converting your varchar dates into a datetime, then you can do whatever you want with them
That said, if you HAVE FIELD_034 as a DateTime, then its as easy as
SELECT CONVERT(VARCHAR(10), FIELD_034, 112)
from Sur_CompassAuto1_1_7_fetch
If the field is a varchar its similar:
SELECT CONVERT(datetime, FIELD_034, 112)
from Sur_CompassAuto1_1_7_fetch
The difficulty of this one is if you have your values in different or nonstandard formats. Then it would require some clean up to make the query work.
Edit: as #AArnold says, its 112 instead of 111. Date formats are subtle.

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),' ','-'))