having a time with 2 digits when using DATEPART [duplicate] - sql

I want to add a leading zero before time in (12 Hrs Format)
Example:
SELECT RIGHT('0000000'+CONVERT(VARCHAR(20), CONVERT(DATETIME, '01:00PM', 0), 100), 7)
The result is 1:00PM
I am expected result like 01:00PM
But, when I try below code it works
SELECT RIGHT('0000000'+'1:00PM',7)
I want to know why the first query not given the expected result.

Try with this
SELECT FORMAT (getdate(), 'hh:mmtt') as time
The result is: 07:30AM
SELECT FORMAT (CONVERT(DATETIME, '01:00PM', 0), 'hh:mmtt') as time
The result is : 01:00PM

If you run:
select '"' + CONVERT(VARCHAR(20), CONVERT(DATETIME, '01:00PM', 0), 100) + '"'
You will get:
"Jan 1 1900 1:00PM"
I'm not sure why you think that prepending '0000000' and taking the last seven characters would do what you want.
It is a little hard to figure out what you really want, because you are starting with a constant that has the format you want.
If you want a zero-padded 12-hour time format, you can use:
select right('0' + convert(varchar(255), convert(time, getdate()), 100), 7)

Just try it step by step to see what is happening:
SELECT CONVERT(DATETIME, '01:00PM', 0)
--> 1900-01-01T13:00:00Z
SELECT CONVERT(VARCHAR(20), CONVERT(DATETIME, '01:00PM', 0))
--> Jan 1 1900 1:00PM
SELECT '0000000'+CONVERT(VARCHAR(20), CONVERT(DATETIME, '01:00PM', 0))
--> 0000000Jan 1 1900 1:00PM
And the seven rightmost letters of the last expression are, obviously, 1:00PM.
As others have suggested, use FORMAT (SQL Server 2012 and above) instead:
SELECT FORMAT(CONVERT(DATETIME, '01:00PM', 0), 'hh:mmtt')
--> 01:00PM

This part of the code:
CONVERT(VARCHAR(20), CONVERT(DATETIME, '01:00PM', 0), 100)
will give you this result: Jan 1 1900 1:00PM
This part:
CONVERT(DATETIME, '01:00PM', 0)
will return this: 1900-01-01 13:00:00.000
What is the goal here ?
I agree with the option provided from #SANDEEP answer.
Check the demo

Related

Leading Zero Before Time in SQL Server

I want to add a leading zero before time in (12 Hrs Format)
Example:
SELECT RIGHT('0000000'+CONVERT(VARCHAR(20), CONVERT(DATETIME, '01:00PM', 0), 100), 7)
The result is 1:00PM
I am expected result like 01:00PM
But, when I try below code it works
SELECT RIGHT('0000000'+'1:00PM',7)
I want to know why the first query not given the expected result.
Try with this
SELECT FORMAT (getdate(), 'hh:mmtt') as time
The result is: 07:30AM
SELECT FORMAT (CONVERT(DATETIME, '01:00PM', 0), 'hh:mmtt') as time
The result is : 01:00PM
If you run:
select '"' + CONVERT(VARCHAR(20), CONVERT(DATETIME, '01:00PM', 0), 100) + '"'
You will get:
"Jan 1 1900 1:00PM"
I'm not sure why you think that prepending '0000000' and taking the last seven characters would do what you want.
It is a little hard to figure out what you really want, because you are starting with a constant that has the format you want.
If you want a zero-padded 12-hour time format, you can use:
select right('0' + convert(varchar(255), convert(time, getdate()), 100), 7)
Just try it step by step to see what is happening:
SELECT CONVERT(DATETIME, '01:00PM', 0)
--> 1900-01-01T13:00:00Z
SELECT CONVERT(VARCHAR(20), CONVERT(DATETIME, '01:00PM', 0))
--> Jan 1 1900 1:00PM
SELECT '0000000'+CONVERT(VARCHAR(20), CONVERT(DATETIME, '01:00PM', 0))
--> 0000000Jan 1 1900 1:00PM
And the seven rightmost letters of the last expression are, obviously, 1:00PM.
As others have suggested, use FORMAT (SQL Server 2012 and above) instead:
SELECT FORMAT(CONVERT(DATETIME, '01:00PM', 0), 'hh:mmtt')
--> 01:00PM
This part of the code:
CONVERT(VARCHAR(20), CONVERT(DATETIME, '01:00PM', 0), 100)
will give you this result: Jan 1 1900 1:00PM
This part:
CONVERT(DATETIME, '01:00PM', 0)
will return this: 1900-01-01 13:00:00.000
What is the goal here ?
I agree with the option provided from #SANDEEP answer.
Check the demo

Varchar date conversion in SQL

My date field has value format to be : Feb 15 2019. Is there a way to convert this to MMDDYYYY format?
Desired output: 02152019
Query I tried: SELECT convert(varchar, getdate(), 112) - this query is showing YYYYMMDD format :(
Any help?
select format( convert(date, 'Feb 15 2019'), 'MMddyyyy')
-- results to: 02152019
-- But again, your application is to take care about format!!!
Try this query,
select replace(convert(varchar, getdate(),101),'/','')
You can try this
Select convert(varchar(12), convert(date, 'Feb 15 2019'), 112)
or
SELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 101), '/', '') AS [MMDDYYYY]
This will give output - 20190215.
You can pass different value like 101. For different output see Here.

