Datetime format - sql

how do I convert a datetime to look like "dd/MM/yyyy" in a stored procedure?

do like
SELECT convert(varchar, getdate(), 103) – dd/mm/yyyy
and see other format from here http://anubhavg.wordpress.com/2009/06/11/how-to-format-datetime-date-in-sql-server-2005/

Related

How to convert string dd.mm.yyyy to date in SQL

I have column field of varchar type in format dd.mm.yyyy. I am trying to convert this varchar field to date time in SQL server using
CONVERT(Datetime, LTRIM(RTRIM([Completion Date])), 102)
but it gives me error 'Conversion of varchar type to date type results in out of range value'
How to convert this field to datetime format?
use style 104 (dd.mm.yyyy) instead of 102 (yyyy.mm.dd)
CONVERT(Datetime, LTRIM(RTRIM([Completion Date])), 104)
Also the field of type varchar so there could be some bad dates which needs to be eliminated before the conversion .
If you are using SQL SERVER 2012+ then use TRY_CONVERT, for bad dates it will result NULL
TRY_CONVERT(Datetime, LTRIM(RTRIM([Completion Date])), 104)
You can always use this:
SELECT convert(datetime, '23/07/2009', 102)
You can also use this:
SELECT convert(date, '21.12.2016', 104)
You can use convert with date and datetime as below:
SELECT convert(datetime, '27-09-2013', 104)
SELECT convert(date, '27-09-2013', 104)

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 Text to Date in SQL Server

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)

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