Convert format of date - sql

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.

Related

Convert datetime to specific format in SQL Server

How to convert datetime to this format : 2019/06/17 18:00:00.123
I use select convert(varchar, getdate(), 111) and select convert(varchar, getdate(), 108) together but I get only 2019/06/17 18:00:00
I can't find a combination to get format I need. Is there any way to get it?
You could use FORMAT to return the DATETIME in a specified format (assuming you use SQL-Server and want to return a string type).
DECLARE #example DATETIME = GETDATE();
SELECT FORMAT(#example, 'yyyy/MM/dd HH:mm:ss.fff');
use datetime2 data type and cast getdate to datetime2(3) for 3 digit precesion
select cast( getdate() as DATETIME2(3))

Converting string 'yyyy-mm-dd' to date

I want to select from table where date column is equal to specific date which I sending as a string in format 'yyyy-mm-dd'. I need to convert that string and than to compare if I have that date in my table.
For now I am doing this:
select *
FROM table
where CONVERT(char(10), date_column,126) = convert(char(10), '2016-10-28', 126)
date_column is a date type in table and I need to get it from table in this format 'yyyy-mm-dd' and because that I use 126 format. I am just not sure with the other part where I converting string which is already in that format and do I need to convert it because I don't know is it good to use this:
CONVERT(varchar(10), date_column,126) = '2016-10-28'
You don't need to convert the column as well. In fact, you better not convert the column, because using functions on columns prevents sql server from using any indexes that might help the query plan on that column.
Also, you are converting a string to char(10) - better just convert it to date:
where date_column = convert(date, '2016-10-28', 126)
Also, if you are using a datetime data type and not date, you need to check that the datetime value is between the date you pass to the next date.
You can convert string to date as follows using the CONVERT() function by giving a specific format of the input parameter
declare #date date
set #date = CONVERT(date, '2016-10-28', 126)
select #date
You can find the possible format parameter values for SQL Convert date function here
You do not need to do that. yyyy-MM-dd is the default format.
Please note that you need to take into account the time as well, if there's a timestamp in date_column. In that case you should write something like this
... WHERE date_column >= '2016-10-28 00:00:00' AND date_column < '2016-10-29 00:00:00'
... WHERE date_column BETWEEN '2016-10-28 00:00:00' and '2016-10-29 00:00:00'
As I just learned that (other than I thought) BETWEEN actually includes the end timestamp and thus is not equivalent to the above >= ... < approach.
This should use indexes properly as well.

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 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 datetime to specific format?

I want to convert a datetime to a specific format. The conversion should result in a variable of type datetime and not char or varchar. How do I do this in SQL server 2000, 2005 and 2008 ?
select CONVERT(varchar(30),getdate(),120)
I tried this, but it gives me a string. I want a datetime without the milli-seconds. SS 2012 has an option for this, but not previous versions.
http://blog.sqlauthority.com/2012/11/21/sql-server-display-datetime-in-specific-format-sql-in-sixty-seconds-033-video/
You can't have it both ways ... a variable of type "datetime" is defined as:
Defines a date that is combined with a time of day
with fractional seconds that is based on a 24-hour clock
You can CONVERT and DISPLAY in whatever format you choose, but the datetime data type will always have the milliseconds, even if you set them to zero.
You can remove the milliseconds from the datetime like this:
DATEADD(ms, -DATEPART(ms, date), date) > '2013-11-18 03:21:52'
Also check SQL Server Date Formats
or may be try like this to remove the millisecond part:-
declare #str datetime
set #str = '2013-11-18 17:24:05.784'
select convert(datetime, convert(char(19), #str, 126))