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

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

Related

How to have a good format of date?

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.

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

Convert Data from yyyy-dd-mm to mm/dd/yyyy Issue

I have a column in my table with Dates in the format yyyy-mm-dd I want to convert all the dates in that column to the format mm/dd/yyyy
I am using the below query
UPDATE Test.dbo.Status
SET DateIn = CONVERT(DATE,DateIn ,101)
The DateIn column is defined as Date in my table (DateIn DATE NULL)
The query does no change to the data. am I doing some thing wrong here?
You can change the default format in which SQL Server displays a date, but you can't alter the way a DATE value is stored via CONVERT(). You can format a date however you want if you store it as a string, but you lose functionality when you do that and it's not advisable. If you are hell-bent on storing a formatted version, you might want to create a new VARCHAR() field so you can preserve your DATE version.
You're better off formatting the date at the application level.
The reason your query does nothing is that the actual DATE values are equivalent. Notice when you take any valid date format and CAST() it as DATE the resulting format is the same regardless of the input:
SELECT CAST('20040510' AS DATE)
SELECT CAST('2004-05-10' AS DATE)
SELECT CAST('May 10, 2004' AS DATE)
All return: 2004-05-10 on my instance of SQL Server.

How do I display DATE in 'DD MON YYYY' format?

I am a newbie for Oracle database programming and I wish to INSERT date (also display) in 'DD MON YYYY' format. (PS: This only involves INSERT event). Which data type (DATE or TIMESTAMP) is the most suitable option for me in order to accomplish this format? How was I supposed to do that? Thanks.
A DATE column does not have any format.
So the format that you use when inserting or updating data is irrelevant for displaying that data (that's one of the reasons why you should never store a date in a VARCHAR column).
Any formatted output you see for a DATE column in your SQL tool (e.g. SQL*Plus) is applied by that tool. It is not part of the data stored in that column.
When providing a date literal you should either use the to_date() function with an explicit format mask:
insert into some_table (some_date_column)
values (to_date('27-06-2014', 'dd-mm-yyyy'));
I also do not recommend using formats with written month names (27-JUN-2014) when supplying a date literal because they also depend on the NLS settings of the client computer and might produce (random) errors due to different languages. Using numbers only is much more robust.
I prefer to use ANSI date literals because it's a bit less typing:
insert into some_table (some_date_column)
values (DATE '2014-06-27');
The format for an ANSI date (or timestamp) literal is always the ISO format (yyyy-mm-dd).
When you select your data you can display the date in whatever format you like. Either by using the to_char() function (e.g. when using a SQL tool) or using functions from your programming language (the preferred way to use inside an application):
select to_char(some_date_column,'dd-mon-yyyy')
from some_table;
Note that the DATE data type in Oracle (despite it's name) also stores the time. a TIMESTAMP does the same thing only with a higher precision (it includes milliseconds, whereas the DATE data type only stores seconds).
To insert a record,
INSERT INTO TABLE_NAME (DATE_FIELD) VALUES (TO_DATE ('27-JUN-2014', 'DD-MON-YYYY');
It is advisable to use DATE data-type until and unless you need the date's accuracy to be till milli seconds. In your case, go with DATE datatype and TIMESTAMP is not necessary
To select a record,
SELECT TO_CHAR(DATE_FIELD, 'DD-MON-YYYY') FROM TABLE_NAME;
In genral, remember this:
TO_DATE is a function used to convert a string(CHAR) TO DATE
TO_CHAR is a function used to convert a DATE to a string(CHAR)
In this scenario date datatype will be suitable for you, and for the desired format you should try like this:-
INSERT INTO TABLE_NAME(DATE_COLUMN) VALUES('27-JUN-2014');
Hope this can help you.

Insert only Month and Year date to SQL table

I am using MS SQLServer and trying to insert a month/year combination to a table like this:
INSERT INTO MyTable VALUES (1111, 'item_name', '9/1998')
apparently, the above command cannot work since
Conversion failed when converting date and/or time from character string.
Because 9/1998 is a bad format. I want to fix this and this column of the table will show something like:
9/1998
12/1998
(other records with month/year format)
...
Can someone help me with this?
thank you
SQL Server only supports full dates (day, month, and year) or datetimes, as you can see over on the MSDN data type list: http://msdn.microsoft.com/en-us/library/ff848733(v=sql.105).aspx
You can use a date value with a bogus day or store the value as a string, but there's no native type that just stores month/year pairs.
I see this is an old post but my recent tests confirm that storing Date or splitting the year and month to two columns (year smallint, month tinyint) results in the overall same size.
The difference will be visible when you actually need to parse the date to the filter you need (year/month).
Let me know what do you think of this solution! :)
Kind regards
You can just use "01" for the day:
INSERT INTO MyTable VALUES (1111, 'item_name', '19980901')
You can:
1) Change the column type to varchar
2) Take the supplied value and convert it to a proper format that sql server will accept before inserting, and format it back to 'M/YYYY' format when you pull the data: SELECT MONTH([myDate]) + '/' + YEAR([myDate]) ...
You may want to consider what use you will have for your data. At the moment, you're only concerned with capturing and displaying the data. However, going forward, you may need to perform date calculations on it (ie, compare the difference between two records). Because of this and also since you're about two-thirds of the way there, you might as well convert this field to a Date type. Your presentation layer can then be delegated with the task of displaying it appropriately as "MM/yyyy", a function which is available to just about any programming language or reporting platform you may be using.
if you want use date type, you should format value:
declare #a date
SELECT #a='2000-01-01'
select RIGHT( convert (varchar , #a, 103), 7) AS 'mm/yyyy'
if you want make query like SELECT * FROM...
you should use varchar instead date type.