Concatenate String + date in SQL Server - sql

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

Related

Change date time format from YYY-MM-DD HH:MM:SS to YYYY.MM.DD HH:MM:SS in SQL Server

I have a table with a column RequestDate with following format 2019-12-01 00:00:00:000. I want to see results like this: 2019.12.01 00:00:00:000.
I used this command
SELECT CONVERT(char(10), RequestDate, 104) AS finaldate
From the above query I am seeing results as 2019.12.01, but I am missing time (00:00:00:000) - how can I keep along with time. I want final results like 2019.12.01 00:00:00:000
I don't think that there is a built-in format specifier for this. But you could do:
replace(convert(varchar, requestDate, 121), '-', '.')
121 gives you format YYYY-MM-DD HH:MM:SS.SSS. You can then replace each occurence of '-' with ':'.
Note that this assumes that you have a datetime datatype to start with. If you have a string, then no need to convert(), you can just replace().

Convert date to varchar in SQL Server

How do I convert a column which is date type to varchar?
Sample data:
ENDDATE (DATE TYPE)
'1947-12-01 00-00-00'
Requested results:
ENDDATE (VARCHAR)
121947
If I understand the question correctly, you need the ENDDATE of value '1947-12-01 00-00-00' as 121947. You can use the below query
SELECT RIGHT(MONTH(ENDDATE)*1010000+YEAR(ENDDATE),6)
If you are working with 2012 version or higher, you can use format. For earlier versions you can use convert with some string manipulations:
DECLARE #D as date = '1947-12-01'
SELECT REPLACE(RIGHT(CONVERT(char(10), #d, 103), 7), '/', '') As charValue2008,
FORMAT(#d, 'MMyyyy') as charValue2012
Results:
charValue2008 charValue2012
121947 121947
Please note that Format runs relativley slow, so if you have a lot of rows you might want to choose another way to do that.

change the date format in sql server

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')

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 to convert a date 15-May-2019 in SQL to 2019/05/15 using PATINDEX

I need to convert a date in SQL. The date as is 15-May-2019 and it should display as 2019/05/15.
This is the code I have so far
CASE WHEN WHEN LEN(AgeGroup) > 1 THEN PATINDEX ('%[A-Z]%', date)
I'm not completely sure how to use Patindex. Can someone please help me to fix this?
I would recommend just converting to the date data type:
select try_convert(date, '15-May-2019')
If you want it with slashes, you can produce a string instead:
select replace(convert(varchar(10), try_convert(date, '15-May-2019'), 120), '-', '/')
Use the below code and you can refer the link for other formats.https://www.mssqltips.com/sqlservertip/1145/date-and-time-conversions-using-sql-server/
SELECT CONVERT(varchar,(cast('15-may-2019' as date)), 111)
PATINDEX is slightly overkilling.
Try:
SELECT FORMAT(
TRY_PARSE('15-May-2019' AS DATE USING 'en-US')
, 'yyyy/MM/dd')
TRY_PARSE parses the string to DATE using English culture.
FORMAT is to precisely control the output