Change SQL date format - sql

I have a column with this format 20150228 and I need to change it to 02/28/2015. Is there any function in SQL to do it.
Thanks

You can use this assuming the date is stored as a varchar:
SELECT CONVERT(VARCHAR(50),CONVERT(DATE,'20150228',112),101)
It returns:
02/28/2015

Use FORMAT function:
SELECT CONVERT(NVARCHAR(10), GETDATE(), 101)

Related

Convert Full Date as String to Date SQL Server

I have a table with the column EPDATA, varchar(25) field in the format down below.
How do I convert this format to YYYY-MM-DD?
E.g 2020-12-16
You can simply use try_conver().
FYI: try_convert() will return a NULL if the conversion fails.
Example:
Select try_convert(date,'Jun 10 2016 12:00AM')
Returns
2016-06-10
Convert it to date datatype and format it like so:
SELECT FORMAT(CAST(EPDATE AS DATE) ,'yyyy-MM-dd')
FROM tablename
Use FORMAT
Select FORMAT(COLUMNNAME, 'YYYY-MM-dd')
Also,By CONVERT
SELECT CONVERT(varchar, COLUMNNAME, 23)
You can convert the way you want with this query.
CASE
WHEN SUBSTRING(#date,1,3)='Jan' THEN SUBSTRING(#date,8,11)+'-01-'+SUBSTRING(#date,5,6)
WHEN SUBSTRING(#date,1,3)='Feb' THEN SUBSTRING(#date,8,11)+'-02-'+SUBSTRING(#date,5,6)
WHEN SUBSTRING(#date,1,3)='Mar' THEN SUBSTRING(#date,8,11)+'-03-'+SUBSTRING(#date,5,6)
WHEN SUBSTRING(#date,1,3)='Apr' THEN SUBSTRING(#date,8,11)+'-04-'+SUBSTRING(#date,5,6)
WHEN SUBSTRING(#date,1,3)='May' THEN SUBSTRING(#date,8,11)+'-05-'+SUBSTRING(#date,5,6)
WHEN SUBSTRING(#date,1,3)='June'THEN SUBSTRING(#date,8,11)+'-06-'+SUBSTRING(#date,5,6)
WHEN SUBSTRING(#date,1,3)='July'THEN SUBSTRING(#date,8,11)+'-07-'+SUBSTRING(#date,5,6)
WHEN SUBSTRING(#date,1,3)='Aug' THEN SUBSTRING(#date,8,11)+'-08-'+SUBSTRING(#date,5,6)
WHEN SUBSTRING(#date,1,3)='Sep' THEN SUBSTRING(#date,8,11)+'-09-'+SUBSTRING(#date,5,6)
WHEN SUBSTRING(#date,1,3)='Oct' THEN SUBSTRING(#date,8,11)+'-10-'+SUBSTRING(#date,5,6)
WHEN SUBSTRING(#date,1,3)='Nov' THEN SUBSTRING(#date,8,11)+'-11-'+SUBSTRING(#date,5,6)
WHEN SUBSTRING(#date,1,3)='Dec' THEN SUBSTRING(#date,8,11)+'-12-'+SUBSTRING(#date,5,6)
END

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)

How to convert date and time in SQL Server

I have the following columns in a table:
Signed_In_Date Signed_Out_Time
11/1/2005 12:00:00 am 11/1/2005 10:27:00PM
I would like to convert them to the following output:
Signed_In_Date Signed_Out_Time
11/1/2005 10:27:00PM
Is there a function or conversion code in SQL Server that would do it?
Assuming that the columns you're referring to are DATETIME columns, I would use the code below:
Date Only
SELECT CONVERT(VARCHAR(10), GETDATE(), 101)
Time Only
SELECT LTRIM(RIGHT(CONVERT(VARCHAR(20), GETDATE(), 100), 7))
You can see the queries in action / play with them here.
For Sign_In_Date Use
select CONVERT(VARCHAR(10),'11/1/2005 10:27:00PM',108)
Ouput:
11/1/2005
For Sing_Out_Time
declare #time time
set #time=cast('11/1/2005 10:27:00PM' as Time)
select convert(varchar(10),#time,100)
Output:
10:27PM
try that :
select CONVERT(VARCHAR(10),Signed_Out_Time,108) ,-- 108 is d/M/yyyy if you want mm/dd/yyy you should use 101
CONVERT(VARCHAR(8),Signed_In_Date,103)
You can use CONVERT to change datetime format to your own desirable format:
SELECT CONVERT(VARCHAR(10),Signed_In_Date,101) as 'Signed_In_Date',
CONVERT(VARCHAR(10),Signed_Out_Time,108) as 'Signed_Out_Time';
For more date format, go over this link:
http://www.sql-server-helper.com/tips/date-formats.aspx

How do I convert a datetime field to a formatted string in SQL Server 2005?

I want to SELECT a formatted date string from a datetime type in SQL Server 2005.
In the format "yyyy/mm/dd hh:mm:ss".
What is the best way to do using only a query?
Check out the CONVERT statement.
SELECT CONVERT(VARCHAR(20), getdate(), 120)
is closest to what you want. (Note the different separators (- instead of / ))
select convert(varchar, datetime_field, 120) from tablename;
will do almost what you want.
120 is the conversion "style", see here for more.

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)