Convert datetime to specific format? - sql

I want to convert a datetime to a specific format. The conversion should result in a variable of type datetime and not char or varchar. How do I do this in SQL server 2000, 2005 and 2008 ?
select CONVERT(varchar(30),getdate(),120)
I tried this, but it gives me a string. I want a datetime without the milli-seconds. SS 2012 has an option for this, but not previous versions.
http://blog.sqlauthority.com/2012/11/21/sql-server-display-datetime-in-specific-format-sql-in-sixty-seconds-033-video/

You can't have it both ways ... a variable of type "datetime" is defined as:
Defines a date that is combined with a time of day
with fractional seconds that is based on a 24-hour clock
You can CONVERT and DISPLAY in whatever format you choose, but the datetime data type will always have the milliseconds, even if you set them to zero.

You can remove the milliseconds from the datetime like this:
DATEADD(ms, -DATEPART(ms, date), date) > '2013-11-18 03:21:52'
Also check SQL Server Date Formats
or may be try like this to remove the millisecond part:-
declare #str datetime
set #str = '2013-11-18 17:24:05.784'
select convert(datetime, convert(char(19), #str, 126))

Related

SQL Round Seconds on Column to 00 seconds [duplicate]

I'm having a bit of trouble with truncating data. I'm using SQL's GETDATE() function to get the current date and time and enter them into a database. However, I only want to save the date and time up until the minute. In other words, I want dd/mm/yyyy hh:mm:00.000 or dd/mm/yyyy hh:mm to be saved when I input new data. How can I go about doing this?
I should note I'm using MS-SQL.
There are a number of ways to go about doing this.
For example, you could convert the generated datetime from GetDate() to a smalldatetime first, à la:
CAST(GetDate() AS smalldatetime)
To be clear, this will round the generated seconds up (or down) to the nearest minute depending up the value of the current second.
EDIT:
Alternatively, you can have SQL Server truncate a datetime for you for a "cleaner" (READ: no rounding, since the value is pre-truncated) conversion to smalldatetime:
CAST(DateAdd(minute, DateDiff(minute, 0, GetDate()), 0) AS smalldatetime)
For truncation:
SELECT SMALLDATETIMEFROMPARTS(
datepart(year ,dt)
,datepart(month ,dt)
,datepart(day ,dt)
,datepart(hour ,dt)
,datepart(minute,dt)
)
FROM (SELECT GETDATE()) t(dt)
One way is to convert it to smalldatetime for the assignment (and back as needed).
smalldatetime always has seconds and beyond set to 00.
SELECT CONVERT(smalldatetime, GETDATE())
As this may round up or down, another way to safely truncate the seconds would be this:
SELECT CONVERT(datetime, CONVERT(nchar(16), GETDATE(), 120), 120)
The conversion code 120 returns the format yyyy-mm-dd hh:mi:ss.
Combine DATEADD and SMALLDATETIME to truncate
CAST(DATEADD(S, -30, dt) AS SMALLDATETIME)
The other option is not sure why you cannot consider the front-end instead of the back-end so don't change the SQL and thus format in the front-end as dd/MM/yyyy HH:mm or whatever format you need if that is doable or appropriate in the context of what you are trying to achieve. For example in an SSRS report you could format the field in question in the report designer or use the Format function. If it is a webpage or Excel I am sure you could also do something similar.

Dutch varchar date issue in SQL server 13 month

I have the following datetime format ( as varchar ) in my database 13-04-2018 1:05:00.
I need to convert it to the following format: 2018-04-13 01:05:00. As datetime.
Normal convert functions can't do this because they try to take the 13th month, and that month doesn't exist. This error:
The conversion of a varchar data type to a datetime data type resulted
in an out-of-range value.
Does someone know how to convert this date issue?
Using datetimes is always a pain regardless of the language because of all the different formats across the world.
To sort your issue out currently, you need to use a format style which is a third parameter to CONVERT. Personally what I would suggest here is to store as a datetime, because storing datetimes as strings is never a good idea. It just gets too messy later on, but if saved in the format you would like, it would be saved as yyyy-MM-dd hh:mm:ss
SELECT CONVERT(DATETIME, '13-04-2018 1:05:00',103)
You can create your own function to format it in your desired output string.
CREATE FUNCTION FormatMyDate
(#Date DATETIME) RETURNS VARCHAR(20)
AS
BEGIN
RETURN FORMAT(#Date,'yyyy-dd-MM hh:mm:ss')
END
And then you can call it in SELECT statements like this:
SELECT dbo.FormatMyDate(yourDateCol)
FROM yourTable
this takes the date from the format where month comes before day and reverses the 2 values (month and day)
SELECT CONVERT(DATETIME, '2018-13-04 01:05:00', 103);
Results:
2018-04-13 01:05:00.000
This should work for you requirement...
SELECT FORMAT(yourdate, 'yyyy-dd-MM')
Your Solution Bro...
DECLARE #d DATETIME = GETDATE()
SELECT FORMAT ( #d, 'MM-dd-yyyy hh:mm:ss', 'de-de' ) AS 'Hoping your result'

Convert format of date

I have a date format like
'2003-11-27 00:00:00.000'
How can I convert it into YYYYMMDD like 20031127 WITH SQL SERVER ?
To convert the date as per ISO standard you can write as:
SELECT CONVERT (VARCHAR(8), GETDATE(),112) as [YYYYMMDD]
SELECT CONVERT (VARCHAR(8), cast('2003-11-27 00:00:00.000' as datetime), 112)
It depends of how your date is declared.
DECLARE #date datetime = '2003-11-27T00:00:00.000' -- datetime
DECLARE #date2 char(23) = '2003-11-27 00:00:00.000' -- char(23)
SELECT
convert(char(8), #date, 112) datetimeconvert,
convert(char(8), convert(datetime, #date2, 121), 112) charconvert
Result:
datetimeconvert charconvert
20031127 20031127
The date types in SQL Server do not have any format, they are binary types. Formats apply only when you convert them to text or try to parse a text literal.
You don't specify what is the value you posted, or what you want to do with it. There are several possibilities:
You want to truncate the time portion of a datetime field. Then just cast(myField as date)
You want to create a text string containing the date portion of a datetime or date field: FORMAT(myField,'yyyyMMdd') or CONVERT(varchar(8),myField,112). FORMAT provides almost as much flexibility as .NET's String.Format but in this case CONVERT it's enough.
You want to convert one text literal to another: FORMAT(CONVERT(date,'2003-11-27 00:00:00.000',121),'yyyyMMdd'). You convert the original string to a date then format it as you wish.

Truncate seconds and milliseconds in SQL

I'm having a bit of trouble with truncating data. I'm using SQL's GETDATE() function to get the current date and time and enter them into a database. However, I only want to save the date and time up until the minute. In other words, I want dd/mm/yyyy hh:mm:00.000 or dd/mm/yyyy hh:mm to be saved when I input new data. How can I go about doing this?
I should note I'm using MS-SQL.
There are a number of ways to go about doing this.
For example, you could convert the generated datetime from GetDate() to a smalldatetime first, à la:
CAST(GetDate() AS smalldatetime)
To be clear, this will round the generated seconds up (or down) to the nearest minute depending up the value of the current second.
EDIT:
Alternatively, you can have SQL Server truncate a datetime for you for a "cleaner" (READ: no rounding, since the value is pre-truncated) conversion to smalldatetime:
CAST(DateAdd(minute, DateDiff(minute, 0, GetDate()), 0) AS smalldatetime)
For truncation:
SELECT SMALLDATETIMEFROMPARTS(
datepart(year ,dt)
,datepart(month ,dt)
,datepart(day ,dt)
,datepart(hour ,dt)
,datepart(minute,dt)
)
FROM (SELECT GETDATE()) t(dt)
One way is to convert it to smalldatetime for the assignment (and back as needed).
smalldatetime always has seconds and beyond set to 00.
SELECT CONVERT(smalldatetime, GETDATE())
As this may round up or down, another way to safely truncate the seconds would be this:
SELECT CONVERT(datetime, CONVERT(nchar(16), GETDATE(), 120), 120)
The conversion code 120 returns the format yyyy-mm-dd hh:mi:ss.
Combine DATEADD and SMALLDATETIME to truncate
CAST(DATEADD(S, -30, dt) AS SMALLDATETIME)
The other option is not sure why you cannot consider the front-end instead of the back-end so don't change the SQL and thus format in the front-end as dd/MM/yyyy HH:mm or whatever format you need if that is doable or appropriate in the context of what you are trying to achieve. For example in an SSRS report you could format the field in question in the report designer or use the Format function. If it is a webpage or Excel I am sure you could also do something similar.

SQL datetime value

I have data formatted "2009-07-17T00:00:00-05:00" in varchar variable. How can I convert this data to datetime field in MS SQL server using query or TSQL?
If you are on SQL Server 2008, you can use the datetimeoffset type:
select cast('2009-07-17T00:00:00-05:00' as datetimeoffset)
Since you are on 2005, datetimeoffset data type is not available for you. You should decide if you want to keep time zone information separately. If you just want the datetime part, just strip the time zone part from the string and cast it as a datetime:
select cast(left('2009-07-17T00:00:00-5:00', 19) as datetime)
Cast as DATETIME won't work, but that format is a valid XML datetime format, so you can route the cast through an XML type first:
declare #d varchar(50);
select #d = '2009-07-17T00:00:00-05:00';
select x.value(N'.', N'datetime') from (select cast(#d as xml) as x) as t;