I need to convert a value which is in a DateTime variable into a varchar variable formatted as yyyy-mm-dd format (without time part).
How do I do that?
I need to convert a value which is in a DateTime variable into a varchar variable formatted as yyyy-mm-dd format (without time part).
How do I do that?
With Microsoft Sql Server:
--
-- Create test case
--
DECLARE @myDateTime DATETIME
SET @myDateTime = '2008-05-03'
--
-- Convert string
--
SELECT LEFT(CONVERT(VARCHAR, @myDateTime, 120), 10)
Try the following:
CONVERT(varchar(10), [MyDateTimecolumn], 20)
For a full date time and not just date do:
CONVERT(varchar(23), [MyDateTimecolumn], 121)
See this page for convert styles:
http://msdn.microsoft.com/en-us/library/ms187928.aspx
OR
SQL Server CONVERT() Function
Either Cast or Convert:
Syntax for CAST:
CAST ( expression AS data_type [ (length ) ])
Syntax for CONVERT:
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )
http://msdn.microsoft.com/en-us/library/ms187928.aspx
Actually since you asked for a specific format:
REPLACE(CONVERT(varchar(10), Date, 102), '.', '-')
For SQL Server 2008+ You can use CONVERT and FORMAT together.
For example, for European style (e.g. Germany) timestamp:
CONVERT(VARCHAR, FORMAT(GETDATE(), 'dd.MM.yyyy HH:mm:ss', 'de-DE'))
Try the following:
CONVERT(VARCHAR(10),GetDate(),102)
Then you would need to replace the "." with "-".
Here is a site that helps http://www.mssqltips.com/tip.asp?tip=1145
declare @dt datetime
set @dt = getdate()
select convert(char(10),@dt,120)
I have fixed data length of char(10) as you want a specific string format.
You did not say which database, but with mysql here is an easy way to get a date from a timestamp (and the varchar type conversion should happen automatically):
mysql> select date(now());
+-------------+
| date(now()) |
+-------------+
| 2008-09-16 |
+-------------+
1 row in set (0.00 sec)
DECLARE @DateTime DATETIME
SET @DateTime = '2018-11-23 10:03:23'
SELECT CONVERT(VARCHAR(100),@DateTime,121 )
select REPLACE(CONVERT(VARCHAR, FORMAT(GETDATE(), N'dd/MM/yyyy hh:mm:ss tt')),'.', '/')
will give 05/05/2020 10:41:05 AM as a result
Write a function
CREATE FUNCTION dbo.TO_SAP_DATETIME(@input datetime)
RETURNS VARCHAR(14)
AS BEGIN
DECLARE @ret VARCHAR(14)
SET @ret = COALESCE(SUBSTRING(REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(26), @input, 25),'-',''),' ',''),':',''),1,14),'00000000000000');
RETURN @ret
END
Simple use "Convert" and then use "Format" to get your desire date format
DECLARE @myDateTime DATETIME
SET @myDateTime = '2008-05-03'
SELECT FORMAT(CONVERT(date, @myDateTime ),'yyyy-MM-dd')
You don't say what language but I am assuming C#/.NET because it has a native DateTime data type. In that case just convert it using the ToString method and use a format specifier such as:
DateTime d = DateTime.Today;
string result = d.ToString("yyyy-MM-dd");
However, I would caution against using this in a database query or concatenated into a SQL statement. Databases require a specific formatting string to be used. You are better off zeroing out the time part and using the DateTime as a SQL parameter if that is what you are trying to accomplish.