vendredi 6 novembre 2015

How to connect to Microsoft SQL Server database instance via Internet with PHP?

I am new to Microsoft SQL Server and am not sure how to connect via internet with PHP to retrieve some data from a table in a database instance in MS SQL Server in the server.

The main server is connected via hamachi with a few other outlets that will periodically update some data to the main server database

the "Server" is actually a desktop computer running on win 8 that is on 24/7

I got the server hostname ( which is the pc name/something ) and created a login account + password with Microsoft SQL Server Management Studio that can access the database I want locally.

Bu I have no idea how the http:// url looks like or how to connect to the DB via internet with PHP ...

or is it possible to connect with just the IP?

Do I need to do Port Forwarding + install PHP, apache, SQL FTP etc and setup the server?

jeudi 5 novembre 2015

MSSQL - DATEDIFF not accurate

Im trying to retrieve the month difference of two dates but it seems like i cant find a way to get the accurate months.

Here are the queries i tried so far :

SELECT DATEDIFF(month,convert(datetime, '11/05/2015'), convert(datetime, '12/06/2015')) - 1

This will result to 0 which is wrong and when i used another date :

 SELECT DATEDIFF(month,convert(datetime, '12/31/2015'), convert(datetime, '01/01/2016')) - 1

This would yield to 1.

Leap year must also be considered.

Exporting SQL Server 2005 database to CSV - Columns contain only question marks

I am trying to export a SQL Server 2005 database using ESF Database Migration Toolkit to .CSV, which I can then - hopefully - manipulate enough to create an Import file for importing into WordPress.

However, the current site is in Korean language and shall remain in Korean on the new server running MySQL.

After exporting all of the tables, I am finding columns where I would expect to find "some korean string" to contain "T???? ?????? ?????" - no Korean words in sight! I have tried exporting using ANSI, OEM and UNICODE options in ESF, each provides the same result.

Anyone?

I have string with number, how to get number out of this string in sql server

I have string something like ACGI SHIPPING (3471) and i need to get 3471 number out of it.

If there is no number just print 0. in sqlserver

SQL Query to count multiple values from one table into specific view

I like to request your help. I can get the results seperated but now i want to create a query which has it perfect for a external person. my explanation:

I have a statistics database with in this database a table when some records comes in and each records has several columns with values etc...

Now one of these columns is called "MT" MT Column can have only one of the following values per records: A,B,C,D,E The records also have a columne called TotalAmount which indicate a size of a value outside the database. This TotalAmount column is numeric without decimals and can have a value between 1 and 10.000. And the last part is the records it self, the table has X amount of records.

So Basicly i need to create a query which seperates each MT value and calculates the amount of records per MT and the sum of TotalAmount.

This is on SQL Server 2005.

Many thanks for your assistance!

mercredi 4 novembre 2015

How to use dynamic SQL in SQL Server 2005 to alter login

I have a script that works fine in SQL Server 2008R2 but it couldn't work in SQL Server 2005. Is there any way that can work for both version?

Thanks!

declare @SQL nvarchar(max)  = ''

select @SQL = @SQL + 'ALTER LOGIN ' + QUOTENAME(DisabledName) + ' DISABLE;'
from [SysAdmin].[dbo].[DisabledAccountHistory]
where DisabledName in (select name 
                       from sys.server_principals 
                       where is_disabled = 0)
group by DisabledName

exec sp_executesql @SQL

sql-server-2005 query 2nd record of 3 consecutive days based on date

I need to fetch the second record for each employee that had events (access to building) in 3 consecutive days and only the oldest result for each employee.

Time is not taking into consideration.

My table look like this:

EMPID   EVENTIME
4   2015-08-05 13:34:54.000
4   2015-08-19 16:29:32.000
4   2015-08-21 16:30:35.000
4   2015-08-24 13:51:25.000
4   2015-08-24 16:32:39.000
4   2015-08-26 13:48:32.000
4   2015-08-26 16:29:58.000
4   2015-08-27 16:30:07.000
4   2015-08-28 14:00:02.000
4   2015-08-28 16:29:09.000
19  2015-08-10 07:27:10.000
19  2015-08-10 15:18:51.000
19  2015-08-11 07:33:12.000
19  2015-08-11 07:33:16.000
19  2015-08-11 10:19:56.000
19  2015-08-11 15:49:12.000
19  2015-08-12 07:21:06.000
19  2015-08-12 10:37:53.000
19  2015-08-12 12:48:12.000
19  2015-08-12 14:11:25.000
19  2015-08-12 15:01:18.000
19  2015-08-13 07:25:38.000
21  2015-08-03 10:07:00.000
21  2015-08-10 08:00:41.000

And the desired results would look like this:

EMPID   EVENTIME
4   2015-08-27 //first record that had an event 1 day before and 1 day after
19  2015-08-11 //first record that had an event 1 day before and 1 day after

Since access is controlled by proximity cards, and these are tested and then a few days later delivered to new employees, then the first record won't work, and the first record of these 3 consecutive days won't work either since cards are delivered in the afternoon missing events from the morning. That's why I need a query for the second record of the first 3 consecutive days.

This will give me a start date for each employee that I can use for reports.

I'm using mssql 2005.

Thank you!