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

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().

Related

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

Convert to DATE T-SQL

I have a field with date in the format dd.mm.yyyy E.x. 29.05.2016. I want to SELECT it as DATE but I get an error when I try the following:
CAST([PublishedDate] AS DATE) AS PublishedOn
Conversion failed when converting date and/or time from character string.
Reading this I tried the following:
CONVERT(VARCHAR(10), [PublishedDate], 126) AS PublishedDate
But it doesn't change the format.
How do I SELECT to have it in YYYY-MM-DD format.
EDIT: I have 3 different date fields with all three having different formats: dd.mm.yyyy | yyyy-mm-dd | yyyy/mm/dd. I want to select all of them with the same format. Since I use this query to build reports, right now I have dates in different formats. Doing the following:
CONVERT(VARCHAR(10), [PublishedDate], 104) AS PublishedOn
CONVERT(VARCHAR(10), [ValidFromDate], 104) AS ValidFrom
Gives me the following
You should use the right format, in your case it would be:
CONVERT(date, [PublishedDate], 104) AS PublishedDate
Also, once it's in a date, datetime or other date datatype, it doesn't have a format at all.
edit: Once you have your values in a date datatype, of course you can recast to a varchar to get the visual representation of the date you need.
edit2: If you want a date datatype, you should convert to date: CONVERT(DATE, [your column], [your format]).
If you want a nvarchar datatype, you should convert to nvarchar: CONVERT(nvarchar(x), [your column], [your format]).
You have an nvarchar that you want to display in a certain format, so you should first convert to date, then back to varchar (I doubt you need unicode):
CONVERT(VARCHAR(10), CONVERT(date, [PublishedDate], 104), 126)
The 104 you have to change for columns that are currently in a different format.
The best solution by far, is to change the datatypes to date. That is a bit of work, but definitely worthwhile.
If you really want the string do like this:
CONVERT(char(10), CONVERT(date,[date], 104),126) AS PublishedDate
Convert the string to date, using the 104 format (dd.mm.yyyy), as it is your original format, then convert the date into string, using the 126 format (yyy-mm-dd)
If you have a newer version of SQL Server, you may try
SELECT COALESCE(TRY_CAST(adate AS DATE), TRY_convert(DATE, adate, 126), TRY_convert(DATE, adate, 104)), format
FROM (
VALUES ('26.04.2017', 'dkformat') ,
('01.01.2017', 'EU format'),
('12.12.12', 'unknown format')
) a(aDate, format)
But you are going down a dangerous path, when you are trying to guess what format your source is in. Been there, done that, had the t-shirt.
I still hate the 0.1% conversions that are wrong.
This solution will result in NULLs, so you can see where your conversion did not succeed.

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.

SQL - Unable to Convert a string in to YYYYMMDD format

I have a table that one of its columns is XML data type.
This XML value includes a TimePeriod value that looks like this:
"{start:{align:'Start',date:'2011-05-20T00:00:00'}"
I managed to extract only the date from this string with the following query:
SELECT SUBSTRING(configuration.value('(/ActiveService/#TimePeriod)[1]','nvarchar(50)'),
CHARINDEX('''', configuration.value('(/ActiveService/#TimePeriod)[1]','nvarchar(50)'),28)+1,10 )
FROM [MySystem].[dbo].[MyServices]
so i got this as the result: 2013-11-01
But, now i need to convert it to this format: YYYYMMDD
And when I'm using CONVERT to format 112 I Still get the same result: 2013-11-01
Why?
It you took value from xml as varchar, you cannot convert it into format YYYYMMDD (because it's already varchar), you have to convert it to date (or datetime first):
select convert(varchar(8), convert(datetime,
substring(
#data.value('(/ActiveService/#TimePeriod)[1]','nvarchar(50)'), 29, 10
), 120), 112)
But in your case it could be easier just remove -, by replace(<your value>, '-', '').
sql fiddle demo

Convert/format a column in a SQL Server 2005 select statement

I have an SQL query which is select DateOfBirth from people, and it shows up in the result pane as
DateOfBirth
07/07/2010 5:08:02
07/09/2010 5:08:02
07/13/2010 5:08:02
I want to format as,
07/Jul/2010
09/Jul/2010
13/Jul/2010
NOTE: DateOfBirth column has datatype nvarchar(50), not datetime...
This is a little tricky as the best way to do this is to take the varchar convert it into a datetime and then format it. Annother complication is that the format you want is not a format that SQLServer will output.
So.
SELECT CONVERT(DateTime, DateOfBirth) from people
will get you the date time and we can then convert it to a string format as follows
SELECT CONVERT(DateTime, DateOfBirth), 106) from people
this will produce the string output 'dd Mon YYYY'
then its just a matter of replacing the spaces with '/'
SELECT REPLACE(CONVERT(varchar, CONVERT(DateTime, DateOfBirth), 106), ' ','/') FROM people
will get you the format you want.