change the date format in sql server - sql

I have a date value as below in my table.
2015-05-25
I want to convert the values as below.
05/25
How to do this? Date value is having date datatype.

Use the CONVERT function to change it to mm/dd/yy (style 1)
And the LEFT function to only select mm/dd (integer_expression 5)
SELECT LEFT(CONVERT(date, 1), 5)
FROM yourtable
Input:
2015-05-25
Output:
date
05/25

Try:
select convert(varchar(5),getdate(),101)

Give it format by using Tostring()
string date = YourDate.ToString("MM/dd");

Write as:
SELECT Left(CONVERT(VARCHAR(8), GETDATE(), 1),5) AS [MM/DD]

If you are using SQL Server 2012 (or more), I advise you using the new FORMAT function:
SELECT FORMAT(#date, 'MM/dd')

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

Concatenate String + date in SQL Server

I have the following data:
KEY ID DATE
123456789 09BA2038 01-01-2017
And I would like to concatenate it, but keep the original format of the date. When I try:
CONCAT(Key, '-', ID, '-', DATE)
it gives me an output of
123456789-09BA2038-Jan 01 2017 11:00AM
But I would like the output to be
123456789-09BA2038-01-01-2017
If you're using SQL Server 2012 or newer, then you can use FORMAT to change a date or datetime into a varchar with the format of your liking.
select CONCAT([Key],'-',ID,'-',FORMAT([DATE],'MM-dd-yyyy')) as Key2
from (values (123456789,'09BA2038',convert(date,'2017-01-15',126))) v([Key],ID,[DATE]);
Result:
Key2
123456789-09BA2038-01-15-2017
Or you could use CONVERT instead using the 110 style for the USA date format.
Convert the date, I am guessing you want the format mm-dd-yyyy, if so:
CONCAT([key],'-',[ID],'-',CONVERT(VARCHAR(10), [DATE], 110))
If you want dd-mm-yyyy it is:
CONVERT(VARCHAR(10), [DATE], 105)
You need to use an explicit convert to get the date format you want.
In this case CONCAT(Key, '-', ID, '-', convert(varchar(10),DATE,105)) should work fine.
You can find the full list of formats here: https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql
Try CONCAT(Key, '-', ID, '-', CONVERT(varchar(10),DATE, 110)). The 110 tells SQL Server to format the date as 'mm-dd-yyyy' (US style).
For more information about this look here.
Niels
I think not using concat is better in this situation
like this:
select convert(nvarchar(100),KEY)+'-'+convert(nvarchar(100),ID)+'-'+convert(nvarchar(100),DATE)
from tableName

SQL Convert Date from dd.mm.yyyy to dd-mm-yyyy

I want to select data which is set in a specific date range. Unfortunately I get the date in this form:
01.05.2016 and 02.06.2016
In the database, the date are in the form:
2013-06-21
How can I convert the date in my sql query?
SELECT `artikel`.*
FROM `artikel`
WHERE (buchungsdatum >= '01.05.2016') AND (buchungsdatum <= '02.06.2016')
If this is MySQL, you can use STR_TO_DATE:
SELECT `artikel`.*
FROM `artikel`
WHERE
buchungsdatum >= STR_TO_DATE('01.05.2016','%d.%m.%Y')
AND buchungsdatum <= STR_TO_DATE('02.06.2016', '%d.%m.%Y')
Check here for the available date formats.
Try DATE_FORMAT() function of mysql as DATE_FORMAT() function is used to display date/time data in different formats.:
SELECT `artikel`.*
FROM `artikel`
WHERE (DATE_FORMAT(buchungsdatum, "%d.%m.%Y") >= '01.05.2016') AND (DATE_FORMAT(buchungsdatum, "%d.%m.%Y") <= '02.06.2016')
You can try this:
select
SUBSTRING(convert(varchar(10),REPLACE('01.05.2016','.', ''),103),5,4)+ '-'+
SUBSTRING(convert(varchar(10),REPLACE('01.05.2016','.', ''),103),3,2)+'-'+
SUBSTRING(convert(varchar(10),REPLACE('01.05.2016','.', ''),103),1,2)
It formats 01.05.2016 to 2016-05-01
I have to mention that it changes the 01.05.2016 to 01052016 and then it formats it.

Change SQL date format

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)

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