sql combining two columns one of which is a datetime - sql

I have the following sql code (ms sql server 2008)
select (analysisno + ' ' + '-' + ' ' + description + ' ' + '-' + ' ' + formdate) as columnA
from Old_Analysis_Data
order by formdate
I get the following error Conversion failed when converting date and/or time from character string.
AnalysisNo is a varchar(10)
description is a varchar(500)
formdate is a datetime
(not my table, its an old one)
Any ideas, as cant find an answer on google.

Convert the time to a string using Convert before concatenation:
SELECT ( analysisno + ' ' + '-' + ' ' + description + ' ' + '-' + ' '
+ CONVERT(VARCHAR(20), formdate, 100) ) AS columnA
FROM
Old_Analysis_Data
ORDER BY
formdate
In this case, 100 is a style that sets datestamp format to mon dd yyyy hh:miAM (or PM) as an example
See http://www.w3schools.com/sql/func_convert.asp

Try this:
select (analysisno + ' - ' + description + ' - '
+ convert(varchar(100),formdate)) as columnA
from Old_Analysis_Data order by formdate

Instead of concatenating formdate directly, convert it to string first,
convert(varchar(15), formdate, 103)
which gives you the following format
dd/MM/yyyy

Related

SQL Server 2008 Alter Table Concatenate

I need to concatenate 2 ints and a varchar column into plan_no so it would look like this
Any help would be much appreciated.
We can try using CONVERT here to convert the numeric fields into text:
SELECT
CONVERT(varchar(10), lot) + '-' + forester + '-' +
CONVERT(varchar(10), year) AS plan_no
FROM yourTable;
If you want an update, then just use:
UPDATE yourTable
SET plan_no = CONVERT(varchar(10), lot) + '-' + forester + '-' +
CONVERT(varchar(10), year);
You need to do conversions :
select *, cast(lot as varchar(255)) + '-' +forester + '-' +cast([year] as varchar(5)) as plan_no
from table t;
You can alter your DDL :
alter table t
add plan_no as (cast(lot as varchar(255)) + '-' +forester + '-' +cast([year] as varchar(5)))
Edit :
update t
set plan_no = cast(lot as varchar(255)) + '-' +forester + '-' +cast([year] as varchar(5))
where plan_no is null;
I think you want to update data in your current table:
UPDATE TableName
SET plan_no = cast(lot as nvarchar(50)) + '-' + forester + '-' + cast([year] as nvarchar(50))
Please note: if lot, forester or year columns can contain nulls then you need to wrap values by ISNULL() function

SELECT statement issue - Joining of data of different fields in one cell of Gridview

I am using following SELECT statement for Gridview:
SelectCommand="SELECT ID, Date, Train, I_R, Dir_Ind, Detn, Rly, DiV, Loco, Shed, locoClass, loco_type,
(maj_sch_type + ','+ ' ' + maj_sch_place +',' +' '+ (CAST(maj_sch_dt as VARCHAR(11)) + ' '+'/'
+ ' ' + min_sch_type +',' + ' ' + min_sch_place + ',' + ' '+(CAST(min_sch_dt as VARCHAR (11))) )) as "major",
Equipt, I_R, reason
FROM PunctualityMain Order by Date ASC"
With the above, I am getting results in single cell from 6 (3 + 3) different fields of SQL data as
POH, KPA, Jan 2 2012 / IB, Shed, Apr 18 2012
Issue is this that whenever data in any group of 3 fields (before or after '/') is blank, Gridview cell is remain completely blank. If both group of fields having data than Gridview dispaly is OK as mentioned above. Can SELECT statement be modified for showing data in Gridview with any one group of fields are empty?
If you try to concatenate a null string with a non-null string, the result will be null.
Select NULL + 'test'
this will return NULL.
Also, I think your code didn't copy paste extremely well into your question, but you need to use the ISNULL() function. I think you can get the point. You'll probably have to clean up the code a bit for it to run. Just wrap every field that you are concatenating with ISNULL(MyField, '')
SELECT
ID,
Date,
Train,
I_R,
Dir_Ind,
Detn,
Rly,
DiV,
Loco,
Shed,
locoClass,
loco_type,
(isnull(maj_sch_type, '') + ', ' + isnull(maj_sch_place, '') +', '+ (isnull(CAST(maj_sch_dt as VARCHAR(11), '')) + ' / ' + isnull(min_sch_type, '') +', ' + isnull(min_sch_place, '') + ', '+ (isnull(CAST(min_sch_dt as VARCHAR (11)), '')) )) as "major",
Equipt,
I_R,
reason
FROM PunctualityMain
Order by Date ASC
Reference
IsNull

