Convert Text to Date in SQL Server - sql

How to I convert a text value like "18/06/11" to a date format like "2011-06-18"?
I have tried the following
convert(char,[InstrumentText], 106)
but the value just stays in the same format
Thanks

The correct format for your string would appear to be 103.
More importantly, you need to convert to a datetime not to a char:
convert(date,[InstrumentText], 103)
If you then want to convert it back to a string in the format yyyy-mm-dd, you can do:
convert(varchar(10), convert(date,[InstrumentText], 103), 120)

Try
SELECT convert(datetime, '18/06/11' , 3)

I grabbed this from sqlusa.com:
SELECT CAST([InstrumentText] AS datetime)

Related

converting string format to date format

I am not able to convert the string to DATE format
select CONVERT(DATE, '8/17/2016', 103)
Conversion failed when converting date and/or time from character string.
what can we do to resolve this
thanks
Actually you don't need that style, It is the one causing the error. Just CAST/CONVERT it to date. Try this
select CONVERT(DATE, '8/17/2016')
You can try with cast also
select cast( '8/17/2016' as date)
SELECT CONVERT(DATE, '8/17/2016', 101)
SELECT convert(datetime, '8/17/2016', 111)

How to convert string date into valid date format in SQL Server 2008?

I have string date in yymmdd format, for example 150202
I want to convert this string into a valid date in format yyyymmdd, e.g. 20150202.
Thanks in advance.
convert your string to datetime and then do that you want with it
declare #dt varchar(6) = '150213'
select CONVERT(datetime, #dt, 112)
Do another CONVERT to transform it to yyyymmdd format.
SELECT CONVERT(VARCHAR(10), CONVERT(DATETIME, #dt, 112), 112)
may this will work
select CONVERT(datetime, '150202', 112)
for all date conversions
http://www.sqlusa.com/bestpractices/datetimeconversion/
instead of select CONVERT(datetime, '150202', 112)
its better to use "select TRY_CONVERT(datetime, '150202', 112)"
while using try_convert if there is any error it will returns null,if we are using convert it will returns error when conversion fails.

Convert format of date

I have a date format like
'2003-11-27 00:00:00.000'
How can I convert it into YYYYMMDD like 20031127 WITH SQL SERVER ?
To convert the date as per ISO standard you can write as:
SELECT CONVERT (VARCHAR(8), GETDATE(),112) as [YYYYMMDD]
SELECT CONVERT (VARCHAR(8), cast('2003-11-27 00:00:00.000' as datetime), 112)
It depends of how your date is declared.
DECLARE #date datetime = '2003-11-27T00:00:00.000' -- datetime
DECLARE #date2 char(23) = '2003-11-27 00:00:00.000' -- char(23)
SELECT
convert(char(8), #date, 112) datetimeconvert,
convert(char(8), convert(datetime, #date2, 121), 112) charconvert
Result:
datetimeconvert charconvert
20031127 20031127
The date types in SQL Server do not have any format, they are binary types. Formats apply only when you convert them to text or try to parse a text literal.
You don't specify what is the value you posted, or what you want to do with it. There are several possibilities:
You want to truncate the time portion of a datetime field. Then just cast(myField as date)
You want to create a text string containing the date portion of a datetime or date field: FORMAT(myField,'yyyyMMdd') or CONVERT(varchar(8),myField,112). FORMAT provides almost as much flexibility as .NET's String.Format but in this case CONVERT it's enough.
You want to convert one text literal to another: FORMAT(CONVERT(date,'2003-11-27 00:00:00.000',121),'yyyyMMdd'). You convert the original string to a date then format it as you wish.

Convert a datetime column into a specific date format. (T-SQL)

I have a datetime column (SellDate) that I want to output in the following format:
mm/dd/yyyy
such as 06/01/1998
How would I do this? Thanks in advance!
Convert it to a string with the appropriate conversion style;
convert(varchar(10), thedatecol, 101)
to get date in DD/MM/YYYY
convert(varchar(10), TransactionDateTime, 103)
OutPut: 01-07-2018

How do I convert a datetime column to nvarchar with the format DD/MM/YYYY?

I need to select a datetime column in a table. However, I want the select statement to return the datetime as a nvarchar with the format DD/MM/YYYY.
Here is the convert documentation:
https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql
Looking through that, it looks like you want style 103:
SELECT CONVERT(nvarchar(10), getdate(), 103)
This should help. It contains all (or most anyway) the different date formats
http://wiki.lessthandot.com/index.php/Formatting_Dates
I think you'd be better off handling the string conversion in client if possible.
You can convert a date in many formats, in your case :
CONVERT(NVARCHAR(10), YOUR_DATE_TIME, 103) => 15/09/2016
CONVERT(NVARCHAR(10), YOUR_DATE_TIME, 3) => 15/09/16
Syntax:
CONVERT('TheDataTypeYouWant', 'TheDateToConvert', 'TheCodeForFormating' * )
The code is an integer, here 3 is the third formatting option (without century), if you want the century just change the code to 103.
See more at: http://www.w3schools.com/sql/func_convert.asp
select CONVERT (NVARCHAR, GETDATE(), 103)
Look up convert in BOL.
Use Convert with the 103 option.
select convert(nvarchar(10), datefield, 103)