How do I get my output to be HH:mm (09:40) and not HH:mm:ss xxxxxxx (09:40:00.0000000)
Below is my query
SELECT
PatientMRN,
first_ScheduleTimeSlotDate,
CASE WHEN ExaminationTypecode in ('MSMBO') THEN dateadd(
HOUR,-1, first_ScheduleTimeSlotTime
) WHEN ExaminationTypecode not in ('MSMBO')
and EX_Sub_Modality like 'MR%' THEN dateadd(
MINUTE,-15, first_ScheduleTimeSlotTime
) WHEN ExaminationTypecode not in ('MSMBO')
and EX_Modality in ('Ultrasound', 'CT', 'MRI')
THEN (first_ScheduleTimeSlotTime) ELSE '' END as appointment_time1
I want the output to look like appointment_time but I get appointment_time1
appointment_time appointment_time1
09:40 09:40:00 0000000
11:15 11:15:00 0000000
12:20 12:20:00 0000000
15:05 15:05:00 0000000
08:00 08:00:00 0000000
You can achieve it using format function. Below is an example which extracts the hour and minute parts in the format you wanted from appointment_time1 field.
;with cte AS
(
SELECT
PatientMRN,
first_ScheduleTimeSlotDate,
CASE WHEN ExaminationTypecode in ('MSMBO') THEN dateadd(
HOUR,-1, first_ScheduleTimeSlotTime
) WHEN ExaminationTypecode not in ('MSMBO')
and EX_Sub_Modality like 'MR%' THEN dateadd(
MINUTE,-15, first_ScheduleTimeSlotTime
) WHEN ExaminationTypecode not in ('MSMBO')
and EX_Modality in ('Ultrasound', 'CT', 'MRI')
THEN (first_ScheduleTimeSlotTime) ELSE '' END as appointment_time1
)
select PatientMRN,
first_ScheduleTimeSlotDate,
FORMAT(appointment_time1, N'hh:mm') AS appointment_time,
appointment_time1,
from cte
IF you change
THEN (first_ScheduleTimeSlotTime) ELSE '' END
to
THEN (first_ScheduleTimeSlotTime) ELSE NULL END
then the engine will not convert the date/time values to strings (it has to since you're specified a string value as the "default". That will leave the values as date/times (like the first column) and you can format them however you want when you display them.
Related
My Input :
2020-01-01 09:01:00.000
2020-01-03 18:01:00.000
My Output Should be like :
9 AM
6 PM
You can use the format function like this as long as you're using SQL Server 2012 or later.
SELECT FORMAT(GETDATE(),'h tt') AS MyTime
Note the use of the lower case 'h' which gives a 12 hour clock with no leading zero, 'HH' gives a two digit 24 hour clock so you end up with output like '16 PM'.
If the input is a date-related type, you can use FORMAT with the appropriate format string, eg :
SELECT FORMAT(getdate(), 'HH:mm tt')
For me, this returns :
12:51 PM
Just HH tt returns the desired string, in this case :
12 PM
You can do conversion :
SELECT CONVERT(varchar(10), CONVERT(TIME, '2020-01-01 09:01:00.000'), 100)
I would do:
SELECT V.YourColumn,
LTRIM(SUBSTRING(ca.v,13,2) + ' ' RIGHT(ca.v,2))
FROM (VALUES(CONVERT(Datetime2(3),'2020-01-01 09:01:00.000')),
(CONVERT(Datetime2(3),'2020-01-03 18:01:00.000')))V(YourColumn)
CROSS APPLY (VALUES(CONVERT(varchar(30),V.YourColumn,0))) ca(v);
In the CROSS APPLY I convert the datetime to the format MMM d YYYY h:mmAM/PM, then you can use SUBSTRING/LEFT to get the bits you want.
An alternative approach would be to use DATEPART:
SELECT CONCAT(CASE DATEPART(HOUR,V.YourColumn) WHEN 12 THEN 12 WHEN 0 THEN 12 ELSE DATEPART(HOUR,V.YourColumn) % 12 END,' ', RIGHT(CONVERT(varchar(30),V.YourColumn,0),2))
FROM (VALUES(CONVERT(Datetime2(3),'2020-01-01 09:01:00.000')),
(CONVERT(Datetime2(3),'2020-01-03 00:01:00.000')),
(CONVERT(Datetime2(3),'2020-01-03 18:01:00.000')))V(YourColumn);
Because 12 % 12 and 0 % 12 both = 0, then you need the CASE expression to handle those.
Your query.
SELECT convert(VARCHAR(30), cast('2020-01-01 09:01:00.000' AS DATETIME), 100)
,convert(VARCHAR(30), cast('2020-01-03 18:01:00.000' AS DATETIME), 100)
I have a 24 hour time, start_time, that is stored as varchar (no control over this). I need to convert it to 12 hour with AM/PM.
Examples:
17:45 should become 5:45 PM
09:00 should become 9:00 AM
Do multi conversations :
select convert(varchar(20), convert(time(0), start_time), 100)
from table t;
EDIT :
Use apply :
select stuff(time_c, len(time_c)-1, 0, ' ')
from table t cross apply
( values (convert(varchar(20), convert(time(0), start_time), 100))
) t(time_c)
With a space between the time and AM/PM:
select replace(replace(convert(varchar(8), convert(time(0), '09:00'), 100),'A',' A'),'P',' P')
It is easy with FORMAT function:
SELECT timestr
, FORMAT(CONVERT(DATETIME, timestr, 108), 'h:mm t')
, FORMAT(CONVERT(DATETIME, timestr, 108), 'h:mm tt')
FROM (VALUES
('17:45'),
('09:00')
) t(timestr)
Note that tt format specifier does not work with time datatype.
You really should be fixing your design and I strongly suggest you do. The time data type exists for a reason, and the format of the value should be determined in the presentation layer, not in the RDBMS. If, you have to conver into the RDBMS in an inferior format, then you would need to CONVERT to time, and then back to varchar and use some string manipulation:
SELECT V.YourTime,
STUFF(NewTime, LEN(NewTime)-4,3,' ') AS NewTime
FROM (VALUES('09:00'),('17:45'))V(YourTime)
CROSS APPLY(VALUES(CONVERT(varchar(10),CONVERT(time(0),V.YourTime),109)))C(NewTime);
DB<>Fiddle
I try to execute SQL select query given below. Tarih is a datetime field. Tarih in table: 2013-01-09 00:00:00.000
but my search value is 09.01.2013.
select * from [BankaEntegrasyonEslesmeyenler] as x where x.Tarih='09.01.2013'
99 6631 220220059 BÜLENT ÖZCAN
28.3200 2831.5500 2013-01-09 00:00:00.000 TL 8
Try this
SELECT * FROM [BankaEntegrasyonEslesmeyenler] AS x WHERE CONVERT (CHAR(10), x.Tarih, 104) = '09.01.2013'
format the date in dd.mm.yyyy and compare date without time.
Pass your date value as parameter or in the universal ISO format such as
yyyymmdd, e.g.
'20130109'
You are confused on how it is stored. 2013-01-09 00:00:00.000 is just the default display format.
If you want that format then try this
select CONVERT ( char(10) , getdate() , 104 )
select CONVERT ( char(10) , x.Tarih , 104 ) from ....
CAST and CONVERT (Transact-SQL)
SELECT CONVERT(VARCHAR(8), GETDATE(), 3) AS [DD/MM/YY]
selects the date in dd/mm/yy format
I m trying to dig more in the SQL query , I'm with it.
Yearmo Back_1_month
------- ------------
201202 201201 Where 201202 is Feb 2012 , 201201 is Jan 2012
201201 201112
201204 201203
201212 201211
I m confused , how can make it for 201201.
Assuming that yearmo is an int:
DECLARE #Yearmo INT
SET #Yearmo = 201201
-- First, convert yearmo to a varchar.
-- Next, add a "day" portion to the varchar.
-- Then, convert to a date.
-- Then, subtract a month.
-- Finally, convert back to your original format using char(6).
SELECT CONVERT(
CHAR(6),
(DATEADD(MONTH, -1, CAST(CAST(#YearMo AS VARCHAR) + '01' AS DATETIME))),
112
)
This works in one single sql query :
SELECT CONVERT(VARCHAR(6), DATEADD(month, -1 , convert(datetime, CAST ( 201201 AS VARCHAR(8)) + '01', 112) ) , 112)
To substract a month, you can use DATEADD like this
SELECT DATEADD(month, -1, yearmo)
But then you will need to format your date and you might need to cast yearmo as a datetime if it's not.
Try this:
SELECT yearmo,
LEFT(CONVERT(CHAR(8), DATEADD(month, -1, CONVERT(datetime, yearmo + '01')), 112), 6)
FROM <YOUR_TABLE>
SQL Fiddle
You can create a Stored Procedure with argument as yearmo and then save substring(yr) and substring(mo) in two variables and then see, if substring(mo) is 01 then decrement substring(yr) after casting and change substring(mo) to 12 and then append both substring and return.
Yesterday I posted a question about a problem i need to solve in SQL Server 2005/2008. There were some answer that were useful but they were written much advanced than my experience. I have updated the desired output:
select state, date_time, item sold
from product
Below is the sample data only. Actual date range is 12/10/2010 to 01/15/2011.
----------------------------------------------------------
State | Date_time | Item_sold
----------------------------------------------------------
VA 12/10/2010 1:30:00 PM Candy
VA 12/10/2010 3:30:00 PM Chips
VA 12/13/2010 12:50:00 AM Wine
DC 12/13/2010 8:00:00 AM Gum
DC 12/13/2010 12:30:00 PM Bags
DC 12/13/2010 1:16:00 PM Cheese
DC 12/13/2010 12:00:00 AM Hotdog
NJ 12/14/2010 12:00:00 AM Coffee
NJ 12/14/2010 1:15:00 PM Beers
NJ 12/14/2010 3:45:00 AM Cream
NJ 12/14/2010 1:45:00 PM Water
Is there a way in SQL server that can count the products sold in each state starts from
12/10/2010 to 12/11/2011; 12/10/2010
to 12/12/2010; 12/10/2010 to
12/13/2010; 12/10/2010 to 12/14/2010;
12/10/2010 to 12/15/2010...?
the sample output would be:
State 12/10 to 12/11 12/10 to 12/12 12/10 to 12/13 12/10 to 12/14
VA 2 2 3 3
DC 0 0 3 3
NJ 0 0 0 4
Thanks a gain, folks.
Off the top of my head something like:
SELECT State, convert(varchar,Date_Time,101) as Period, count(item_sold)
FROM YourTable
Group By State, Period
Order by Period, State
If you want the format shown above have a look at the T-SQL Pivot statement
I would strongly suggest creating a table of dates or numbers (often called a tally table).
You would have to do this with dynamic SQL to handle the changing output schema.
Use that in combination with a dynamic pivot technique often discussed here on SO.
You have a complex requirement that require a complex query. I really don't see a "simple" alternative for such requirements. I would suggest working through How to count incrementally in SQL SERVER (your original question)
I can't see when you would really want to do dynamic columns to that level. I can see if you have a variable number of statuses, between 3 and 10 or something, and you wanted that. But this will grow with a column for every day past the begin date if I understand correctly, which can't be what you want.
That being said, here is a solution which would allow you to have multiple columns with different specified date ranges cleanly. Let me know what you think.
IF EXISTS(SELECT * FROM sys.objects WHERE [object_id] = OBJECT_ID('ProductTest'))
DROP TABLE ProductTest;
GO
CREATE TABLE ProductTest
(
InState CHAR(2)
, DateSold DATETIME2(1)
, ItemSold VARCHAR(20)
);
GO
INSERT INTO ProductTest
VALUES ('VA', '12/10/2010 1:30:00 PM', 'Candy')
, ('VA', '12/10/2010 3:30:00 PM', 'Chips')
, ('VA', '12/13/2010 12:50:00 AM', 'Wine')
, ('DC', '12/13/2010 8:00:00 AM', 'Gum')
, ('DC', '12/13/2010 12:30:00 PM', 'Bags')
, ('DC', '12/13/2010 1:16:00 PM', 'Cheese')
, ('DC', '12/13/2010 12:00:00 AM', 'Hotdog')
, ('NJ', '12/14/2010 12:00:00 AM', 'Coffee')
, ('NJ', '12/14/2010 1:15:00 PM', 'Beers')
, ('NJ', '12/14/2010 3:45:00 AM', 'Cream')
, ('NJ', '12/14/2010 1:45:00 PM', 'Water');
GO
IF EXISTS(SELECT * FROM sys.objects WHERE [object_id] = OBJECT_ID('udfProductsSoldInDateRange'))
DROP FUNCTION udfProductsSoldInDateRange;
GO
CREATE FUNCTION udfProductsSoldInDateRange (#StartDate AS DATE, #EndDate AS DATE, #State CHAR(2))
RETURNS INT
AS
BEGIN
DECLARE #Result INT;
SELECT
#Result = COUNT(*)
FROM ProductTest
WHERE CAST(ProductTest.DateSold AS DATE) BETWEEN #StartDate AND #EndDate
AND ProductTest.InState = #State;
RETURN #Result;
END;
GO
DECLARE #StartDate DATE = '20101210';
DECLARE #EndDate1 DATE = '20101211';
DECLARE #EndDate2 DATE = '20101212';
DECLARE #EndDate3 DATE = '20101213';
DECLARE #EndDate4 DATE = '20101214';
SELECT
InState
, dbo.udfProductsSoldInDateRange(#StartDate, #EndDate1, InState) AS [To 12/11]
, dbo.udfProductsSoldInDateRange(#StartDate, #EndDate2, InState) AS [To 12/12]
, dbo.udfProductsSoldInDateRange(#StartDate, #EndDate3, InState) AS [To 12/13]
, dbo.udfProductsSoldInDateRange(#StartDate, #EndDate4, InState) AS [To 12/14]
FROM ProductTest
GROUP BY InState
ORDER BY InState
-- Or if you don't want to make the function, just run this query and hardcode your values.
SELECT
InState
, COUNT(*) AS ItemsSold
FROM ProductTest
WHERE CAST(ProductTest.DateSold AS DATE) BETWEEN #StartDate AND #EndDate3
GROUP BY InState
ORDER BY InState