How to convert Date in sql 05/04/207 as 4MAY2017 - sql

I am using
CONVERT(nvarchar, Date, 106)
it gives 04MAY2017
but i need 4MAY2017 FOR 04/05/2017
and 11MAY2017 FOR 11/05/2017

If CONVERT(nvarchar, Date, 106) gives 04MAY2017 and you if you want to remove the first character if it is 0 then you can use a CASE expression.
Query
SELECT CASE WHEN LEFT(CONVERT(nvarchar, Date, 106), 1) = '0'
THEN RIGHT(CONVERT(nvarchar, Date, 106), LEN(CONVERT(nvarchar, Date, 106)) - 1)
ELSE CONVERT(nvarchar, Date, 106) END
FROM your_table_name;

You could check when day-in-month < 10 then remove first character.
SELECT REPLACE(CASE
WHEN datepart(day,dateColumn) < 10
THEN STUFF(convert(nvarchar(20), dateColumn, 106), 1,1,'')
ELSE convert(nvarchar(20), dateColumn, 106)
END, ' ', '') AS dateText
FROM yourTable

Related

Extract date from datetime column - SQL Server Compact

I'm using SQL Server Compact 4.0 version, and although it might seem a simple thing to find in google, the examples I've tried none of them work.
My column signup_date is a DateTime with a value 04-09-2016 09:05:00.
What I've tried so far without success:
SELECT FORMAT(signup_date, 'Y-m-d') AS signup_date;
SELECT CONVERT(signup_date, GETDATE()) AS signup_date
SELECT CAST(data_registo, date) AS signup_date
I found that I could use DATEPART function, but that would force me to concat the values, is this the right path to follow? If so, how do I concat as Y-m-d?
SELECT DATEPART(month, signup_date)
SQL Server Compact has no date type.
If you don't want to see the time, convert the datetime value to a string:
SELECT CONVERT(nvarchar(10), GETDATE(), 120)
(This has been tested and actually works against SQL Server Compact)
you were actually on track with the CAST function just a slight error in the syntax. In the CAST function, there needs to be 'as' i.e CAST(data_registo as date)
SELECT CAST(data_registo as date) AS signup_date;
Most of the answers seek to achieve same thing but the explanation to the codes is not enough
CONVERT(date, Date_Updated, 120)
this code does the conversion with mssql. The first item 'date' is the datatype to return. it could be 'datetime', 'varchar', etc.
The second item 'Date_Updated' is the name of the column to be converted.
the last item '120' is the date style to be returned. There are various styles and the code entered will determine the output. '120' represent YYYY-MM-DD.
Hope this helps
The old fashioned way of doing this in SQL Server might work for your purposes:
select dateadd(day, datediff(day, 0, signup_date), 0)
The datediff() gets the number of days (as an integer) since time 0. The dateadd() adds this number back.
If you don't like 0 as a date, you can put any valid date in its place:
select dateadd(day, datediff(day, '2000-01-01', signup_date), '2000-01-01')
EDIT:
If you simply don't want to see the time, convert the date to a string:
select convert(nvarchar(10), signup_date, 120)
(I recommend the YYYY-MM-DD format, but others are available.)
I have tried this and many other solutions. I wanted a generic solution that would work with any LCID. My solution is a bit of convoluted code, but it works perfectly for me. It's a booking system where I needed to find out who was arriving on a particular date. ArriveDate is the column, d is the DATE I want.
SQL = "SELECT * FROM tablename WHERE dateadd(day, datediff(day, 0,
ArriveDate), 0)=' " & Format(d, "yyyy-MM-dd") & " ' "
This will return only date value in original datetime type. So you can do any comparison using the output
SELECT convert(datetime, CONVERT(nvarchar(10), GETDATE(), 120))
Just saw the Question today, a bit late I know :) but maybe this will help..,
select convert(date,(convert(varchar(20),'04-09-2016 09:05:00')))
select convert(nvarchar, getdate(), 1) = 09/25/19
select convert(nvarchar, getdate(), 2) = 19.09.25
select convert(nvarchar, getdate(), 3) = 25/09/19
select convert(nvarchar, getdate(), 4) = 25.09.19
select convert(nvarchar, getdate(), 5) = 25-09-19
select convert(nvarchar, getdate(), 6) = 25 Sep 19
select convert(nvarchar, getdate(), 7) = Sep 25, 19
select convert(nvarchar, getdate(), 10) = 09-25-19
select convert(nvarchar, getdate(), 11) = 19/09/25
select convert(nvarchar, getdate(), 12) = 190925
select convert(nvarchar, getdate(), 23) = 2019-09-25
select convert(nvarchar, getdate(), 101) = 09/25/2019
select convert(nvarchar, getdate(), 102) = 2019.09.25
select convert(nvarchar, getdate(), 103) = 25/09/2019
select convert(nvarchar, getdate(), 104) = 25.09.2019
select convert(nvarchar, getdate(), 105) = 25-09-2019
select convert(nvarchar, getdate(), 106) = 25 Sep 2019
select convert(nvarchar, getdate(), 107) = Sep 25, 2019
select convert(nvarchar, getdate(), 110) = 09-25-2019
select convert(nvarchar, getdate(), 111) = 2019/09/25
select convert(nvarchar, getdate(), 112) = 20190925
select convert(nvarchar, getdate(), 8) = 13:48:36
select convert(nvarchar, getdate(), 14) = 13:49:48:713
select convert(nvarchar, getdate(), 24) = 13:49:57
select convert(nvarchar, getdate(), 108) = 13:50:07
select convert(nvarchar, getdate(), 114) = 13:50:14:490
select convert(nvarchar, getdate(), 0) = Sep 25 2019 1:50PM
select convert(nvarchar, getdate(), 9) = Sep 25 2019 1:50:31:813PM
select convert(nvarchar, getdate(), 13) = 25 Sep 2019 13:50:39:307
select convert(nvarchar, getdate(), 20) = 2019-09-25 13:50:49
select convert(nvarchar, getdate(), 21) = 2019-09-25 13:50:58.923
select convert(nvarchar, getdate(), 22) = 09/25/19 1:51:07 PM
select convert(nvarchar, getdate(), 25) = 2019-09-25 13:51:14.473
select convert(nvarchar, getdate(), 100) = Sep 25 2019 1:51PM
select convert(nvarchar, getdate(), 109) = Sep 25 2019 1:51:32:227PM
select convert(nvarchar, getdate(), 113) = 25 Sep 2019 13:51:38:740
select convert(nvarchar, getdate(), 120) = 2019-09-25 13:51:50
select convert(nvarchar, getdate(), 121) = 2019-09-25 13:51:57.153
select convert(nvarchar, getdate(), 126) = 2019-09-25T13:52:03.627
Use this, i had the same problem
SELECT CAST(data_registo as date) AS "signup_date"
To get a string value, use CONVERT
select convert(varchar(10), signup_date, 11)
Check here for various formats:
https://msdn.microsoft.com/en-us/library/ms187928.aspx
To get a DATE, and just strip out the time, do this
Select Cast (signup_date as DATE)

