how to use the data in parameter and make a format like this #Hours : #Mins - sql-server-2005

how to use the data in parameter and make a format like this #Hours : #Mins and display and output like this 24:33. because I use A different parameter for the hours and mins. now I want to call it in the UI so I want it to output like this 24:33

Create a variable to store #Hours + ':' + #Mins

Related

How to properly combine static hour and minute

We have in our table, in SQL Server 2008, the datetime is only holding the date.
There is also an hour col and a minute col, there are for the appointment. the are both columns defined as number.
The hour is typically like this:
12.00
and minute is like 40.00. I tried adding them but it gives a total rather than
12:40 which is what we need. How can I get this to show 12:40. With the : would be better.
Well... taking a shot here, for SQL Server
declare #table table (d datetime,h decimal(4,2), m decimal(4,2))
insert into #table
values
('20170113',12.00,40.00),
('20170113',9.00,8.00)
select
d + cast(left(h,len(floor(h))) + ':' + cast(left(m,len(floor(m))) as varchar(2)) + ':' + '00' as datetime)
from
#table
Prepend 00. to minutes - please note that dot there.
Convert both to times.
Add them.
Convert result back to string.
This way You will also corectly handle situation like 12.40 + 00.30.00 = 13.10

Convert a string ddmmyyyyhhmi into yyyy-mm-dd in SQL server

how can I convert a string ddmmyyyyhhmi into yyyy-mm-dd?
A string 210420161701 (a varchar) into 2016-04-21 (a datetime data type).
I tried a combination of sub-string, cast, convert but can't seem to make it work.
Please kindly help thanks.
The first 2 lines are just setting up the variable, but the following works (as long as the format is always the same). You may also need to make sure the date format of your SQL installation is correct (SET DATEFORMAT). You can also change the datatype from date to datetime if required.
DECLARE #Date Varchar(100) = '210420161701'
DECLARE #DateVariable date
SELECT #DateVariable = SUBSTRING(#date,5,4) + '-' + SUBSTRING(#date,3,2) + '-' + SUBSTRING(#date,1,2)
SELECT #DateVariable
select
SUBSTRING(convert(varchar(10),'210420161701',103),5,4)+ '-'+
SUBSTRING(convert(varchar(10),'210420161701',103),3,2)+'-'+
SUBSTRING(convert(varchar(10),'210420161701',103),1,2)

Sql convert date format

I want to convert dateformat from mm/dd/yyyy to yyyy/mm/dd. I want the output in datetime format.
I tried this
convert(datetime,convert(varchar,getdate(),111),123)
but doesn't work. The error is "explicit conversion to datetime not available"
What is the best way to solve this problem? I'm using Sybase.
Try this
select convert(varchar,CAST('12/11/2010' as DateTime),111)
That won't work. The DATETIME data type has its own format that is really the amount of time that has passed since a fixed reference date; if you ask for a DATETIME it will always be returned according to that format.
How it is displayed to an end user is a function of the client. You can use CONVERT to convert it to a string and specify a format for how it is displayed in the string, but then you're returning a string, not a DATETIME. You can return it as a DATETIME (which has no inherent display format), and then it is up to the client application or OS to define how it is formatted for display. In client applications you also typically have formatting functions that display a date/time according to a format you specify. And if you haven't specified it explicitly in an application, then the display of the date/time will typically be defined by the localization settings in the OS.
Basically, there is a difference between the data type - DATETIME - and its representation to end users.
Formatting is something that should be done in the presentation tier not the data tier. However, most vendors, like Sybase, provide the ability to do rudimentary formatting:
Select Cast( Year(GetDate()) As char(4) )
+ '/' + Right( '00' + Cast( Month(GetDate()) As varchar(2) ), 2 )
+ '/' + Right( '00' + Cast( Day(GetDate()) As varchar(2) ), 2 )
Try this query
select (CONVERT(varchar(10), GETDATE(), 120))

SQL Fastest way to compare two dates (non standard varchar format and datetime)

I have two "date" fields that I need to join on.
The first is a normal datetime in the format yyyy-mm-dd hh:mm:ss
The second is a varchar(8) in the red-headed step child format mmddyyyy
Now this gets painful because there is no easy way to convert to the corresponding type. There is a built-in format that is yyyymmdd but that doesn't match the varchar format.
There are two paths I can see:
declare #normal_date as datetime;
declare #hated_date as varchar(8);
set #normal_date='1974-11-01 00:00:00.000'
set #hated_date='11011974'
--cast to date time with string splits
select #normal_date
where CONVERT(datetime, RIGHT(#hated_date,4)+LEFT(#hated_date,2)+SUBSTRING(#hated_date,3,2))=#normal_date
--convert normal date to ackward format
select #normal_date
where REPLACE(CONVERT(varchar(10),#normal_date,101), '/','')=#hated_date
Which is better? Or is there a better way?
Edited to show costs
--Operator cost (39%)
CONVERT(datetime, RIGHT(#hated_date,4)+LEFT(#hated_date,2)+SUBSTRING(#hated_date,3,2))=#normal_date
--Operator cost (57%)
REPLACE(CONVERT(varchar(10),#normal_date,101), '/','')=#hated_date
--Operator cost (46%)
cast(stuff(stuff(#hated_date, 3,0, '/'),6,0,'/') as datetime)=#normal_date
--Operator cost (47%)
RIGHT(#hated_date, 4) + LEFT(#hated_date, 4)=#normal_date
This is yyyymmdd no?
RIGHT(#hated_date, 4) + LEFT(#hated_date, 4)
So, your script becomes
declare #normal_date as datetime;
declare #hated_date as varchar(8);
set #normal_date='1974-11-01 00:00:00.000'
set #hated_date='11011974'
--SELECT #hated_date = RIGHT(#hated_date, 4) + LEFT(#hated_date, 4))
select 'hurrah' WHERE #normal_date = RIGHT(#hated_date, 4) + LEFT(#hated_date, 4)
Another approach is this:
MONTH(#normal_date)*1000000 + DAY(#normal_date)*10000 + YEAR(#normal_date)
=
CAST(#hated_date AS INT)
one more thing: it is more precise to compare real execution costs than to rely on the optimizer's estimates.
Try this:
select cast(stuff(stuff('11011974', 3,0, '/'),6,0,'/') as datetime)
Update
Suggest you either fix the column to be datetime or add a datetime column to the table and convert the data so that you only have to do this conversion once when the data is entered (and once of course for existing data) This could probaly even be a calculated column. This is NOT something you want to be doing in select statements. If necessary create a dateconversion table with every opossible date in both formates and join to it if the table can't be changed.
You might also want to check to make sure there are no invalid dates in there which is always a possibility with storing dates in a data type other than a datetime one.

How can i change datetime format in sql?

How can i change dateformat?Forexample:
2009-06-10 10:16:41.123->2009-June
2009-05-10 10:16:41.123->2009-May
You should not change the date-format in your database.
You should just make sure that , when displaying the date, you correctly format the date, so that you display them in the format that you want.
How to do that, is related to the language you use in your program.
You can also output the date directly in the format you want using the method of ck.
Try this:
select cast(datepart(year, mydatecolumn) as char(4)) + '-'
+ datename(month, mydatecolumn)
from mytable
To interpret input use SET DATEFORMAT
To cast to character, see the CONVERT styles.
To format output, use whatever your client environment uses to format output, SQL itself has no output but TDS and display is left to the client.
Its a pain to do custom formats without writing your own function.
best i have is
SELECT NewFormat = YEAR(GETDATE()) + '-' + DATENAME(month, GETDATE())