SQL convert datetime statement - sql

Does anyone know how do I convert the following SQL Statement into ddmmyy without has any delimiter?
convert(varchar(10), '2015-06-01 00:00:00.000', ??)
For example 2015-06-01 (yyyy-mm-dd) want to convert as 010615 (ddmmyy).

You can use Convert function providing it with the style:
SELECT REPLACE(CONVERT(varchar(10), GETDATE(), 5), '-', '')

You could use something like this
SELECT (REPLACE(CONVERT(nchar(8), GETDATE(), 3), '/', ''))

SELECT FORMAT(GETDATE(), 'ddMMyy')
Tested with MS SQL Server 2012. You can use a custom format of your choice.

Related

How to change datetime format in query?

My datetime column is in YYYYMMDDHHmmss format. Datasource of Grafana is SQL Server 2014 with read only access (TRIM() not valid).
How could I filter results in my query using Grafana time filter options?
I thought in using $__timeFrom() and $__timeTo(), but Grafana uses 2022-01-21T06:29:28Z format or unixepoch.
My query needs to convert:
2022-01-21T06:29:28Z -> 20220121062928
[EDIT]
Try 1 (it works):
WHERE s.zeitpunkt
BETWEEN
CAST(REPLACE(REPLACE(REPLACE(REPLACE($__timeFrom(), '-', ''), 'T', ''), ':', ''), 'Z', '') AS VARCHAR(25))
AND CAST(REPLACE(REPLACE(REPLACE(REPLACE($__timeTo(), '-', ''), 'T', ''), ':', ''), 'Z', '') AS VARCHAR(25))
Try 2 (it does not work):
WHERE s.zeitpunkt
BETWEEN CONCAT(
CONVERT(varchar, $__timeFrom(), 112)
, REPLACE(CONVERT(varchar, $__timeFrom(),108),':','')
)
AND CONCAT(
CONVERT(varchar, $__timeTo(), 112)
, REPLACE(CONVERT(varchar, $__timeTo(),108),':','')
)
If you are having trouble with date format of sql server you can do a number of things to change date format. For Example SSMS and SQL Server date format are decided by Your language and region set
You can use CAST(),CONVERT() to change the format of date or set your format from ssms.
You Can follow this link Date and Time Format in SQL
for datetime to datetimewith different formats
set language 'British'
cast(convert(varchar, getdate(), 103) as datetime)
This will change 2022-01-24 00:38:54.840 to 24/01/2022
This is what finally worked:
I Casted the datetime and used FORMAT() with a custom format. The other solutions I tried where a little bit slower.
WHERE s.zeitpunkt
BETWEEN
CAST(
FORMAT(CAST($__timeFrom() AS DATETIME),'yyyyMMddHHmmss')
AS VARCHAR)
AND CAST(
FORMAT(CAST($__timeTo() AS DATETIME),'yyyyMMddHHmmss')
AS VARCHAR)

convert date yyyy-mm-dd to mmm-yy SQL Server 2016

I want to convert date yyyy-mm-dd (stored as a date format) to mmm-yy format.
There are no exact matches in the prior questions on the site.
I have tried substrings and convert function, was considering creating a scalar function but its taking me a while and hoping someone has an easy solution.
You can construct the format using string operations:
select left(datename(month, datecol), 3) + '-' + right(datename(year, datecol), 2)
Or using format():
select format(datecol, 'MMM-yy')
Try this
select replace(right(convert(varchar(9), getdate(), 6), 6), ' ', '') asDate

SQL - Convert varchar to datetime

Any way to convert this varchar -> "18-Jan-2015 12:43:51" to datetime in SQL database?
Thanks.
Just like everybody said above, the best way to do it is either with convert or cast, besides, this is very basic sql
here are both cases:
SELECT CAST('18-Jan-2015 12:43:51' AS DATETIME) AS DATE
SELECT CONVERT (DATETIME, REPLACE('18-Jan-2015 12:43:51', '-', ' '))
Use convert with replace:
Select convert(datetime, replace('18-Jan-2015 12:43:51', '-', ' '), 113)
Try this:
SELECT (CONVERT(DATETIME,LEFT('18-Jan-2015 12:43:51',23),101))
You can use statement like below
Select convert(datetime, replace('18-Jan-2015 12:43:51', '-', ' '))
SELECT CAST('18-Jan-2015 12:43:51' AS DATETIME)

What is the best way to write the date in mm-mm-yyyy in SQL

I'd like to return a date from SQL in the following format 2011-12-DEC, 2012-01-JAN, 2012-02-FEB, 2012-03-MAR etc...
Currently I have the below code, but don't think it's the best. It also does not return a 0 in front of the month (i.e. 8 rather than 08)
print CONVERT(VARCHAR(20), DATEPART(yyyy,GETDATE())) + '-'+ CONVERT(VARCHAR(20), DATEPART(mm,GETDATE()))
For SQL Server, something like this
select convert(varchar(8), getdate(),120)
+ convert(varchar(3),datename(month,getdate()))
For Oracle use TO_CHAR and date formatting. For Example
SELECT TO_CHAR(SYSDATE, 'YYYY-DD-MON') FROM DUAL
There is a wide range of possible date format strings
See here.
If you really do want yyyy-mm-mmm then you could get it like so (for SQL server):
select substring(convert(nvarchar, getdate(), 120), 1, 7)
+ '-' +
substring(upper(datename(mm, getdate())), 1, 3)
Consider a DATE_FORMAT function or the like, depending on your RDMS.
Well,
you should use a formatter (eg AS [DD/MM/YYYY] ).
All of them are listed here including your requirement for "month" naming
http://www.sql-server-helper.com/tips/date-formats.aspx
SELECT SUBSTRING(CONVERT(VARCHAR(11), GETDATE(), 113), 4, 8) AS [Mon-YYYY]
would give you Apr-2006
just turn it around as you like.

How to convert a date 15-May-2019 in SQL to 2019/05/15 using PATINDEX

I need to convert a date in SQL. The date as is 15-May-2019 and it should display as 2019/05/15.
This is the code I have so far
CASE WHEN WHEN LEN(AgeGroup) > 1 THEN PATINDEX ('%[A-Z]%', date)
I'm not completely sure how to use Patindex. Can someone please help me to fix this?
I would recommend just converting to the date data type:
select try_convert(date, '15-May-2019')
If you want it with slashes, you can produce a string instead:
select replace(convert(varchar(10), try_convert(date, '15-May-2019'), 120), '-', '/')
Use the below code and you can refer the link for other formats.https://www.mssqltips.com/sqlservertip/1145/date-and-time-conversions-using-sql-server/
SELECT CONVERT(varchar,(cast('15-may-2019' as date)), 111)
PATINDEX is slightly overkilling.
Try:
SELECT FORMAT(
TRY_PARSE('15-May-2019' AS DATE USING 'en-US')
, 'yyyy/MM/dd')
TRY_PARSE parses the string to DATE using English culture.
FORMAT is to precisely control the output