Conversion format from time to default datetime - sql-server-2005

i have time as 1:00PM.I have to convert to format '1/1/1900 01:00:00 PM'.Can anybody help?

select convert(datetime , "1/1/1900 " + "1:00 PM" , 101)

See "CAST and CONVERT" in sql server books online
dd/mm/yy hh:mi:ss:mmmAM
you probably want 131
but return it as a varchar
so
SELECT CONVERT(VARCHAR(20),GETDATE(),131)

STEP 1: Let's get the date and save it into #Date variable.
DECLARE #Date DATETIME;
SELECT #Date = CONVERT(DATETIME, '1:00PM' , 100);
Now #Date = 1900-01-01 13:00:00.000
STEP 2: Let's convert it into custom format
SELECT CONVERT(VARCHAR, #Date, 101) + ' '
+ REPLACE(LTRIM(SUBSTRING(CONVERT(VARCHAR, #Date, 131), 12, 14)),
':000', ' ');
This returns '1/1/1990 1:00:00 PM'

Related

How to convert varchar date into datetime in sql

I have varchar date in this format:
03/13/2015 : 2130
and i would like to convert it into datetime something like this:
2015-03-13 21:30:00.000
i have seen example like this but did not work for what i am looking for
DECLARE #Date char(8)
set #Date='12312009'
SELECT CONVERT(datetime,RIGHT(#Date,4)+LEFT(#Date,2)+SUBSTRING(#Date,3,2))
This will work assuming all date times parts are padded with 0's consistently.
DECLARE #Input VARCHAR(50);
SET #Input = '03/13/2015 : 2130';
SET #Input = LEFT(#Input, 10) + ' ' + LEFT(RIGHT(#Input, 4), 2) + ':' + RIGHT(RIGHT(#Input, 4), 2);
PRINT #Input;
PRINT CONVERT(DATETIME, #Input);
PRINT CONVERT(VARCHAR(50), CONVERT(DATETIME, #Input), 121);
Output:
03/13/2015 21:30
Mar 13 2015 9:30PM
2015-03-13 21:30:00.000
OP wants mmddyy and a plain convert will not work for that:
select convert(datetime,'12312009')
Msg 242, Level 16, State 3, Line 1
The conversion of a char data type to a datetime data type resulted in
an out-of-range datetime value
so try this:
DECLARE #Date char(8)
set #Date='12312009'
SELECT CONVERT(datetime,RIGHT(#Date,4)+LEFT(#Date,2)+SUBSTRING(#Date,3,2))
OUTPUT:
2009-12-31 00:00:00.000
(1 row(s) affected)
I think this is what you're looking for:
DECLARE #Date VARCHAR(20)
SET #Date = '03/13/2015 : 2130'
-- Format the date string
SET #Date = LEFT(#Date, 10) + ' ' + SUBSTRING(#Date, 14, 2) + ':' + SUBSTRING(#Date, 16, 2)
-- convert to date
Select CONVERT(varchar, CONVERT(DATETIME, #Date), 121)
SQL Fiddle
More Info
Sql Function:
CONVERT(data_type(length),expression,style)
you can try this:
CONVERT(datetime,#varCharDate,121)
For more see this link
If convert is not working for you then you can use mid to take date, month, year etc. And then use str_to_date to construct datetime in desired format.
In oracle use to_date and this stackoverflow link for taking substring

How to display Date in DD/MM/YYYY H:MM AM/PM format in SQL Server

I have a DATETIME Column in a table and I need to show the date in the
following format "DD/MM/YYYY H:MM AM/PM"
CreatedBy
2013-07-30 12:44:06.000
2013-07-30 12:45:57.000
2013-08-05 16:51:26.000
2013-08-05 19:08:18.000
2013-08-05 19:11:46.000
2013-09-12 12:44:27.000
I need the date like this--> "30/07/2013 12.44 PM"
Use this in SQL:
print convert(nvarchar(10), getdate(), 103) + right(convert(nvarchar(30), getdate(), 0), 8)
declare #Date datetime = '2013-07-30 12:44:06.000',
#Time time = '12:44:06.000',
#VarcharDate varchar(100),
#VarcharTime varchar(100)
set #VarcharDate= CONVERT(varchar,#Date,111) -- get the date in the format you want
set #VarcharTime = CONVERT(varchar,#Date) -- get the time in format you want
print #VarcharDate + ' '+SUBSTRING(#VarcharTime,13,LEN(#VarcharTime))
This will give you the desire output.
Try this
SELECT convert(varchar(20), GetDate(), 0);
To extract only AM/PM
substring(convert(varchar(30), GetDate(), 9), 25, 2);
DECLARE #Date AS DATETIME
SET #Date = '2013-07-30 12:44:06.000'
SELECT CONVERT(VARCHAR(16), #Date, 103) + ' ' +
substring(convert(varchar(30), #Date, 100), 13, 8)

Get DateTime with time as 23:59:59

I'm trying to do a where statement that specifies a DateTime field is between the start and end of the previous month.
To do this, I need to specify that the first day of the previous month has a time of 00:00:00 and the last day of the previous month has a time of 23:59:59.
This second condition is giving me a headache..
Can someone help me out?
Cheers
MSSQL 2008
try:
SELECT DATEADD(ms, -3, '2011-07-20')
This would get the last 23:59:59 for today.
why 3 milliseconds?, this is because Microsoft SQL Server DATETIME columns have at most a 3 millisecond resolution (something that is not going to change). So all we do is subtract 3 milliseconds
You can also use the less than '<' without the equal. So that you don't need 23:59:59.
Eg.
WHERE DateCreated < '20111201 00:00:00'
Try this, it could be helpful for you
I use one of these two ways to work with time portion in DATETIME fields to do comparisons
EX: get a user log for one day, i.e. from Today's date at 12:00:00 AM till Today's date but at 12:00:00 PM
DECLARE #FromDate datetime
DECLARE #ToDate datetime
SET #FromDate = GETDATE()
SET #ToDate = GETDATE()
Print '------------------------ '
PRINT #FromDate
PRINT #ToDate
SET #FromDate = CONVERT(DATETIME, CONVERT(varchar(11),#FromDate, 111 ) + ' 00:00:00', 111)
SET #ToDate = CONVERT(DATETIME, CONVERT(varchar(11),#ToDate, 111 ) + ' 23:59:59', 111)
Print '------------------------ '
PRINT #FromDate
PRINT #ToDate
DECLARE #TEST_FROM DATETIME
SET #TEST_FROM = dateadd(month,((YEAR(#FromDate)-1900)*12)+MONTH(#FromDate)-1,DAY(#FromDate)-1) + ' 12:00:00'
DECLARE #TEST_TO DATETIME
SET #TEST_TO = dateadd(month,((YEAR(#ToDate)-1900)*12)+MONTH(#ToDate)-1,DAY(#ToDate)-1) + ' 23:59:59'
Print '------------------------ '
PRINT #TEST_FROM
PRINT #TEST_TO
This will print the following in SQL Query editor screen
------------------------
Dec 28 2011 3:18PM
Dec 28 2011 3:18PM
------------------------
Dec 28 2011 12:00AM
Dec 28 2011 11:59PM
------------------------
Dec 28 2011 12:00PM
Dec 28 2011 11:59PM
References
The way using the convert is from my experience, the other way is from this link http://weblogs.sqlteam.com/jeffs/archive/2007/01/02/56079.aspx
Have fun :)
Try this query for using Datetime datatype to get
2018-01-29 23:59:59.997
select dateadd(ms, -3, (dateadd(day, +1, convert(varchar, GETDATE(), 101))))
declare #myDate DateTime, #lastMonth DateTime, #thisMonth DateTime
set #myDate = GETDATE()
set #lastMonth = DateAdd(month, -1, CAST(#myDate as Date))
set #thisMonth = DateAdd(day, -DatePart(day, #myDate)+1, CAST(#myDate as Date))
select #myDate as MyDate, DateAdd(day, -DatePart(day, #lastMonth) + 1, #lastMonth) FirstDay, DateAdd(second, -1, #thisMonth) LastDay
Results
Try This:
SELECT dateadd(millisecond,-1,cast(cast(getdate() AS date) AS datetime2))
SELECT DATEADD(ms, -2, CAST(CONVERT(date, DATEADD (DAY,1,getdate())) AS varchar(10)))
output: yyyy-mm-dd 23:59:59.997
2020-08-31 23:59:59.997
I hope someone finds this useful
declare #TodaysDate smalldatetime
declare #TodaysDatepm smalldatetime
First I get the date and Time as of Midnight i.e 16/05/2021 12:00 am
set #TodaysDate = DATEADD(minute, 0,CAST(FLOOR(CAST(CURRENT_TIMESTAMP AS float)) AS datetime))
Then I add 23hours and 59 minutes onto it i.e (60*23)+59
Which gives 1439, from there I use the the dateadd function
set #TodaysDatepm =DATEADD(minute, 1439, #TodaysDate)
This will always print out midnight of what you set in #TodaysDate
Print #TodaysDatepm

Convert SQL DateTime format

How can I display a DATETIME value (2010-12-02 15:20:17.000) as 02/12-2010 15:20?
For SQL Server:
select stuff(convert(varchar, getdate(), 105), 3, 1, '/') + ' ' + left(convert(varchar, getdate(), 8), 5)
DateTime is a DateTime is a DateTime - it just holds a date and time and doesn't have any string representation, really.
See the CAST and CONVERT topic in the SQL Server Books Online for details - it shows all supported date formats that SQL Server supports.
For your source format (2010-12-02 15:20:17.000) you could probably use style no. 121
DECLARE #source VARCHAR(50)
SET #source = '2010-12-02 15:20:17.000'
DECLARE #Date DATETIME
SELECT #Date = CONVERT(DATETIME, #source, 121)
SELECT #Date
but your target format is a bit odd..... I don't see any "out of the box" style that would match your needs. You'll need to use some string manipulation code to get that exact format.
Use MSSQL's build-in function to convert datetime to string with format,
SELECT CONVERT(VARCHAR(8), GETDATE(), 1) AS [MM/DD/YY] --2/5/12
SELECT CONVERT(VARCHAR(10), GETDATE(), 103) AS [DD/MM/YYYY] --5/2/2012
You need to create custom function to get various format to use like this;
SELECT dbo.ufn_FormatDateTime(GETDATE(),'YYYY-MM-DD HH:mm:SS tt')
--Output : 2012-02-05 01:58:38 AM
SELECT dbo.ufn_FormatDateTime(GETDATE(),'(dddd) mmmm dd, yyyy hh:mm:ss.fff tt')
--Output : (Sunday) February 05, 2012 01:58:38.723 AM
SELECT dbo.ufn_FormatDateTime(GETDATE(),'dd/MM/yyyy')
--Output : 05/02/2012
SELECT dbo.ufn_FormatDateTime(GETDATE(),'yyyy MMM, dd (ddd) hh:mm:ss tt')
-- Output : 2012 Feb, 05 (Sun) 01:58:38 AM
Get the code snippet from this link.
http://www.tainyan.com/codesnippets/entry-62/sql-server-date-time-format-function.html
http://msdn.microsoft.com/en-us/library/ms189491.aspx
Is this what you're looking for?
Assuming Oracle:
select TO_CHAR(SYSDATE, "dd/mm-yyyy HH24:mi")
from DUAL;
Assuming SQL Server:
select STR(DATEPART(DAY, GETDATE()), 2)
+ '/'
+ STR(DATEPART(MONTH, GETDATE()), 2)
+ '-'
+ STR(DATEPART(YEAR, GETDATE()), 4)
+ ' '
+ STR(DATEPART(HOUR, GETDATE()), 2)
+ ':'
+ STR(DATEPART(MINUTE, GETDATE()), 2);
Little example I use for Germany and Switzerland: dd.mm.yyyy hh:mm
SELECT CONVERT(varchar, GETDATE(), 104) + ' ' + LEFT(CONVERT(varchar, GETDATE(), 108), 5)

str_to_date function in sql server?

MySQL has a function called STR_TO_DATE, that converts a string to date.
Question:
Is there a similar function in SQL Server?
If you need to parse a particular format, use CONVERT(datetime, #mystring, #format). Use this as a reference: https://web.archive.org/web/20200729210252/http://www.sqlusa.com/bestpractices/datetimeconversion/
Some examples:
SELECT convert(datetime, '10/23/2016', 101) -- mm/dd/yyyy
SELECT convert(datetime, '2016.10.23', 102) -- yyyy.mm.dd ANSI date with century
SELECT convert(datetime, '23/10/2016', 103) -- dd/mm/yyyy
SELECT convert(datetime, '23.10.2016', 104) -- dd.mm.yyyy
SELECT convert(datetime, '23-10-2016', 105) -- dd-mm-yyyy
-- mon types are nondeterministic conversions, dependent on language setting
SELECT convert(datetime, '23 OCT 2016', 106) -- dd mon yyyy
SELECT convert(datetime, 'Oct 23, 2016', 107) -- mon dd, yyyy
-- 2016-10-23 00:00:00.000
SELECT convert(datetime, '20:10:44', 108) -- hh:mm:ss
-- 1900-01-01 20:10:44.000
What if the string is 7/7/2010?
Then use CONVERT with either 101 (mm/dd/yy) or 103 (dd/mm/yy) depending on what you want:
SELECT CONVERT(DATE, '7/7/2010', 103)
Result:
2010-07-07
CAST(<string> AS DATETIME)
Use CAST.
declare #MyString varchar(10)
declare #MyDate datetime
set #MyString = '2010-08-19'
set #MyDate = cast(#MyString as datetime)
select #MyDate
Here is a good example:
declare #myDate datetime
set #myDate = '06/09/2017'
select concat(convert(varchar(20), #myDate,101), ' -- ',
convert(varchar(20), #myDate,103), ' -- ',
convert(varchar(20), #myDate,6))
This is what you get, depending on 101 or 103 or 6:
09/06/2017 -- 06/09/2017 -- 06 Sep 17
A good summary of types of dates is here - https://www.w3schools.com/sql/func_convert.asp
On MSSQL:
select cast('2012/06/12 10:32AM' as datetime);
You will get it:
2012-06-12 10:32:00.000