SQL Server dates - sql

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

Related

SQL query is using YYYY-DD-MM format, when table is YYYY-MM-DD

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

How to get datepart from datetime in a Column

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.

How to get date from sql server in user specified format

I want get date from sql server in user specified format using GETDATE() function.
if i give this query
select GETDATE()
then it is displating output date in this format
2015-03-17 07:29:58.377
but i want output like this .
2015-03-17
what statement should be added with query to get the result.
help me from this problem.
Just use convert():
select convert(varchar(10), getdate(), 121)
Just have a look to the following link. which provides more conversion options.
https://msdn.microsoft.com/en-us/library/ms187928.aspx
In SQL Server 2012 onward you may use FORMAT() which is a bit more intuitive than recalling style numbers.
e.g.
select FORMAT( GETDATE() , 'yyyy-MM-dd' )
see: https://msdn.microsoft.com/en-AU/library/hh213505.aspx
however do note the second parameter is case sensitive
'yyyy-MM-dd' works
'YYYY-MM-DD' would only work for the MM

MS SQL - DB2 Tables

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

Current System Time - SQL

How to get the current system time only in SQL Server / any database.
The date is not required...only the current time is required.
You can use getdate() or current_timestamp
This will give you both date and time but then you can format it however you need.
If you are using SQL Server 2008+ you can even format the result as time:
select cast(getdate() as time)
If you are not in SQL Server 2008+, then you can use many different methods to get the time only including:
SELECT convert(char(8), getdate(), 108)
See SQL fiddle demo with several versions
Using `getdate()` like:
select getdate()
=============================
and get only time use convert function with 108 code like :
select convert(varchar,GETDATE(),108)
select CONVERT(VARCHAR(8),GETDATE(),108) CurrTime;
There is 3 different statement available to get the current system time
select CONVERT(VARCHAR(8),getDate(),108)
select CONVERT(VARCHAR(8),{fn NOW()},108)
select CONVERT(VARCHAR(8),CURRENT_TIMESTAMP,108)
They all produce the same results and performance are the same, therefore it is really whatever one of the three you prefer the look of.