Tag: sql

Find the start and end date of two different dates in SQL Server, Group by month

If input is provided like below declare @from datetime = ‘2016-09-15’ declare @to datetime = ‘2016-12-25’ Then output should be like below Month Start_date End_date September 2016-09-15 2016-09-30 October 2016-10-01 2016-10-31 November 2016-11-01 2016-11-30 December 2016-12-01 2016-12-25 If input is provided like this: declare @from datetime = ‘2016-12-05’ declare @to datetime = ‘2016-12-25’ Then output […]

Case When SQL to LINQ

I am still new to LINQ and trying to convert this SQL command Id | TitleName —— | —— 1 | Accounts 2 | Buyer 3 | Engineer 4 | Other 5 | Apple SELECT Id, TitleName FROM dbo.Title ORDER BY CASE WHEN TitleName = ‘Other’ THEN 0 ELSE 1 END DESC, TitleName ASC This […]

SQL separate one column into three

Basically i’m trying to take an address field that is one column and separate it into three columns, the first having the address number, the second having the street name, and the third having if its an apt # or lot # Address ——————- 990 A street Apt 1B 127 B street Lot 3 So […]

All records updated with same value when updating database Record

I updated sql data base using c# text box but all records updated with same value what I entered in that text box. where I did wrong. Below My code private void btnsave_Click(object sender, EventArgs e) { try { string connectionString = (@”Data Source = M2\SQL2016; Initial Catalog = inventoryDB; Integrated Security = True”); using […]

multiple condition in select statement

I have a SELECT statement that contains a multiple conditions. Here is my code for the query: SELECT G.CLOSED_MONTH, EE.BASIC_SALARY, V.EMPLOYEE_NO, G.CLOSED_YEAR CASE WHEN ((G.CLOSED_YEAR = YEAR (GETDATE()) AND G.CLOSED_MONTH <= MONTH (GETDATE()) THEN EE.BASIC_SALARY) WHEN (G.CLOSED_YEAR BETWEEN EE.DATE_JOINED AND YEAR (GETDATE()) THEN CASE WHEN V.ACTIVITY_TYPE = ‘0’ THEN EE.BASIC_SALARY – V.ACTIVITY_VALUE ELSE EE.BASIC_SALARY + […]

SQL Server 2016 Developer version can not connect to (LocalDB)\MSSQLLocalDB

I have reinstalled my SQL Server 2016 as for an unknown reason which lead to the SQL Server down…But, when I finished the installation, I found I can not connect to (LocalDB)\MSSQLLocalDB any more. I got this error message from SQL Server. Cannot connect to (LocalDB)\MSSQLLocalDB. A network-related or instance-specific error occurred while establishing a […]

How to check dates condition from one table to another in SQL

Which way we can use to check and compare the dates from one table to another. Table : inc +——–+———+———–+———–+————-+ | inc_id | cust_id | item_id | serv_time | inc_date | +——–+———+———–+———–+————-+ | 1 | john | HP | 40 | 17-Apr-2015 | | 2 | John | HP | 60 | 10-Jan-2016 | | […]

TSQL Dynamic Pivot

I have a “small” issue with a PIVOT SQL script. I have based my current solution on a similar question here Dynamic PIVOT . I have managed to write the PIVOT script and largely it is ok. However, my situation is this, instead of getting the unique entries onto single rows, the script outputs something […]

Handling millisecond in SQL with IsDate() Function

I need to cast the above DATE column of varchar to date format. So I used, SELECT TOP 1000 [Tagname] ,FORMAT((CASE WHEN 1 = ISDATE(CAST([Date] AS VARCHAR))THEN CAST(CAST([Date] AS VARCHAR)AS datetime) else NULL END ), ‘yyyy-MM-dd HH:mm:ss.fff’ ) AS [Date] ,[Value] ,[VValue] ,[OPCQuality] ,[QualityDetail] ,[QualityDescription] ,[StatusCode] ,[ErrorMessage] FROM [Dashboard].[dbo].[Data20170202123655985] while using this, the fields with […]

SQL Escape '#' Symbol

I have a SQL query that inserts data to the database. Below is my sample data: OrderID: 1 Email: #TestDistribution However, when I am trying to insert the OrderID and Email values to the table but the ‘#‘ cannot be read by SQL Server. So that, how would I escape it? I have an idea […]

MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.