sql date format order dd-mm-yyyy

I am using this code to format date with SQL:
convert(VARCHAR, DT, 20)
The output is:
2016-01-01 20:40:12
But how do I get the day first.
I would like to get: dd-mm-yyyy:
01-01-2016 20:40:12
When I look in this table:
http://www.blackwasp.co.uk/sqldatetimeformats.aspx
I do not see this formatting.
Thx!
For dd/mm/yyyy hh:mi:ss
(CONVERT(varchar, DT, 103) + ' ' + CONVERT(varchar, DT, 108)) AS MyEuropeanDate
For dd-mm-yyyy hh:mi:ss
(CONVERT(varchar, DT, 105) + ' ' + CONVERT(varchar, DT, 108)) AS MyEuropeanDate
And to understand it right for the next time, look at the official doc that Gordon wisely linked in the comments :
https://msdn.microsoft.com/en-us/library/ms187928.aspx
Could not find a native tSQL function but you can do something like this:
DECLARE #Date DATETIME = '20160101';
SELECT CASE
WHEN LEN(CAST(DAY(#Date) AS VARCHAR)) = 1 THEN '0'+CAST(DAY(#Date) AS VARCHAR)
ELSE CAST(DAY(#Date) AS VARCHAR)
END
+'-'+
CASE
WHEN LEN(CAST(MONTH(#Date) AS VARCHAR)) = 1 THEN '0'+CAST(MONTH(#Date) AS VARCHAR)
ELSE CAST(MONTH(#Date) AS VARCHAR)
END
+'-'+
CAST(YEAR(#Date) AS VARCHAR)
+' '+
SUBSTRING(CONVERT(VARCHAR, #Date, 114), 1, 8);
OR you can do this:
DECLARE #Date DATETIME = '20160101';
SELECT (REPLACE(CONVERT(varchar, #Date, 103),'/','-') + ' ' + CONVERT(varchar, #Date, 108))
RESULT:

how to get today's date in sql with specific format

I currently have this format in my table:
2015-03-19 10:33:16.983
but I would like to convert it into this format:
3/18/2015 12:00:00 AM.
How can I get that format?
select myDate from myTable
You are looking for the Convert() function.
Something like
SELECT CONVERT(varchar(10),GETDATE(),3) + ' ' + CONVERT(varchar(15),CAST(getdate() AS TIME),100)
You can change 3 as per your local. 3 here will mean it in DD/MM/YYYY format i.e, British and French local.
Copy and paste this and alter as you need:
DECLARE #StartTimestamp datetime
SET #StartTimestamp = CAST((CONVERT(varchar(11), DATEADD(DAY, -1, GETUTCDATE()), 106)) AS datetime)
SELECT CONVERT(varchar, #StartTimestamp, 103) + CONVERT(varchar, #StartTimestamp, 108)
Use Convert
SELECT CONVERT(VARCHAR(25), myDate, 101) + ' ' + CONVERT(VARCHAR(25), myDate, 108) + ' ' + RIGHT(CONVERT(VARCHAR(19),myDate),2)
FROM myTable
This would give you the exact output you requested
mm/dd/yyyy hh:mi:ss AM
(Or PM depending on time)
you need to use two converts with a cast and a right function:
SELECT CONVERT(VARCHAR(10), getdate(), 101) +
' ' + CONVERT(VARCHAR(15), getdate(), 108) +
' ' + RIGHT(CONVERT(VARCHAR(20),getdate()),2)
OUTPUT: 03/19/2015 18:44:19 PM

SQL server 2008 date conversion formats

I produce a CSV file but cannot figure out the proper date format.
I am aware of the MSDN site codes for datetime conversions:
http://msdn.microsoft.com/en-us/library/ms187928.aspx
It seems there is no code to convert my datetime into this format:
MM/DD/YYYY HH:MMAM
e.g.:
12/28/2014 4:33AM
How do you achieve such format?
Platform:
Microsoft SQL server 2008
SELECT CONVERT(VARCHAR(10), GETDATE(), 101) +
RIGHT(CONVERT(VARCHAR, GETDATE(), 100), 7)
This is what you can use and is probably the most straightforward:
SELECT
RIGHT('0' + cast(month(dateColumn) AS NVARCHAR(2)), 2) + '/' -- generate the day
+ RIGHT('0' + cast(day(dateColumn) AS NVARCHAR(2)), 2) + '/' -- generate the month
+ cast(year(dateColumn) AS NVARCHAR(4)) + ' ' -- generate the year
+ convert(VARCHAR, cast(dateColumn AS TIME)), 100) -- generate the time
FROM TABLE
SELECT CONVERT(VARCHAR, GetDate(), 101) + ' ' +
CONVERT(VARCHAR, DATEPART(hh, GetDate())) + ':' +
RIGHT('0' + CONVERT(VARCHAR, DATEPART(mi, GetDate())), 2) AS TIME
EDIT: This gets the AM/PM also
SELECT CONVERT(CHAR(11),GETDATE(),101)
+ CONVERT(CHAR( 5),GETDATE(),114)
+ RIGHT(CONVERT(CHAR( 5),GETDATE(),109), 2)
SELECT CONVERT(VARCHAR(10),GETDATE(),3) as 'dd/MM/yy'
SELECT CONVERT(VARCHAR(10),GETDATE(),103) as 'dd/MM/yyyy'
SELECT CONVERT(VARCHAR(10),GETDATE(),4) as 'dd.MM.yy'
SELECT CONVERT(VARCHAR(10),GETDATE(),104) as 'dd.MM.yyyy'
SELECT CONVERT(VARCHAR(10),GETDATE(),5) as 'dd-MM-yy'
SELECT CONVERT(VARCHAR(10),GETDATE(),105) as 'dd-MM-yyyy'
SELECT CONVERT(VARCHAR(10),GETDATE(),6) as 'ddMonthyy'
SELECT CONVERT(VARCHAR(10),GETDATE(),106) as 'ddMonthyyyy'
SELECT CONVERT(VARCHAR(10),GETDATE(),7) as 'Monthdd.yy'
SELECT CONVERT(VARCHAR(10),GETDATE(),107) as 'Monthdd.yyyy'
SELECT CONVERT(VARCHAR(10),GETDATE(),8) as 'hh.mm.ss'
SELECT CONVERT(VARCHAR(10),GETDATE(),108) as 'hh.mm.ss'
SELECT CONVERT(VARCHAR(100),GETDATE(),9) as 'Monthddyy hh.mm.ss.mss'
SELECT CONVERT(VARCHAR(100),GETDATE(),109) as 'Monthddyyyy hh.mm.ss.mss'
SELECT CONVERT(VARCHAR(100),GETDATE(),10) as 'mm-dd-yy'
SELECT CONVERT(VARCHAR(100),GETDATE(),110) as 'mm-dd-yyyy'
SELECT CONVERT(VARCHAR(100),GETDATE(),11) as 'yy/MM/dd'
SELECT CONVERT(VARCHAR(100),GETDATE(),111) as 'yyyy/MM/dd'
SELECT CONVERT(VARCHAR(100),GETDATE(),12) as 'yyMMdd'
SELECT CONVERT(VARCHAR(100),GETDATE(),112) as 'yyyyMMdd'
SELECT CONVERT(VARCHAR(100),GETDATE(),13) as 'ddMonthyy hh.mm.ss.mss'
SELECT CONVERT(VARCHAR(100),GETDATE(),113) as 'ddMonthyyyy hh.mm.ss.mss'

Convert datetime in sql server

How can i convert the datetime format below
2010-10-25 11:13:36.700
into
25-Oct-2010 or 2010-10-25 00:00:00.000
To get "25-Oct-2010"
Assuming the value is supplied as a string, not a DATETIME data type:
SELECT REPLACE(CONVERT(VARCHAR, CAST('2010-10-25 11:13:36.700' AS DATETIME), 106), ' ', '-')
See the CAST/CONVERT documentation for other formats, though the one you requested requires post-processing.
To get "2010-10-25 00:00:00.000"
The best performing means is to use DATEADD & DATEDIFF:
SELECT DATEADD(d, DATEDIFF(dd, 0, '2010-10-25 11:13:36.700'), 0)
References:
DATEADD
DATEDIFF
Testing
WITH sample AS (
SELECT CAST('2010-10-25 11:13:36.700' AS DATETIME) dt)
SELECT REPLACE(CONVERT(VARCHAR, s.dt, 106), ' ', '-') AS col1,
DATEADD(d, DATEDIFF(dd, 0, s.dt), 0) AS col2
FROM sample s
Returns:
col1 col2
-------------------------------------
25-Oct-2010 2010-10-25 00:00:00.000
Addendum
Being that you're on SQL Server 2005, you could make date formatting easier for yourself by creating a SQLCLR function that would allow you to use the .NET date formatting.
check this one
SELECT convert(VARCHAR, getdate(),106)
this will give you 25 Oct 2010
AND
SELECT REPLACE(convert(VARCHAR, getdate(),106), ' ' , '-')
this will give you 25-Oct-2010
Check if this can help you:
SELECT REPLACE(CONVERT(VARCHAR(11), GETDATE(), 106), ' ', '-')
You can get more info Here
PD. Stackoverflow can be too addicted sometimes, you might wanna try google before
select CONVERT(NVARCHAR(20),getDATE(),105)
Please try
SELECT CONVERT(varchar, GETDATE(), 1) --12/30/06
SELECT CONVERT(varchar, GETDATE(), 2) --06.12.30
SELECT CONVERT(varchar, GETDATE(), 3) --30/12/06
SELECT CONVERT(varchar, GETDATE(), 4) --30.12.06
SELECT CONVERT(varchar, GETDATE(), 5) --30-12-06
SELECT CONVERT(varchar, GETDATE(), 6) --30 Dec 06
SELECT CONVERT(varchar, GETDATE(), 7) --Dec 30, 06
SELECT CONVERT(varchar, GETDATE(), 10) --12-30-06
SELECT CONVERT(varchar, GETDATE(), 11) --06/12/30
SELECT CONVERT(varchar, GETDATE(), 12) --061230
SELECT CONVERT(varchar, GETDATE(), 23) --2006-12-30
SELECT CONVERT(varchar, GETDATE(), 101) --12/30/2006
SELECT CONVERT(varchar, GETDATE(), 102) --2006.12.30
SELECT CONVERT(varchar, GETDATE(), 103) --30/12/2006
SELECT CONVERT(varchar, GETDATE(), 104) --30.12.2006
SELECT CONVERT(varchar, GETDATE(), 105) --30-12-2006
SELECT CONVERT(varchar, GETDATE(), 106) --30 Dec 2006
SELECT CONVERT(varchar, GETDATE(), 107) --Dec 30, 2006
SELECT CONVERT(varchar, GETDATE(), 110) --12-30-2006
SELECT CONVERT(varchar, GETDATE(), 111) --2006/12/30
SELECT CONVERT(varchar, GETDATE(), 112) --20061230