how to join two query into one column - sql-server

select
Name,
convert(varchar, Connect, 103) Connect,
convert(varchar, Connect, 108) Connect,
Job
from
TableWork
where
Account ='Account1'
Result :
Mary | 23/01/2022 | 18:39:00 | Manager
I need to join the two columns of times so they look like this:
Mary | 23/01/2022 18:39:00 | Manager
Thanks a lot for the help

I think you are looking for something like this
SELECT
Name,
convert(varchar, Connect, 103) Connect || ' ' || convert(varchar, Connect, 108) Connect AS Date,
Job
FROM
TableWork
Account ='Account1'
The || is an operator for string concatenation and whatever you put inside the quotation marks will be concatenated, in this case a white space.
The AS is to indicate a temporal name for the new column, the one formed with the concatenated strings, in this case "Date".

Related

How to convert mm/dd/yyyy militarytime -> mm/dd/yyyy hh:mm:ss AM/PM

I'm using the following code and almost getting what I'm looking for:
SELECT sdb.NAME AS DatabaseName
,COALESCE(CONVERT(VARCHAR(10), cast(max(bus.backup_finish_date) as date), 101) + ' ' + convert(varchar(12), max(bus.backup_finish_date), 108), 'Never Restored') as [LastBackupTime]
FROM sys.sysdatabases sdb
INNER JOIN dbo.backupset bus
ON bus.database_name = sdb.NAME
GROUP BY sdb.NAME,
bus.backup_finish_date
Your result should be something like:
mm/dd/yyyy HH:mm:ss
I'm trying to get
mm/dd/yyyy hh:mm:ss AM/PM
I've tried multiple converts, a series of casts, ltrim/right, and even offering homage to the T-SQL overlords. No luck yet.
I've even tried
SELECT sdb.NAME AS DatabaseName
--Code below needs changed to show Date & time--
,COALESCE(CONVERT(VARCHAR(30), MAX(bus.backup_finish_date), 100), 'Never
backed up.') AS LastBackUpTime
FROM sys.sysdatabases sdb
INNER JOIN dbo.backupset bus
ON bus.database_name = sdb.NAME
GROUP BY sdb.NAME,
bus.backup_finish_date
but that gets me (for example) Mar 21 2017 10:47AM. We really prefer 3/21/2017 10:47AM.
Suggestions? I'm still picking this apart but could use some help.
Thanks!
If you are using SQL Server 2012 or later, you can use FORMAT():
Select Format(Max(bus.backup_finish_date), N'MM/dd/yyyy hh:mm:ss tt')
If you are using SQL Server 2012 or later you can use FORMAT, although be wary of doing this on large data sets.
SELECT FORMAT(GETDATE(), 'MM/dd/yyyy hh:mm:sstt')
For earlier versions, or if performance is a concern, For earlier versions, or if performance is a concern, you can concatenate the date in the format MM/dd/yyyy (style 101), with the time in the format hh:mm:ss (style 8) and a case expression to determine AM or PM
SELECT CONVERT(VARCHAR(10), GETDATE(), 101) + ' '
+ CONVERT(VARCHAR(10), GETDATE(), 8)
+ CASE WHEN DATEPART(HOUR, GETDATE()) < 12 THEN 'AM' ELSE 'PM' END
HOWEVER, formatting is a job for the presentation layer. If it was me doing this, then I would just send the native datetime, including nulls back to the presentation layer and let this handle it. It means that in your application layer you can still work with the dates, perform date calculations, or sort etc without the worry that 15/01/2017 is going to appear after 02/02/2017. It also means you can display dates in the end user's preferred locale, rather than yours.
One easiest way is to use format but it is not highly performant:
select FORMAT(Max(bus.backup_finish_date),'MM/dd/yyyy hh:mm:ss tt')
For earlier versions one another naive way of doing is as below:
Select CONVERT(VARCHAR(10), getdate(), 101) + ' ' + LTRIM(RIGHT(CONVERT(CHAR(20), getdate(), 22), 11))
Instead of GetDate() use your date
But that is already mentioned by #GarethD So never mind

Get minimum value from columnconcat from three tables + lpad inside. MS SQL

I am working in SQL Server Managment Studio 2014.
In the project I am working on I have three tables, each containing 2 columns, one datetime with the exact date (but on time is contained) and the other one - smallint containing time (8:55 is 855 value, while for example 14:45 is 1445).
What I want to do is to get minimum value which is merged from both of those columns from all of those three tables.
What I have figure out by myself until now is:
Use lpad("U_StartTime", 0, '4') to fill values like 855 into 0855 (for exact comparison). However lpad is not recognized at my studio.
lpad is not recognized built in function
Then I can merge both columns like this:
SELECT concat("U_StartDate", ' ', "U_StartTime") FROM "TABLE1".
This is ok until I try make it with lpad.
Then I may take all values to consider like this:
SELECT concat("U_StartDate", ' ', "U_StartTime") FROM "TABLE1"
UNION
SELECT concat("U_StartDate", ' ', "U_StartTime") FROM "TABLE2"
...
And I can take MIN(column) but I do not know how to get MIN from the whole UNION SELECT (of those three tables).
Thank you in advance for any advices on my current sql problems.
edit - image for NikHil:
EDIT:
I have changed the way a bit. Now I am modifying datetime object rather than working on string comparison. As an example for someone I paste part of the code:
select DATEADD(minute, "U_StartTime"%100, DATEADD(hour, "U_StartTime"/100, "U_StartDate")) from "TABLE1"
rather than
select MIN(concat("U_StartDate", ' ', RIGHT('0000' + "U_StartTime", '4'))) from "TABLE1"
You can use RIGHT instead of lpad
SELECT RIGHT('0000' + '855', 4) -- 0855
SELECT RIGHT('0000' + '1445', 4) -- 1445
Query looks like
SELECT MIN(RIGHT('0000' + YourColumn, 4) * 1)
FROM
Tbl
may be you can try this
select data from
(
select concat("U_StartDate", ' ', "U_StartTime")as 'data' from "TABLE1"
UNION
select concat("U_StartDate", ' ', "U_StartTime")as 'data' from "TABLE2"
...
)
where data is not null
order by data asc
LIMIT 1;

convert datetime is not running to view,

i tried to this query. but its not running. how can i do?
SELECT dbo.isemri_data.sipnum,
dbo.kayitlar.id,
dbo.kayitlar.makina_id,
dbo.kayitlar.personel,
dbo.kayitlar.isemrino,
dbo.kayitlar.tarih,
dbo.kayitlar.gercek_hiz,
dbo.kayitlar.durus_kod,
dbo.kayitlar.miktar,
dbo.kayitlar.fire
FROM dbo.isemri_data
LEFT OUTER JOIN dbo.kayitlar
ON dbo.isemri_data.isemrino = dbo.kayitlar.isemrino
WHERE CONVERT(VARCHAR(10), dbo.kayitlar.tarih, 104) BETWEEN
CONVERT(VARCHAR(10), '12.08.2015', 104) AND
CONVERT(VARCHAR(10), '19.08.2015', 104)
AND CONVERT(VARCHAR(3), dbo.kayitlar.makina_id) = 'M1'
AND dbo.kayitlar.isemrino LIKE '%'
if delete
CONVERT(varchar(10),dbo.KAYITLAR.TARIH,104) BETWEEN convert(varchar(10),'12.08.2015',104) and convert(varchar(10),'19.08.2015',104)
query is running.
or if run to only main table allready running my query.
i cant find what is the problem.
Since you have mentioned the data type is Datetime , DO NOT convert it to anything else in there where clause, do it in select if you want to.
SELECT dbo.isemri_data.sipnum,
dbo.kayitlar.id,
dbo.kayitlar.makina_id,
dbo.kayitlar.personel,
dbo.kayitlar.isemrino,
dbo.kayitlar.tarih,
dbo.kayitlar.gercek_hiz,
dbo.kayitlar.durus_kod,
dbo.kayitlar.miktar,
dbo.kayitlar.fire
FROM dbo.isemri_data
LEFT OUTER JOIN dbo.kayitlar
ON dbo.isemri_data.isemrino = dbo.kayitlar.isemrino
AND dbo.kayitlar.tarih >= '20150812' -- use ANSI date YYYYMMDD
AND dbo.kayitlar.tarih <= '20150819' -- use ANSI date YYYYMMDD
AND CONVERT(VARCHAR(3), dbo.kayitlar.makina_id) = 'M1'
AND dbo.kayitlar.isemrino LIKE '%'
Converting your datetime to a string means sql server treats them values as strings and not a date/datetime values hence you will get unexpected results Also sql server will not be able to make use of any indexes defined on that column if there are any.
Keep date/datetime values as it is and change the format of date in presentation layer for more easy to eyes date/datetime format.
i found answer. if i use
dbo.kayitlar.tarih BETWEEN
CONVERT(DATETIME, '12.08.2015', 104) AND
CONVERT(DATETIME, '19.08.2015', 104)
running query.
thanx for your help

Convert varchar to date with malformed values in SQL Server 2008

This whole date code is beyond my understanding but non the less I need to convert a column in a query result from varchar to date. I have some malformed values and I'm trying to filter them out.
SET DATEFORMAT dmy
SET LANGUAGE us_english
select convert(datetime, some_date, 103)
from some_table
where isdate(some_date) = 1
I keep getting
Conversion failed when converting date and/or time from character string
When querying for the malformed values I get these two records
some_date
---------
621
232
Is there any way to validate the format using isdate ?
Thanks, Frustrated Oracle DBA.
EDIT
Tried #Sean Lange suggestions and indeed it looked promising
select convert(datetime, some_date, 103)
from ( select * from some_table where isdate(some_date) = 1 )
But then I added a where clause
select convert(datetime, some_date, 103)
from ( select * from some_table where isdate(some_date) = 1 )
where convert(datetime, some_date ,103) between
convert(datetime, '01/07/2014')
and convert(datetime,'31/07/2014')
How does a where clause being executed before the from?
Since we know that a valid date looks like dd/mm/yyyy how about a simple structure test?
select convert(datetime, some_date, 103)
from some_table
where len(some_date) = 10 --must have exactly the expected number of characters
This could be expanded upon (depending on requirements)
and some_date like '__/__/____'
Or maybe
and some_date like '[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]'
Anything better would probably need you to put together a custom function that does the CONVERT in a try block, returning NULL in the catch block if its an invalid record. Depends on your requirements...
The problem is happening and will continue to happen because you can't be certain which order the sql engine will take here. One time it might filter the rows first and another time it does the conversion. So what you need to do is force the order the engine works. We can do this by using a CTE to get only the rows we want to convert.
with MyCTE as
(
select some_date
from some_table
where ISDATE(some_date) = 1
)
select CONVERT(datetime, some_date, 103)
from MyCTE
If you're positive that these are the only malformed values and all malformed values will look like this, you could just go ahead and use a hackish CASE statement
SELECT (CASE WHEN LEN(datetime) > 3 THEN convert(datetime, some_date, 103) ELSE NULL),
...
It's really not elegant, but datetimes are notoriously difficult to make elegant because there are so many fragmented formats. If possible, it saves more headaches just to make sure that your data is correctly formatted in the first place through form validation and regex stuff, but since that's not always possible, we have crummy hacks like this to patch ourselves through. Best of luck!

Remove time from DateTime sql server 2005

I need date part from datetime. in format of "dd-mm-yyyy"
I have tried follwoing
Query:
select Convert(varchar(11), getdate(),101)
Output:
01/11/2011
Query
SELECT cast(floor(cast(GETDATE() as float)) as datetime)
Output
2011-01-11 00:00:00.000
Query:
SELECT
CONVERT(VARCHAR(MAX),DATENAME(DD,GETDATE())) + '-' +
CONVERT(VARCHAR(MAX),DATEPART(MONTH,GETDATE())) + '-' +
CONVERT(VARCHAR(MAX),DATENAME(YYYY,GETDATE())) `
Output:
11-1-2011 i.e. "d-m-yyyy"
I required output in "dd-mm-yyyy" format.
SELECT CONVERT(VARCHAR(10),GETDATE(),105)
Try:
SELECT convert(varchar, getdate(), 105)
More here.
Here you can find some examples how to do this: http://blog.pengoworks.com/index.cfm/2009/1/9/Useful-tips-and-tricks-for-dealing-with-datetime-in-SQL
Using the CONVERT function "works" but only if you're comparing strings with strings. To compare dates effectively, you really need to keep the SMALLDATETIME data type strongly typed on both side of the equation (ie "="). Therefore 'apros' comment above is really the best answer here because the blog mentioned has the right formulas to use to strip off the time component by "flattening" it to midnight (ie 12:00:00) via rounding and any date column in SQL Server 2005 will always default to 12:00:00 if the date is given without a time.
This worked for me ...
select dateadd(day, datediff(day, '20000101', #date), '20000101')

Resources