Manipulating the GETDATE function to display DD MM YY

I have been trying to manipluate the getDatefunction in SQL but I want the date to come out as todays date and look exactly like this 07-Jan-15 but I cant't get it to work and I have tried everything!
What I have at the minute is:
convert(varchar(104), GETDATE()) as [Date]
but that is giving me Jan 7 2015 1:52PM and thats not what I'm looking for! I have tried all the convert options! So any help with this matter would be appreciated
Try
SELECT replace(convert(NVARCHAR, GETDATE(), 106), ' ', '-') as [Date]
Output:- 07-Jan-2015
OR
SELECT replace(LEFT(convert(NVARCHAR, getdate(), 106),6) + '-' +
RIGHT(year(convert(NVARCHAR, getdate(), 106)),2), ' ', '-') as [Date]
Output:- 07-Jan-15

Converting datetime format to 12 hour

I have this query
select CONVERT(varchar(5), tdate ,108) AS [Time] from table
which gives me the time in 24 hour format( military)
I wanted to convert it into a 12 hour format so i tried the query below
select SUBSTRING(CONVERT(VARCHAR, tdate, 100),13,2) + ':'
+ SUBSTRING(CONVERT(VARCHAR, tdate, 100),16,2) + ''
+ SUBSTRING(CONVERT(VARCHAR, tdate, 100),18,2) AS T
from table
and i get the 12 hour format but I am just curious if there is a shorter or better way of doing it. any help?
If you want to convert the current datetime for example:
SELECT CONVERT(VARCHAR, getdate(), 100) AS DateTime_In_12h_Format
Instead of getdate() you can put your desired column in a query (such as tdate in your example). If you want JUST the time in 12h and not the date and time use substring/right to separate them. It seems that you already know how to =).
This page lists every datetime conversion. It's really handy if you need other types of conversions.
This will return just the time, not the date.
SELECT RIGHT(CONVERT(VARCHAR, getdate(), 100), 7) AS time
For your table data:
select RIGHT(CONVERT(varchar, tdate ,100), 7) AS [Time] from table
Below code will return only time like 10:30 PM
SELECT FORMAT(CAST(getdate() AS DATETIME),'hh:mm tt') AS [Time]
Get date of server
SELECT LTRIM(RIGHT(CONVERT(VARCHAR(20), GETDATE(), 100), 7))
or
If it is stored in the table
SELECT LTRIM(RIGHT(CONVERT(VARCHAR(20), datename, 100), 7))
Result:
11:41AM
ifnull(date_format(at.date_time,'%d/%m/%Y'),"") AS date_time,
ifnull(time_format(at.date_time ,'%h:%i:%s'),"") AS date_time
This is how a SQL procedure looks...(for separating date and time)..there is no need of a special column for time/date....
Note:if H instead of h it will show the "hour in 24 hour" format

Format SQL DATETIME result like --> 8/25/2011 4:35 PM <-- NOT --> 2011-08-25 16:35:51.000 <--

I have a SQL Query running on SQL Server 2008 R2 that returns a DATETIME column. The current formatting returns this:
2011-08-25 16:35:51.000
and what I would rather see is this:
8/25/2011 4:35 PM
Ideas of how I can modify my SELECT statement? Here a simple statement to work with...
SELECT DtCheckIn from Ticket
Thank you, Andrew
You could do the following:
SELECT convert(varchar, getdate(), 101) + RIGHT(convert(varchar, getdate(), 100), 7)
Resources on date time formatting:
http://anubhavg.wordpress.com/2009/06/11/how-to-format-datetime-date-in-sql-server-2005/
http://www.mssqltips.com/sqlservertip/1145/date-and-time-conversions-using-sql-server/
EDIT - it's a little hokey but it should give you what you are looking for.
Basically the first part SELECT convert(varchar, getdate(), 101) gives you your date 08/25/2011.
The second part SELECT convert(varchar, getdate(), 100) gives you your date and time in the format Aug 25 2011 5:35PM. You only want the time portion which is in the format hh:mmAM or hh:mmPM the total number of characters you want is 7. So if you change this to SELECT RIGHT(convert(varchar, getdate(), 100), 8) you will get your time 5:37PM. I used 8 in my RIGHT() to get an extra space. But now you only want your time not AM/PM so add SELECT LEFT(RIGHT(convert(varchar, getdate(), 100), 8), 6) and you will get your hh:mm.
Finally you want your AM/PM SELECT RIGHT(convert(varchar, getdate(), 100), 2) this will get you the last 2 characters.
Put it all together and you get:
SELECT convert(varchar, getdate(), 101)
+ LEFT(RIGHT(convert(varchar, getdate(), 100), 8), 6)
+ ' ' + RIGHT(convert(varchar, getdate(), 100), 2)
Which gives your final product:
08/25/2011 5:39 PM