I have converted DB2 dates in SQL from yyyymmdd numeric string using substr to get mm/dd/yyyy which is the format we want. Problem is still not recognized as a 'true' date to do calculations on such as datedif.
How do I change the mm/dd/yyyy new format to a true date field? Have I made this more complicated then it has to be?
Try this....
select convert(datetime,yyyymmdd )
or
select Cast(yyyymmdd as datetime)
to convert a mm/dd/yyyy string to a true date use:
convert(datetime, your_nonDB2_date_string_here ,101)
more...
"Have I made this more complicated then it has to be?" Yes.
Don't store dates as strings, it's really that simple.
I have to assume "SQL" in your question means Microsoft SQL Server (MS do not own "SQL" that I'm aware of). So if you are getting strings from DB2 in YYYYMMDD format this happens to be the safest format for conversion to "real dates" in SQL Server.
So, when reading into a SQL Sever table do this: (112 is a "style" for YYYYMMDD)
convert(datetime, your_DB2_date_string_here ,112)
where you see "datetime" above note you can use different types in SQL Server:
date --<< date only (no time)
smalldatetime
datetime
datetime2 --<< highest precision
For OUTPUT of a "real date field" you can use the convert function again, or if you have SQL 2012 or later you can use the format() function
for output as MM/DD/YYYY use
convert(varchar,real_date_field,101)
format(real_date_field,'MM/dd/yyyy') --<< nb case sensitive: MM not mm
see:
http://msdn.microsoft.com/en-us/library/ms187928(v=sql.100).aspx
http://www.sql-server-helper.com/sql-server-2012/format-function-vs-convert-function.aspx
Related
I have a column that has date and time mixed together i.e. 1/31/1960 12:00:00AM and I would like to convert them into two-column through SQL function:
(a) DD/MM/YYYY i.e 31/01/1969
(b) HH:MM i.e 12:00
Thanks in advance.
Take a look at the CONVERT() function, specifically the style for datetime. Additionally, we can CAST() the DATETIME as a TIME data type to extract the time.
Assuming your original column is a DATETIME data type, you can run
SELECT
CONVERT(NVARCHAR(24),{DateField},103)
,CAST({DateField} AS TIME)
If it is string, you can cast it then convert
SELECT
CONVERT(NVARCHAR(24),CAST('1960-01-31' AS DATETIME),103) AS ReportingDate
,CAST(CAST('1960-01-31' AS DATETIME) AS TIME) AS ReportingTime
If you're on a sufficiently recent version of SQL Server, the FORMAT() function is your friend.
DECLARE #d DATE = GETDATE();
SELECT FORMAT(#d, 'dd/MM/yyyy');
Note, that second argument is a .NET formatting string, so you should be able to use whatever is available from the Framework.
If I run the query:
select startdate, count(*)
from tablename
where startdate > '2020-04-06'
It only returns value where the startdate is after 4th June 2020. However the dates in the table are in the format YYYY-MM-DD HH:mm:ss.sss.
If I run a getdate() or sysdatetime() it returns 2020-06-16 14:29:29.157 in the correct format.
So why is the query using YYYY-DD-MM? And how do I get it to change by default?
P.S. I'm aware that I could use CONVERT or FORMAT in the query, but as all dates will be in the YYYY-MM-DD format I'd like that to be the default, and not have to write extra code each time.
EDIT: I'm using Microsoft SQL Server Management Studio
EDIT2: I checked with a colleague and the same thing happens to them.
That depends on various settings. You can get around this by removing the hyphens:
startdate > '20200406'
In SQL Server, this format is always unambiguous, YYYYMMDD. I prefer the version with the hyphens, because it is more standard. But if you are dealing with this as an issue I would suggest using the SQL Server unambiguous format.
You can handle it in two ways:
At the session level. you can set format and issue query
Use ISO 8601 format (Recommended)
DECLARE #table table(a datetime)
INSERT INTO #table values('2020-04-06')
SELECT * FROM #table WHERE A = '2020-04-06' -- ISO 8601
set dateformat ymd
SELECT * FROM #table WHERE A = '2020-04-06' -- Format change
I want to convert the column (EXECUTION_LOCAL_DATE_TIME) which has datetime format as (YYYY-MM-DD hh:mm:ss.nnnnnnn) to format (YYYY-MM-DD). Ho do i get this. I am working on SQL server management studio
If your intention is to just get the DATE part of a DATETIME then you can just convert the format to DATE (note that this will return a 'Date' datatype, not specifically formatted to a string 'YYYY-MM-DD'.)
eg:
DECLARE #Dt DATETIME = '2019-01-25T12:00:00'
SELECT CONVERT(DATE, #Dt)
Will return '2019-01-25'
You want the CAST() function:
https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-2017
Ideally you should return the datetime format from your query and let your presentation layer handle formatting.
However if you are using SQL server 2012 or higher then you can use the Format() function. See the below answer:
Convert Date format into DD/MMM/YYYY format in SQL Server
If you're using SQL Server 2012 or higher you can use the FORMAT() function.
In your case you'd need
SELECT FORMAT(EXECUTION_LOCAL_DATE_TIME, 'yyyy-MM-dd') FROM TABLE_NAME
You can find additional info here
https://www.mssqltips.com/sqlservertip/2655/format-sql-server-dates-with-format-function/
https://learn.microsoft.com/en-us/sql/t-sql/functions/format-transact-sql?view=sql-server-2017
Here Is the Code Worked For me
SELECT convert(varchar, EXECUTION_LOCAL_DATE_TIME, 111) from Tablename
The Execution_Local_Date_Time will be Converted to yyyy/mm/dd format.
I am bit confusing here?
declare #date1 datetime = '2016-01-21 14:10:47.183'
I want to convert '2016-01-21 14:10:47.183' To '21-01-2016'
when I tried: select convert(date,#date1,105)
I am getting: 2016-01-21
But with: select convert(varchar(10),#date1,105)
I am getting: 21-01-2016
Why I am not having same results with above code?
Why should I convert to varchar?
Thanks in advance
This is just presentation matter and should be done in application layer. If you cannot do it in application you could use FORMAT (SQL Server 2012+):
declare #date1 datetime = '2016-01-21 14:10:47.183'
SELECT FORMAT(#date1, 'dd-mm-yyyy');
LiveDemo
Why I am not having same results with above code?
select convert(date,#date1,105)
-- DATETIME -> DATE
-- vs
select convert(varchar(10),#date1,105)
-- DATETIME -> VARCHAR(10) using specific style
If you only to skip time part use SELECT CAST(#date1 AS DATE) and do not bother how it is presented. It is still DATE.
To sum up: in SQL query use DATE as date, in application display it with desired format.
The reason why is because once you put a value in a datetime column (or date or any of the other variations on date-time datatypes) in SQL Server. SQL Server ceases to think of that date as having any particular format. It translates it into numbers, and stores it that way internally.
So when you select a date from a date time column, SQL Server displays it in the default format that you have selected based on your environment/local settings.
If you want to display it in any other format, you have to first convert it to a string, because as far as SQL Server is concerned, dates don't have formats. They are just numbers. The 21st day of March is the 21st day of March, whether you write it as 3/21 or 21/3.
So when you try to convert a date to a date with a different format, SQL Server just ignores you because dates don't have formats. However, if you want to convert that date to a string, SQL Server will be happy to help you display that string in any format you like.
Hope this helps, but sounds like some further research into how SQL Server stores dates would help your understanding.
I use Classic ASP and SQL Server 2012. I have a program that inputs into the database using now(). Originally it was formnatdatetime(now(),2).
For the majority of time everything was fine but for some reason (which is why I'm asking) occasionally it would put the date in the database in the wrong format. So instead of ddmmyyyy it would be mmddyyyy.
I cannot see how or why when the code is the same, the database is the same. I assume now() or getdate() in TSQL is server specific.
You could use the format function
SELECT FORMAT(GETDATE(),'ddMMyyyy')
Results:
11042015
MS SQL Server 2012 does not know NOW() function! As Gordon Linoff mentioned, you need to use GETDATE() instead!
I'd suggest to read this: Date and Time Data Types and Functions (Transact-SQL) together with: CAST and CONVERT (Transact-SQL)
SELECT CONVERT(VARCHAR(30), GETDATE(), 103) AS CustomDateFormat
-- returns: dd/mm/yyyy
Note that sometimes, sql server can't convert date stored as a string, so you need to use SET DATEFORMAT, especially when sql server uses different date format:
SET DATEFORMAT dmy;
SELECT CONVERT(VARCHAR(30), '11/04/2015', 121) AS NewDateTime