How to convert YYYY-MM-DD HH:MM:SS TO MM-DD-YYYY HH:MM:SS IN SQL Server? - sql

How to convert yyyy-mm-dd hh:mm:ss (in 24 hours format) to dd-mm-yyyy hh:mm:ss (in 24 hours format? I am using Sql Server 2008.
Given Date Format: 2017-12-18 18:16:49 - Its in DateTime format
Required Date Format: 18-12-2017 18:16:49

This Would work in Older SQL Server Versions Also, Converted to datetime first if it's VARCHAR otherwise you can skip that conversion.
SELECT convert(varchar,convert(datetime,'2017-12-18 18:16:49'),105) + ' ' +
convert(varchar(8),convert(datetime,'2017-12-18 18:16:49'),14);
Use this Link as Reference for Date & Time conversion Formats

Try this
SELECT FORMAT(CAST('2017-12-18 18:16:49' AS datetime) , 'dd-MM-yyyy HH:mm:ss')

DECLARE #date DATETIME = '2017-12-18 18:16:49'
SELECT FORMAT(#date,'dd-MM-yyyy HH:mm:ss')

I would advice not change directly in SQL formate, instead you could change your php code where query fetch the date data
for example you could assign your $yourDate object to new dateTime, and use formate function to define the date format:
<?php
$yourDate = new DateTime();
$timestring = $yourDate->format('m-d-Y h:i:s');
echo $timestring;
the output will be: 05-18-2018 05:00:34
You could take reference in this older discussion
This will prevent some bug error might be occurred if your data table has some other date format relative to, and also it is more useful once you need to have change the date format again
Hope this will be help

Related

SQL How to format M/DD/YYYY (1/31/1960) to DD/MM/YYYY (31/01/1969)

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.

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.

Convert 2017-01-13T06:00:00Z time format to yyyy-mm-dd hh:mm:ss

I'm trying to convert time formats. The database table has time stored in a VARCHAR(50) column in the format of "yyyy-mm-ddThh:mi:ssZ". When running a query, I want the time formatted as "yyyy-mm-dd hh:mi:ss". (I don't care about time zone adjustment.) I don't understand why GETDATE with the style 120 returns what I want, but when I use the field name it does nothing.
SELECT
PlateEffectiveFrom as Value_In_Table,
STUFF(CONVERT(VARCHAR(33),PlateEffectiveFrom, 120),20,4,'') as 'Gets_Rid_of_Z',
CONVERT(varchar(33), PlateEffectiveFrom, 120) AS Does_Nothing,
CONVERT(varchar(33), GETDATE(), 120) AS Format_I_Want
FROM dbo.TVLTagDetails13
WHERE ID = 5
Returns:
Value_In_Table Gets_Rid_of_Z Does_Nothing Format_I_Want
2008-03-12T06:00:00Z 2008-03-12T06:00:00 2008-03-12T06:00:00Z 2019-01-08 11:16:41
The solution to this problem would be to change the data type of the column to DateTime2 if you don't need the time zone data, or DateTimeOffset if you need the time zone data.
Assuming this can't be done, what you do is a simple string manipulation to get from "yyyy-mm-ddThh:mi:ssZ" to "yyyy-mm-dd hh:mi:ss":
SELECT LEFT(REPLACE(PlateEffectiveFrom, 'T', ' '), 19) AS Format_I_Want
FROM dbo.TVLTagDetails13
WHERE ID = 5
Also, following the conversation in the comments - a must read: Aaron Bertrand's Bad habits to kick : choosing the wrong data type

SQL Table datetime column format

I tried googling to get an answer but in vain. Below is my requirement
User has an option to insert data into a table which has export_date as datetime
When they execute insert statements, I want to ensure that they have keyed in date in "dd-MM-yyyy hh:mm:ss" format. If not, don't allow insert queries to run.
Or allow the user to enter date in any format like dd-MM-yyyy or dd/MM/yyyy but internally convert it into the required format "dd-MM-yyyy hh:mm:ss" and store
Can someone help/guide me?
You can use Set DateFormat
Example
Used data type of date for illustration, but clearly you can use datetime
Set DateFormat DMY
Select try_convert(date,'15/08/2017') -- Returns 2017-08-15
Set DateFormat MDY
Select try_convert(date,'15/08/2017') -- Returns NULL
Set DateFormat YMD
Select try_convert(date,'15/08/2017') -- Returns NULL
You will likely run into issues if you want the user to input the date in the "dd-MM-YYYY" format since if the user inputs in the mm-dd-yyyy format, you'll get different results. "YYYMMDD" is a generic format that SQL Server will always interpret properly.
Once you get the date from the user, you can convert it using the particular format that you want. The following will convert the date to the ISO8601 format:
SELECT
GETDATE() AS UnconvertedDateTime,
CONVERT(nvarchar(30), GETDATE(), 126) AS UsingConvertTo_ISO8601 ;
GO
For more information on the specific date formats, I'd recommend checking out Microsoft's Convert Functions.

datetime conversion issue in sql server

if i try to cast string date to datetime like
select cast('12/01/2010' as datetime) then
it works
but if i try to cast like
select cast('22/01/2010' as datetime) then it is giving error.
again if i try to cast string date to datetime like
select cast('2010/12/01' as datetime) then it works
but if i try to cast like
select cast('2010/25/01' as datetime) then it is giving error. my requirement is whatever way user input date that should be successfully converted to datetime. please tell me best solution
In your case the sql server assumes, that date format id mm/dd/yyyy - US format,
you want to use French format, so use convert
select CONVERT(DATETIME, '12/01/2010', 103)
instead you'll get an error because there in no such a month number - 22
The best solution when passing datetime as string - to use short(without timezone) ISO format:
yyyyMMdd HH:mm:ss.ffff
I recommend you to use fixed '20111231' format. You do not need to use cast, convert or similar command.