SQL SELECT multiple columns into one

I have this query in SQL Server 2008:
SELECT Id, Year, Manufacturer, Model
FROM Table
and I need something like this...
SELECT Id, (Year + [space] + Manufacturer + [space] + Model) AS MyColumn
FROM Table
How can I get this result?
I think all integer or numeric data types you need convert to String data type. When you can create your new column.
Query:
SELECT Id, (Cast([Year] as varchar(4)) + ' ' + Manufacturer + ' ' + Model) AS MyColumn
FROM Tablename
just use ' '
SELECT Id, ([Year] + ' ' + Manufacturer + ' ' + Model) AS MyColumn
FROM Tablename

SQL print a space between concat statements

Concatenating two columns together, Just want them to be displaying together in column with a space between the two numbers. It keeps adding the two numbers together. One is a bigint other is a smallint.Will be displayed in an SSRS report eventually but right now just using SQL to query the data
(NBR +''+ ACCT_NBR) as acct,
Though you didn't mention the database, try
MySQL
concat(NBR,' ',ACCT_NBR) as acct
SQL Server
CAST(NBR AS VARCHAR)+' '+CAST(ACCT_NBR AS VARCHAR) as acct
You don't mention what flavor of SQL you're using, but depending, you may need to convert the values to strings first. For SQLSever...
(Cast(NBR as varchar(20)) + ' ' + Cast(ACCT_NBR as varchar(20))) as acct,
I know this post is already answered and is correct. But would like to post the below answer because from SQL Server 2017 onwards, this is too easy and someoen might find this helpful in future.
CONCAT_WS(CHAR(10),Cast(NBR as varchar(20)) ,Cast(ACCT_NBR as varchar(20))) as acct
CHAR(10) can be for space in SQL SERVER. You can replace CHAR(10) with any separator you like to. For example,
CONCAT_WS(',',Cast(NBR as varchar(20)) ,Cast(ACCT_NBR as varchar(20))) as acct
the above query will add the separator ',' between each strings being concatenated.
If its an Oracle Database, try
NBR || ' ' || ACCT_NBR as acct
Stuff(Coalesce(', ' + [Address1], '') + Coalesce(', ' + [Address2], '') + Coalesce(', ' + [City], '') + Coalesce(', ' + [State], '') + Coalesce(', ' + [Country], '') +Coalesce('-' + [Zip], ''), 1, 1, '') AS [Address] FROM Customers
I know I'm 7 years late, but felt like I should give this a try.
(NBR +space(5) + ACCT_NBR) as acct
This will add 5 whitespaces between the 2 concatenated items.
My example on how I tested was as follows:
select
+ N'(Contactname: '+ contactname + space(5) + N'(Company_Name: ' + companyname + N')' + N'(Contact_Title: ' + contacttitle + N')'
from Sales.Customers
In Teradata SQL you can use the below. It will always pull the last 3 days.
WHERE create_ts BETWEEN (DATE -3 || ' ' || '00:00:00') AND (DATE -1 || ' ' || '23:59:59')

Conversion error in SQL Server?

In my SQL query:
insert into Tbl_EmpMovement_T values (
'004101',
'2011-8-26',
CONVERT(datetime,convert(varchar(10),'2011-8-26',120)+' ' +
CONVERT(varchar(2),SUBSTRING('8:16',1,2))+':'+
convert(varchar(2),SUBSTRING('8:16',4,2))+':00.000',120),
CONVERT(datetime,convert(varchar(10),'2011-8-26',120)+' ' +
CONVERT(varchar(2),SUBSTRING('6:02',1,2))+':'+
convert(varchar(2),SUBSTRING('6:02',4,2))+':00.000',120)
)
I am getting this error
Conversion failed when converting date and/or time from character
string.
I would rather try this:
insert into Tbl_EmpMovement_T values (
'004101',
'2011-8-26',
CAST ('2011-8-26' + ' ' +
( SUBSTRING('16:02',0, PATINDEX('%:%', '16:02')+1)) +
( SUBSTRING('16:02',PATINDEX('%:%', '16:02')+1, 2)) AS datetime),
CAST ('2011-8-26' + ' ' +
( SUBSTRING('16:02',0, PATINDEX('%:%', '16:02')+1)) +
( SUBSTRING('16:02',PATINDEX('%:%', '16:02')+1, 2)) AS datetime)
)
This way, you wont have a problem when the time part moves between single and double digit hours.